+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
}
}
+/* 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]
/* 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. */
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)
}
}
}
+ /* 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)
+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
--- /dev/null
+// 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;
--- /dev/null
+// 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;
--- /dev/null
+// 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" }
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()
{};
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> {};
--- /dev/null
+// 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;
+};
--- /dev/null
+// 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;
+}
--- /dev/null
+// 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;
+};
--- /dev/null
+// 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>);
+};
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;
--- /dev/null
+// 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>);
+ };
+};
--- /dev/null
+// 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>);
+ };
+};
--- /dev/null
+// 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>);
+ };
+};
--- /dev/null
+// 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> {};
--- /dev/null
+// 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 {};
--- /dev/null
+// 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>;
--- /dev/null
+// 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> {
+};
};
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 *) { }
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 }
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 {