c++: DR1710, template keyword in a typename-specifier [PR94057]

Consider

  template <typename T> class A {
    template <typename U> class B {
      void fn(typename A<T>::B<U>);
    };
  };

which is rejected with
error: 'typename A<T>::B' names 'template<class T> template<class U> class A<T>::B', which is not a type
whereas clang/icc/msvc accept it.

"typename A<T>::B<U>" 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<U> 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 <typename T> class A {
    template <typename U> class B {
      void fn(A<T>::B<U>);
    };
  };

Here we morph A<T>::B<U> 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.
This commit is contained in:
Marek Polacek 2020-03-18 19:28:14 -04:00
parent 6d85947d23
commit 517f5356bb
23 changed files with 257 additions and 31 deletions

View File

@ -1,3 +1,14 @@
2020-03-26 Marek Polacek <polacek@redhat.com>
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 <iain@sandoe.co.uk>
* coroutines.cc (coro_init_identifiers): Initialize an identifier

View File

@ -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)

View File

@ -1,3 +1,28 @@
2020-03-26 Marek Polacek <polacek@redhat.com>
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 <iain@sandoe.co.uk>
* g++.dg/coroutines/torture/co-ret-09-bool-await-susp.C: Amend

View File

@ -0,0 +1,9 @@
// DR 1710 - Missing template keyword in class-or-decltype
// { dg-do compile { target c++14 } }
template <int> struct S {
template <int> struct A;
template <int N> using U = typename A<N>::foo;
};
template <typename T> typename S<1>::U<T::foo>::type a;
template <typename T> typename S<1>::template U<T::foo>::type a2;

View File

@ -0,0 +1,8 @@
// DR 1710 - Missing template keyword in class-or-decltype
// { dg-do compile { target c++14 } }
template <typename T> struct S {
template <typename TT>
using U = TT;
};
template <typename T> typename S<int>::template U<T>::type foo;

View File

@ -0,0 +1,9 @@
// DR 1710 - Missing template keyword in class-or-decltype
// { dg-do compile { target c++14 } }
template <typename T> struct S {
template<typename TT> struct A { };
typedef A<int> type;
};
template <typename T> typename S<int>::template A<int> foo;
template <typename T> typename S<int>::template type foo2; // { dg-error "expected template-id" }

View File

@ -12,9 +12,7 @@ template <typename T> struct A
template <typename T> void foo()
{
typedef typename A<T>::B<T>::X Y; // { dg-error "non-template" "non" }
// { dg-error "not declare" "decl" { target *-*-* } .-1 }
// { dg-message "note" "note" { target *-*-* } .-2 }
typedef typename A<T>::B<T>::X Y;
}
void bar()

View File

@ -13,7 +13,4 @@ struct X : Outer<b>::template Inner<T>
{};
template <bool b, typename T>
struct Y : Outer<b>::Inner<T> {}; // { dg-error "used as template" "temp" }
// { dg-error "expected" "exp" { target *-*-* } .-1 }
// { dg-message "note" "note" { target *-*-* } .-2 }
struct Y : Outer<b>::Inner<T> {};

View File

@ -0,0 +1,18 @@
// PR c++/94057 - template keyword in a typename-specifier.
// { dg-do compile { target c++11 } }
template<typename T> struct A {
template<typename U> struct B {
typedef int TT;
typedef int TT2;
typedef int TT3;
typedef int TT4;
};
};
struct X : A<int>::B<int> {
using A<int>::template B<int>::TT;
using typename A<int>::template B<int>::TT2;
using A<int>::B<int>::TT3;
using typename A<int>::B<int>::TT4;
};

View File

@ -0,0 +1,15 @@
// PR c++/94057 - template keyword in a typename-specifier.
// { dg-do compile { target c++11 } }
template<typename T> struct A {
template<typename U>
struct W { };
};
void
g ()
{
// class-key nested-name-specifier template[opt] simple-template-id
struct A<int>::W<int> w;
struct A<int>::template W<int> w2;
}

View File

@ -0,0 +1,7 @@
// PR c++/94057 - template keyword in a typename-specifier.
template <bool> struct A;
template <typename, typename> struct B;
template <typename T, typename U, typename V> struct B<T U::*, V> {
typename A<V::x>::type::type t;
};

