From: Marek Polacek Date: Wed, 18 Mar 2020 23:28:14 +0000 (-0400) Subject: c++: DR1710, template keyword in a typename-specifier [PR94057] X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=517f5356bb0ca717f51e937be880e38a828edbf7;p=gcc.git c++: DR1710, template keyword in a typename-specifier [PR94057] Consider template class A { template class B { void fn(typename A::B); }; }; which is rejected with error: 'typename A::B' names 'template template class A::B', which is not a type whereas clang/icc/msvc accept it. "typename A::B" is a typename-specifier. Sadly, our comments don't mention it anywhere, because the typename-specifier wasn't in C++11; it was only added to the language in N1376. Instead, we handle it as an elaborated-type-specifier (not a problem thus far). So we get to cp_parser_nested_name_specifier_opt which has a loop that breaks if we don't see a < or ::, but that means we can -- tentatively -- parse even B which is not a nested-name-specifier (it doesn't end with a ::). I think this should compile because [temp.names]/4 says: "In a qualified-id used as the name in a typename-specifier, elaborated-type-specifier, using-declaration, or class-or-decltype, an optional keyword template appearing at the top level is ignored.", added in DR 1710. Also see DR 1812. This issue on its own is not a significant problem or a regression. However, in C++20, the typename here becomes optional, and so this test is rejected in C++20, but accepted in C++17: template class A { template class B { void fn(A::B); }; }; Here we morph A::B into a typename-specifier, but that happens in cp_parser_simple_type_specifier and we never handle it as above. To fake the template keyword I'm afraid we need to use cp_parser_template_id with template_keyword_p=true as in the patch below. The tricky thing is to avoid breaking concepts. To handle DR 1710, I made cp_parser_nested_name_specifier_opt assume that when we're naming a type, the template keyword is present, too. That revealed a bug: DR 1710 also says that the template keyword can be followed by an alias template, but we weren't prepared to handle that. alias-decl?.C exercise this. gcc/cp: DR 1710 PR c++/94057 - template keyword in a typename-specifier. * parser.c (check_template_keyword_in_nested_name_spec): New. (cp_parser_nested_name_specifier_opt): Implement DR1710, optional 'template'. Call check_template_keyword_in_nested_name_spec. (cp_parser_simple_type_specifier): Assume that a < following a qualified-id in a typename-specifier begins a template argument list. gcc/testsuite: DR 1710 PR c++/94057 - template keyword in a typename-specifier. * g++.dg/cpp1y/alias-decl1.C: New test. * g++.dg/cpp1y/alias-decl2.C: New test. * g++.dg/cpp1y/alias-decl3.C: New test. * g++.dg/parse/missing-template1.C: Update dg-error. * g++.dg/parse/template3.C: Likewise. * g++.dg/template/error4.C: Likewise. * g++.dg/template/meminit2.C: Likewise. * g++.dg/template/dependent-name5.C: Likewise. * g++.dg/template/dependent-name7.C: New test. * g++.dg/template/dependent-name8.C: New test. * g++.dg/template/dependent-name9.C: New test. * g++.dg/template/dependent-name10.C: New test. * g++.dg/template/dependent-name11.C: New test. * g++.dg/template/dependent-name12.C: New test. * g++.dg/template/dependent-name13.C: New test. * g++.dg/template/dr1794.C: New test. * g++.dg/template/dr314.C: New test. * g++.dg/template/dr1710.C: New test. * g++.dg/template/dr1710-2.C: New test. * g++.old-deja/g++.pt/crash38.C: Update dg-error. --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index e97c4e9d1ad..7d1161fd1c1 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,14 @@ +2020-03-26 Marek Polacek + + DR 1710 + PR c++/94057 - template keyword in a typename-specifier. + * parser.c (check_template_keyword_in_nested_name_spec): New. + (cp_parser_nested_name_specifier_opt): Implement DR1710, optional + 'template'. Call check_template_keyword_in_nested_name_spec. + (cp_parser_simple_type_specifier): Assume that a < + following a qualified-id in a typename-specifier begins + a template argument list. + 2020-03-26 Iain Sandoe * coroutines.cc (coro_init_identifiers): Initialize an identifier diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 05363653691..753b3148ab3 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -6266,6 +6266,32 @@ cp_parser_unqualified_id (cp_parser* parser, } } +/* Check [temp.names]/5: A name prefixed by the keyword template shall + be a template-id or the name shall refer to a class template or an + alias template. */ + +static void +check_template_keyword_in_nested_name_spec (tree name) +{ + if (CLASS_TYPE_P (name) + && ((CLASSTYPE_USE_TEMPLATE (name) + && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name))) + || CLASSTYPE_IS_TEMPLATE (name))) + return; + + if (TREE_CODE (name) == TYPENAME_TYPE + && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR) + return; + /* Alias templates are also OK. */ + else if (alias_template_specialization_p (name, nt_opaque)) + return; + + permerror (input_location, TYPE_P (name) + ? G_("%qT is not a template") + : G_("%qD is not a template"), + name); +} + /* Parse an (optional) nested-name-specifier. nested-name-specifier: [C++98] @@ -6389,7 +6415,17 @@ cp_parser_nested_name_specifier_opt (cp_parser *parser, /* Look for the optional `template' keyword, if this isn't the first time through the loop. */ if (success) - template_keyword_p = cp_parser_optional_template_keyword (parser); + { + template_keyword_p = cp_parser_optional_template_keyword (parser); + /* DR1710: "In a qualified-id used as the name in + a typename-specifier, elaborated-type-specifier, using-declaration, + or class-or-decltype, an optional keyword template appearing at + the top level is ignored." */ + if (!template_keyword_p + && typename_keyword_p + && cp_parser_nth_token_starts_template_argument_list_p (parser, 2)) + template_keyword_p = true; + } /* Save the old scope since the name lookup we are about to do might destroy it. */ @@ -6580,18 +6616,8 @@ cp_parser_nested_name_specifier_opt (cp_parser *parser, if (TREE_CODE (new_scope) == TYPE_DECL) new_scope = TREE_TYPE (new_scope); /* Uses of "template" must be followed by actual templates. */ - if (template_keyword_p - && !(CLASS_TYPE_P (new_scope) - && ((CLASSTYPE_USE_TEMPLATE (new_scope) - && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope))) - || CLASSTYPE_IS_TEMPLATE (new_scope))) - && !(TREE_CODE (new_scope) == TYPENAME_TYPE - && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope)) - == TEMPLATE_ID_EXPR))) - permerror (input_location, TYPE_P (new_scope) - ? G_("%qT is not a template") - : G_("%qD is not a template"), - new_scope); + if (template_keyword_p) + check_template_keyword_in_nested_name_spec (new_scope); /* If it is a class scope, try to complete it; we are about to be looking up names inside the class. */ if (TYPE_P (new_scope) @@ -18120,6 +18146,33 @@ cp_parser_simple_type_specifier (cp_parser* parser, } } } + /* DR 1812: A < following a qualified-id in a typename-specifier + could safely be assumed to begin a template argument list, so + the template keyword should be optional. */ + else if (parser->scope + && qualified_p + && typename_p + && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)) + { + cp_parser_parse_tentatively (parser); + + type = cp_parser_template_id (parser, + /*template_keyword_p=*/true, + /*check_dependency_p=*/true, + none_type, + /*is_declaration=*/false); + /* This is handled below, so back off. */ + if (type && concept_check_p (type)) + cp_parser_simulate_error (parser); + + if (!cp_parser_parse_definitely (parser)) + type = NULL_TREE; + else if (TREE_CODE (type) == TEMPLATE_ID_EXPR) + type = make_typename_type (parser->scope, type, typename_type, + /*complain=*/tf_error); + else if (TREE_CODE (type) != TYPE_DECL) + type = NULL_TREE; + } /* Otherwise, look for a type-name. */ if (!type) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 140f2963355..45b19d5061e 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,28 @@ +2020-03-26 Marek Polacek + + DR 1710 + PR c++/94057 - template keyword in a typename-specifier. + * g++.dg/cpp1y/alias-decl1.C: New test. + * g++.dg/cpp1y/alias-decl2.C: New test. + * g++.dg/cpp1y/alias-decl3.C: New test. + * g++.dg/parse/missing-template1.C: Update dg-error. + * g++.dg/parse/template3.C: Likewise. + * g++.dg/template/error4.C: Likewise. + * g++.dg/template/meminit2.C: Likewise. + * g++.dg/template/dependent-name5.C: Likewise. + * g++.dg/template/dependent-name7.C: New test. + * g++.dg/template/dependent-name8.C: New test. + * g++.dg/template/dependent-name9.C: New test. + * g++.dg/template/dependent-name10.C: New test. + * g++.dg/template/dependent-name11.C: New test. + * g++.dg/template/dependent-name12.C: New test. + * g++.dg/template/dependent-name13.C: New test. + * g++.dg/template/dr1794.C: New test. + * g++.dg/template/dr314.C: New test. + * g++.dg/template/dr1710.C: New test. + * g++.dg/template/dr1710-2.C: New test. + * g++.old-deja/g++.pt/crash38.C: Update dg-error. + 2020-03-26 Iain Sandoe * g++.dg/coroutines/torture/co-ret-09-bool-await-susp.C: Amend diff --git a/gcc/testsuite/g++.dg/cpp1y/alias-decl1.C b/gcc/testsuite/g++.dg/cpp1y/alias-decl1.C new file mode 100644 index 00000000000..e5397e71bd6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/alias-decl1.C @@ -0,0 +1,9 @@ +// DR 1710 - Missing template keyword in class-or-decltype +// { dg-do compile { target c++14 } } + +template struct S { + template struct A; + template using U = typename A::foo; +}; +template typename S<1>::U::type a; +template typename S<1>::template U::type a2; diff --git a/gcc/testsuite/g++.dg/cpp1y/alias-decl2.C b/gcc/testsuite/g++.dg/cpp1y/alias-decl2.C new file mode 100644 index 00000000000..f42b425a30b --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/alias-decl2.C @@ -0,0 +1,8 @@ +// DR 1710 - Missing template keyword in class-or-decltype +// { dg-do compile { target c++14 } } + +template struct S { + template + using U = TT; +}; +template typename S::template U::type foo; diff --git a/gcc/testsuite/g++.dg/cpp1y/alias-decl3.C b/gcc/testsuite/g++.dg/cpp1y/alias-decl3.C new file mode 100644 index 00000000000..b950b20b5d8 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/alias-decl3.C @@ -0,0 +1,9 @@ +// DR 1710 - Missing template keyword in class-or-decltype +// { dg-do compile { target c++14 } } + +template struct S { + template struct A { }; + typedef A type; +}; +template typename S::template A foo; +template typename S::template type foo2; // { dg-error "expected template-id" } diff --git a/gcc/testsuite/g++.dg/parse/missing-template1.C b/gcc/testsuite/g++.dg/parse/missing-template1.C index fcc3aa0c3ff..19c1433d09f 100644 --- a/gcc/testsuite/g++.dg/parse/missing-template1.C +++ b/gcc/testsuite/g++.dg/parse/missing-template1.C @@ -12,9 +12,7 @@ template struct A template void foo() { - typedef typename A::B::X Y; // { dg-error "non-template" "non" } - // { dg-error "not declare" "decl" { target *-*-* } .-1 } - // { dg-message "note" "note" { target *-*-* } .-2 } + typedef typename A::B::X Y; } void bar() diff --git a/gcc/testsuite/g++.dg/parse/template3.C b/gcc/testsuite/g++.dg/parse/template3.C index c284a5ee040..8da8a48a3c3 100644 --- a/gcc/testsuite/g++.dg/parse/template3.C +++ b/gcc/testsuite/g++.dg/parse/template3.C @@ -13,7 +13,4 @@ struct X : Outer::template Inner {}; template -struct Y : Outer::Inner {}; // { dg-error "used as template" "temp" } -// { dg-error "expected" "exp" { target *-*-* } .-1 } -// { dg-message "note" "note" { target *-*-* } .-2 } - +struct Y : Outer::Inner {}; diff --git a/gcc/testsuite/g++.dg/template/dependent-name10.C b/gcc/testsuite/g++.dg/template/dependent-name10.C new file mode 100644 index 00000000000..18e024f7e6d --- /dev/null +++ b/gcc/testsuite/g++.dg/template/dependent-name10.C @@ -0,0 +1,18 @@ +// PR c++/94057 - template keyword in a typename-specifier. +// { dg-do compile { target c++11 } } + +template struct A { + template struct B { + typedef int TT; + typedef int TT2; + typedef int TT3; + typedef int TT4; + }; +}; + +struct X : A::B { + using A::template B::TT; + using typename A::template B::TT2; + using A::B::TT3; + using typename A::B::TT4; +}; diff --git a/gcc/testsuite/g++.dg/template/dependent-name11.C b/gcc/testsuite/g++.dg/template/dependent-name11.C new file mode 100644 index 00000000000..687a9bd5df5 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/dependent-name11.C @@ -0,0 +1,15 @@ +// PR c++/94057 - template keyword in a typename-specifier. +// { dg-do compile { target c++11 } } + +template struct A { + template + struct W { }; +}; + +void +g () +{ + // class-key nested-name-specifier template[opt] simple-template-id + struct A::W w; + struct A::template W w2; +} diff --git a/gcc/testsuite/g++.dg/template/dependent-name12.C b/gcc/testsuite/g++.dg/template/dependent-name12.C new file mode 100644 index 00000000000..7ee94e7457d --- /dev/null +++ b/gcc/testsuite/g++.dg/template/dependent-name12.C @@ -0,0 +1,7 @@ +// PR c++/94057 - template keyword in a typename-specifier. + +template struct A; +template struct B; +template struct B { + typename A::type::type t; +}; diff --git a/gcc/testsuite/g++.dg/template/dependent-name13.C b/gcc/testsuite/g++.dg/template/dependent-name13.C new file mode 100644 index 00000000000..1e971168657 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/dependent-name13.C @@ -0,0 +1,8 @@ +// DR 1710 - Missing template keyword in class-or-decltype + +template struct S { + void fn(typename T::template B::template C); + void fn2(typename T::B::template C); + void fn3(typename T::template B::C); + void fn4(typename T::B::C); +}; diff --git a/gcc/testsuite/g++.dg/template/dependent-name5.C b/gcc/testsuite/g++.dg/template/dependent-name5.C index fc78983324b..15c1acb0347 100644 --- a/gcc/testsuite/g++.dg/template/dependent-name5.C +++ b/gcc/testsuite/g++.dg/template/dependent-name5.C @@ -22,9 +22,7 @@ struct A typedef N type6; typedef A::N type7; -// { dg-error "" "" { target c++2a } .-1 } typedef A::N type8; -// { dg-error "" "" { target c++2a } .-1 } typedef A::template N type9; // { dg-error "" "" { target c++17_down } } typedef typename A::template N type10; diff --git a/gcc/testsuite/g++.dg/template/dependent-name7.C b/gcc/testsuite/g++.dg/template/dependent-name7.C new file mode 100644 index 00000000000..3dfa42d2df0 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/dependent-name7.C @@ -0,0 +1,9 @@ +// PR c++/94057 - template keyword in a typename-specifier. +// { dg-do compile } + +template struct A { + template struct B { + B(A::B&); + void fn(A::B); + }; +}; diff --git a/gcc/testsuite/g++.dg/template/dependent-name8.C b/gcc/testsuite/g++.dg/template/dependent-name8.C new file mode 100644 index 00000000000..ad9e44f9b85 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/dependent-name8.C @@ -0,0 +1,9 @@ +// PR c++/94057 - template keyword in a typename-specifier. +// { dg-do compile } + +template struct A { + template struct B { + B(typename A::B&); + void fn(typename A::B); + }; +}; diff --git a/gcc/testsuite/g++.dg/template/dependent-name9.C b/gcc/testsuite/g++.dg/template/dependent-name9.C new file mode 100644 index 00000000000..6dfdbc176c1 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/dependent-name9.C @@ -0,0 +1,9 @@ +// PR c++/94057 - template keyword in a typename-specifier. +// { dg-do compile } + +template struct A { + template struct B { + B(typename A::template B&); + void fn(typename A::template B); + }; +}; diff --git a/gcc/testsuite/g++.dg/template/dr1710-2.C b/gcc/testsuite/g++.dg/template/dr1710-2.C new file mode 100644 index 00000000000..99d49b746b2 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/dr1710-2.C @@ -0,0 +1,10 @@ +// DR 1710 - Missing template keyword in class-or-decltype +// { dg-do compile } + +template struct A { + template struct B { + }; +}; + +template struct D : A::B {}; +template struct D2 : A::template B {}; diff --git a/gcc/testsuite/g++.dg/template/dr1710.C b/gcc/testsuite/g++.dg/template/dr1710.C new file mode 100644 index 00000000000..c945977971f --- /dev/null +++ b/gcc/testsuite/g++.dg/template/dr1710.C @@ -0,0 +1,9 @@ +// DR 1710 - Missing template keyword in class-or-decltype +// { dg-do compile } + +template struct D : T::template B::template C {}; +template struct D2 : T::B::template C {}; +template struct D3 : T::template B::C {}; +template struct D4 : T::B::C {}; +template struct D5 : T::template B::type::type {}; +template struct D6 : T::B::type::type {}; diff --git a/gcc/testsuite/g++.dg/template/dr1794.C b/gcc/testsuite/g++.dg/template/dr1794.C new file mode 100644 index 00000000000..f629d7d0b98 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/dr1794.C @@ -0,0 +1,14 @@ +// DR 1794 - template keyword and alias templates. +// { dg-do compile { target c++11 } } + +template class Template> +struct Internal { + template + using Bind = Template; +}; + +template class Template, typename Arg> +using Instantiate = Template; + +template class Template, typename Argument> +using Bind = Instantiate::template Bind, Argument>; diff --git a/gcc/testsuite/g++.dg/template/dr314.C b/gcc/testsuite/g++.dg/template/dr314.C new file mode 100644 index 00000000000..7c0d8ac592f --- /dev/null +++ b/gcc/testsuite/g++.dg/template/dr314.C @@ -0,0 +1,15 @@ +// DR 314 - template in base class specifier. + +template +struct A { + template + struct B {}; +}; + +template +struct C : public A::template B { +}; + +template +struct C2 : public A::B { +}; diff --git a/gcc/testsuite/g++.dg/template/error4.C b/gcc/testsuite/g++.dg/template/error4.C index 9d76561aa02..a5030f06c98 100644 --- a/gcc/testsuite/g++.dg/template/error4.C +++ b/gcc/testsuite/g++.dg/template/error4.C @@ -5,5 +5,4 @@ template struct C1 }; template -void foo(typename C1::C2::Type *) { } // { dg-error "template" "error " } -// { dg-message "note" "note" { target *-*-* } .-1 } +void foo(typename C1::C2::Type *) { } diff --git a/gcc/testsuite/g++.dg/template/meminit2.C b/gcc/testsuite/g++.dg/template/meminit2.C index db6e0427fc7..6a56a806746 100644 --- a/gcc/testsuite/g++.dg/template/meminit2.C +++ b/gcc/testsuite/g++.dg/template/meminit2.C @@ -14,8 +14,6 @@ struct A : typename O::template I { // { dg-error "keyword 'typename' template struct B : O::template I { - B() : O::I() // { dg-error "used as template|it is a template" } + B() : O::I() {} }; - -// { dg-bogus "end of input" "bogus token skipping in the parser" { xfail *-*-* } 17 } diff --git a/gcc/testsuite/g++.old-deja/g++.pt/crash38.C b/gcc/testsuite/g++.old-deja/g++.pt/crash38.C index 5d386132436..60cd5e3f44e 100644 --- a/gcc/testsuite/g++.old-deja/g++.pt/crash38.C +++ b/gcc/testsuite/g++.old-deja/g++.pt/crash38.C @@ -3,10 +3,8 @@ template struct S { - typedef typename T::Y::Z X; // { dg-error "non-template" "non-template" } No Y in A -// { dg-message "note" "note" { target *-*-* } .-1 } -// { dg-error "does not declare" "not declare" { target *-*-* } .-2 } - X x; // { dg-error "does not name a type" } No Y in A + typedef typename T::Y::Z X; // { dg-error "not a class template" } No Y in A + X x; }; struct A {