From: Jason Merrill Date: Wed, 28 Jun 2017 19:41:43 +0000 (-0400) Subject: PR c++/45976 - error with ::template in declarator. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=36f48ebd016d4ace605a864c7f7303dbc281748d;p=gcc.git PR c++/45976 - error with ::template in declarator. * pt.c (resolve_typename_type): Fix TEMPLATE_ID_EXPR handling. From-SVN: r249753 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 3b3bb6d51fe..7b7070adf9c 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,8 @@ 2017-06-28 Jason Merrill + PR c++/45976 - error with ::template in declarator. + * pt.c (resolve_typename_type): Fix TEMPLATE_ID_EXPR handling. + PR c++/54769 - wrong lookup of dependent template-name. * parser.c (cp_parser_template_name): Handle dependent object type. (cp_parser_nested_name_specifier_opt): Make template_keyword_p a diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 957d22922d5..b060a190946 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -24624,26 +24624,38 @@ resolve_typename_type (tree type, bool only_current_p) /* For a TYPENAME_TYPE like "typename X::template Y", we want to find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */ + tree fullname = TYPENAME_TYPE_FULLNAME (type); if (!decl) /*nop*/; - else if (identifier_p (TYPENAME_TYPE_FULLNAME (type)) + else if (identifier_p (fullname) && TREE_CODE (decl) == TYPE_DECL) { result = TREE_TYPE (decl); if (result == error_mark_node) result = NULL_TREE; } - else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR + else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR && DECL_CLASS_TEMPLATE_P (decl)) { - tree tmpl; - tree args; /* Obtain the template and the arguments. */ - tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0); - args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1); + tree tmpl = TREE_OPERAND (fullname, 0); + if (TREE_CODE (tmpl) == IDENTIFIER_NODE) + { + /* We get here with a plain identifier because a previous tentative + parse of the nested-name-specifier as part of a ptr-operator saw + ::template X. The use of ::template is necessary in a + ptr-operator, but wrong in a declarator-id. + + [temp.names]: In a qualified-id of a declarator-id, the keyword + template shall not appear at the top level. */ + pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic, + "keyword % not allowed in declarator-id"); + tmpl = decl; + } + tree args = TREE_OPERAND (fullname, 1); /* Instantiate the template. */ result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE, - /*entering_scope=*/0, + /*entering_scope=*/true, tf_error | tf_user); if (result == error_mark_node) result = NULL_TREE; diff --git a/gcc/testsuite/g++.dg/template/template-keyword1.C b/gcc/testsuite/g++.dg/template/template-keyword1.C new file mode 100644 index 00000000000..0f928a8523e --- /dev/null +++ b/gcc/testsuite/g++.dg/template/template-keyword1.C @@ -0,0 +1,15 @@ +// PR c++/45976 + +template +struct A { + static const int value; + + template + struct B { + static const int value; + }; +}; + +template +template +const int A::template B::value = 0; // { dg-error "keyword .template" }