From: Marek Polacek Date: Sat, 18 Apr 2020 03:48:11 +0000 (-0400) Subject: c++: Fix crash with template spec in different namespace [PR94255] X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=21968d4ae067e3fa1c1728c8db26478e8ac8ad0b;p=gcc.git c++: Fix crash with template spec in different namespace [PR94255] This is an ICE on invalid, because we're specializing S::foo in the wrong namespace. cp_parser_class_specifier_1 parses S::foo in M and then it tries to push the nested-name-specifier of foo, which is S. By that, we're breaking the assumption of push_inner_scope that the pushed scope must be a scope nested inside current scope: current scope is M, but the namespace context of S is N, and N is not nested in M, so we fell into an infinite loop in push_inner_scope_r. (cp_parser_class_head called check_specialization_namespace which already gave a permerror.) PR c++/94255 * parser.c (cp_parser_class_specifier_1): Check that the scope is nested inside current scope before pushing it. * g++.dg/template/spec41.C: New test. --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 312f1ab0824..7454352020e 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2020-05-07 Marek Polacek + + PR c++/94255 + * parser.c (cp_parser_class_specifier_1): Check that the scope is + nested inside current scope before pushing it. + 2020-05-07 Marek Polacek P1957R2 diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 5832025443d..d67fa3b13d1 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -23873,7 +23873,12 @@ cp_parser_class_specifier_1 (cp_parser* parser) if (nested_name_specifier_p) { scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)); - old_scope = push_inner_scope (scope); + /* SCOPE must be a scope nested inside current scope. */ + if (is_nested_namespace (current_namespace, + decl_namespace_context (scope))) + old_scope = push_inner_scope (scope); + else + nested_name_specifier_p = false; } type = begin_class_definition (type); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index d14d53434d7..bed2d90fb02 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2020-05-07 Marek Polacek + + PR c++/94255 + * g++.dg/template/spec41.C: New test. + 2020-05-07 Marek Polacek P1957R2 diff --git a/gcc/testsuite/g++.dg/template/spec41.C b/gcc/testsuite/g++.dg/template/spec41.C new file mode 100644 index 00000000000..249fde74c3a --- /dev/null +++ b/gcc/testsuite/g++.dg/template/spec41.C @@ -0,0 +1,17 @@ +// PR c++/94255 - crash with template spec in different namespace. +// { dg-do compile { target c++11 } } + +namespace N { + class S { + template struct foo; + }; + namespace M { + using S = ::N::S; + } +} + +namespace N { + namespace M { + template <> struct S::foo {}; // { dg-error "specialization of" } + } +}