diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index e97c4e9d1ad2..7d1161fd1c11 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 053636536916..753b3148ab31 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 140f29633559..45b19d5061e0 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 000000000000..e5397e71bd6f --- /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 000000000000..f42b425a30bc --- /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 000000000000..b950b20b5d86 --- /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 fcc3aa0c3ff4..19c1433d09f8 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 c284a5ee040d..8da8a48a3c31 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 000000000000..18e024f7e6d8 --- /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 000000000000..687a9bd5df51 --- /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 000000000000..7ee94e7457d9 --- /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 000000000000..1e9711686572 --- /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 fc78983324b3..15c1acb03476 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 000000000000..3dfa42d2df01 --- /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 000000000000..ad9e44f9b850 --- /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 000000000000..6dfdbc176c1b --- /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 000000000000..99d49b746b29 --- /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 000000000000..c945977971f3 --- /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 000000000000..f629d7d0b983 --- /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 000000000000..7c0d8ac592f1 --- /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 9d76561aa021..a5030f06c989 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 db6e0427fc74..6a56a806746e 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 5d3861324365..60cd5e3f44e4 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 {