View File

@ -0,0 +1,8 @@
// DR 1710 - Missing template keyword in class-or-decltype
template<typename T> struct S {
void fn(typename T::template B<int>::template C<int>);
void fn2(typename T::B<int>::template C<int>);
void fn3(typename T::template B<int>::C<int>);
void fn4(typename T::B<int>::C<int>);
};

View File

@ -22,9 +22,7 @@ struct A
typedef N<int> type6;
typedef A::N<int> type7;
// { dg-error "" "" { target c++2a } .-1 }
typedef A<T>::N<int> type8;
// { dg-error "" "" { target c++2a } .-1 }
typedef A<T*>::template N<int> type9; // { dg-error "" "" { target c++17_down } }
typedef typename A<T*>::template N<int> type10;

View File

@ -0,0 +1,9 @@
// PR c++/94057 - template keyword in a typename-specifier.
// { dg-do compile }
template<typename T> struct A {
template<typename U> struct B {
B(A<T>::B<U>&);
void fn(A<T>::B<U>);
};
};

View File

@ -0,0 +1,9 @@
// PR c++/94057 - template keyword in a typename-specifier.
// { dg-do compile }
template<typename T> struct A {
template<typename U> struct B {
B(typename A<T>::B<U>&);
void fn(typename A<T>::B<U>);
};
};

View File

@ -0,0 +1,9 @@
// PR c++/94057 - template keyword in a typename-specifier.
// { dg-do compile }
template<typename T> struct A {
template<typename U> struct B {
B(typename A<T>::template B<U>&);
void fn(typename A<T>::template B<U>);
};
};

View File

@ -0,0 +1,10 @@
// DR 1710 - Missing template keyword in class-or-decltype
// { dg-do compile }
template<typename T> struct A {
template<typename U> struct B {
};
};
template<typename T> struct D : A<int>::B<int> {};
template<typename T> struct D2 : A<int>::template B<int> {};

View File

@ -0,0 +1,9 @@
// DR 1710 - Missing template keyword in class-or-decltype
// { dg-do compile }
template<typename T> struct D : T::template B<int>::template C<int> {};
template<typename T> struct D2 : T::B<int>::template C<int> {};
template<typename T> struct D3 : T::template B<int>::C<int> {};
template<typename T> struct D4 : T::B<int>::C<int> {};
template<typename T> struct D5 : T::template B<int>::type::type {};
template<typename T> struct D6 : T::B<int>::type::type {};

View File

@ -0,0 +1,14 @@
// DR 1794 - template keyword and alias templates.
// { dg-do compile { target c++11 } }
template<template<typename> class Template>
struct Internal {
template<typename Arg>
using Bind = Template<Arg>;
};
template<template<typename> class Template, typename Arg>
using Instantiate = Template<Arg>;
template<template<typename> class Template, typename Argument>
using Bind = Instantiate<Internal<Template>::template Bind, Argument>;

View File

@ -0,0 +1,15 @@
// DR 314 - template in base class specifier.
template <typename T>
struct A {
template <typename U>
struct B {};
};
template <typename T>
struct C : public A<T>::template B<T> {
};
template <typename T>
struct C2 : public A<int>::B<T> {
};

View File

@ -5,5 +5,4 @@ template<class T> struct C1
};
template<class T, class U>
void foo(typename C1<T>::C2<U>::Type *) { } // { dg-error "template" "error " }
// { dg-message "note" "note" { target *-*-* } .-1 }
void foo(typename C1<T>::C2<U>::Type *) { }

View File

@ -14,8 +14,6 @@ struct A : typename O<T>::template I<int> { // { dg-error "keyword 'typename'
template <typename T>
struct B : O<T>::template I<int> {
B() : O<T>::I<int>() // { dg-error "used as template|it is a template" }
B() : O<T>::I<int>()
{}
};
// { dg-bogus "end of input" "bogus token skipping in the parser" { xfail *-*-* } 17 }

View File

@ -3,10 +3,8 @@
template <class T>
struct S {
typedef typename T::Y<T>::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<T>::Z X; // { dg-error "not a class template" } No Y in A
X x;
};
struct A {