From: Marek Polacek Date: Mon, 26 Oct 2020 21:35:56 +0000 (-0400) Subject: c++: Implement CWG 625: Use of auto as template-arg [PR97479] X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=11dc4c4e137a9b48d585d7b1b146fb8404e3e1fe;p=gcc.git 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. --- diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index d65b408a6f5..bd8c241dd82 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; diff --git a/gcc/testsuite/g++.dg/DRs/dr625.C b/gcc/testsuite/g++.dg/DRs/dr625.C new file mode 100644 index 00000000000..ce30a9258e6 --- /dev/null +++ b/gcc/testsuite/g++.dg/DRs/dr625.C @@ -0,0 +1,15 @@ +// DR 625 - Use of auto as a template-argument +// PR c++/97479 +// { dg-do compile { target c++14 } } + +template +struct A { }; + +void f(int); + +int main() +{ + A x = A(); // { dg-error "not permitted|invalid|cannot convert" } + A a = A(); // { dg-error "not permitted|invalid|cannot convert" } + void (*p)(auto); // { dg-error "parameter" } +} diff --git a/gcc/testsuite/g++.dg/cpp0x/auto3.C b/gcc/testsuite/g++.dg/cpp0x/auto3.C index 709898db39d..2cd0520023d 100644 --- a/gcc/testsuite/g++.dg/cpp0x/auto3.C +++ b/gcc/testsuite/g++.dg/cpp0x/auto3.C @@ -17,7 +17,7 @@ struct A { }; A A1; // CWG issue 625 -A A2 = A1; // { dg-error "" "" { target { ! concepts } } } +A A2 = A1; // { dg-error "" } auto foo() { } // { dg-error "auto" "" { target { ! c++14 } } } diff --git a/gcc/testsuite/g++.dg/cpp0x/auto9.C b/gcc/testsuite/g++.dg/cpp0x/auto9.C index a3f9be521d6..0e80c30ef74 100644 --- a/gcc/testsuite/g++.dg/cpp0x/auto9.C +++ b/gcc/testsuite/g++.dg/cpp0x/auto9.C @@ -114,7 +114,7 @@ badthrow2 () throw (auto &) // { dg-error "invalid use of|expected" } template struct G {}; // { dg-error "11:parameter" "" { target { ! c++17 } } } template struct H { H (); ~H (); }; -H h; // { dg-error "invalid|initializer" } +H h; // { dg-error "invalid|initializer|not permitted in template argument" } void qq (auto); // { dg-error "auto" "" { target { ! concepts } } } void qr (auto*); // { dg-error "auto" "" { target { ! concepts } } } diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-pr84979-2.C b/gcc/testsuite/g++.dg/cpp2a/concepts-pr84979-2.C index ce69a0f8ac5..290aaf83819 100644 --- a/gcc/testsuite/g++.dg/cpp2a/concepts-pr84979-2.C +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-pr84979-2.C @@ -4,9 +4,9 @@ template void foo1(T& t) { typename T::template C tcv = t; - typename T::template C u = tcv; - T::template C::f (tcv, u); // { dg-error "incomplete" } - (typename T::template D (t)); // { dg-error "invalid" } + typename T::template C u = tcv; // { dg-error "not permitted" "" { target c++20 } } + T::template C::f (tcv, u); // { dg-error "incomplete|not permitted" } + (typename T::template D (t)); // { dg-error "invalid|not permitted" } } struct T1 { @@ -22,9 +22,9 @@ struct T1 { template void foo2(T& t) { typename T::template C tcv = t; - typename T::template C u = tcv; - T::template C::f (tcv, u); // { dg-error "incomplete" } - T::template D (t); // { dg-error "invalid" } + typename T::template C u = tcv; // { dg-error "not permitted" "" { target c++20 } } + T::template C::f (tcv, u); // { dg-error "incomplete|not permitted" } + T::template D (t); // { dg-error "invalid|not permitted" } } struct T2 { diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-pr84979-3.C b/gcc/testsuite/g++.dg/cpp2a/concepts-pr84979-3.C index 3809c2d3033..d612327b9ae 100644 --- a/gcc/testsuite/g++.dg/cpp2a/concepts-pr84979-3.C +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-pr84979-3.C @@ -8,9 +8,9 @@ template void foo1(T& t) { typename T::template C tcv = t; - typename T::template C u = tcv; - T::template C::f (tcv, u); // { dg-error "incomplete" } - (typename T::template D (t)); // { dg-error "invalid" } + typename T::template C u = tcv; // { dg-error "not permitted" "" { target c++20 } } + T::template C::f (tcv, u); // { dg-error "incomplete|not permitted" } + (typename T::template D (t)); // { dg-error "invalid|not permitted" } } struct T1 { @@ -26,9 +26,9 @@ struct T1 { template void foo2(T& t) { typename T::template C tcv = t; - typename T::template C u = tcv; - T::template C::f (tcv, u); // { dg-error "incomplete" } - T::template D (t); // { dg-error "yields a type" } + typename T::template C u = tcv; // { dg-error "not permitted" "" { target c++20 } } + T::template C::f (tcv, u); // { dg-error "incomplete|not permitted" } + T::template D (t); // { dg-error "yields a type|not permitted" } } struct T2 { diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-pr84979.C b/gcc/testsuite/g++.dg/cpp2a/concepts-pr84979.C index 9bd40df103a..81555eb4554 100644 --- a/gcc/testsuite/g++.dg/cpp2a/concepts-pr84979.C +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-pr84979.C @@ -5,5 +5,5 @@ template void foo() {} void bar() { - foo(); // { dg-error "invalid" } + foo(); // { dg-error "not permitted|invalid|no matching function" } }