From 11dc4c4e137a9b48d585d7b1b146fb8404e3e1fe Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Mon, 26 Oct 2020 17:35:56 -0400 Subject: c++: Implement CWG 625: Use of auto as template-arg [PR97479] This patch implements CWG 625 which prohibits using auto in a template argument. A few tests used this construction. Since this usage was allowed by the Concepts TS, we only give an error in C++20. gcc/cp/ChangeLog: DR 625 PR c++/97479 * parser.c (cp_parser_type_id_1): Reject using auto as a template-argument in C++20. gcc/testsuite/ChangeLog: DR 625 PR c++/97479 * g++.dg/cpp0x/auto3.C: Update dg-error. * g++.dg/cpp0x/auto9.C: Likewise. * g++.dg/cpp2a/concepts-pr84979-2.C: Likewise. * g++.dg/cpp2a/concepts-pr84979-3.C: Likewise. * g++.dg/cpp2a/concepts-pr84979.C: Likewise. * g++.dg/DRs/dr625.C: New test. --- gcc/cp/parser.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'gcc/cp/parser.c') diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index d65b408..bd8c241 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -22419,9 +22419,20 @@ cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags, if (!cp_parser_parse_definitely (parser)) abstract_declarator = NULL; + bool auto_typeid_ok = false; + /* The concepts TS allows 'auto' as a type-id. */ + if (flag_concepts_ts) + auto_typeid_ok = !parser->in_type_id_in_expr_p; + /* DR 625 prohibits use of auto as a template-argument. We allow 'auto' + outside the template-argument-list context here only for the sake of + diagnostic: grokdeclarator then can emit a better error message for + e.g. using T = auto. */ + else if (flag_concepts) + auto_typeid_ok = (!parser->in_type_id_in_expr_p + && !parser->in_template_argument_list_p); + if (type_specifier_seq.type - /* The concepts TS allows 'auto' as a type-id. */ - && (!flag_concepts || parser->in_type_id_in_expr_p) + && !auto_typeid_ok /* None of the valid uses of 'auto' in C++14 involve the type-id nonterminal, but it is valid in a trailing-return-type. */ && !(cxx_dialect >= cxx14 && is_trailing_return)) @@ -22448,6 +22459,9 @@ cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags, inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl); } + else if (parser->in_template_argument_list_p) + error_at (loc, "%qT not permitted in template argument", + auto_node); else error_at (loc, "invalid use of %qT", auto_node); return error_mark_node; -- cgit v1.1