From 77df40e8127ec4f62c01c15f2d6f76d995424863 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Tue, 10 Sep 2019 02:29:13 +0000 Subject: PR c++/84374 - diagnose invalid uses of decltype(auto). * decl.c (grokdeclarator): Diagnose wrong usage of decltype(auto) in a function declaration. * g++.dg/cpp1y/auto-fn57.C: New test. From-SVN: r275557 --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/decl.c | 10 ++++++++++ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/g++.dg/cpp1y/auto-fn57.C | 18 ++++++++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp1y/auto-fn57.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 48582ce..2b98a06 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2019-09-09 Marek Polacek + + PR c++/84374 - diagnose invalid uses of decltype(auto). + * decl.c (grokdeclarator): Diagnose wrong usage of decltype(auto) in + a function declaration. + 2019-09-06 Nathan Sidwell PR c++/91125 diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 88e2c3b..dfcd7b1 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -11681,6 +11681,16 @@ grokdeclarator (const cp_declarator *declarator, "allowed"); return error_mark_node; } + /* Only plain decltype(auto) is allowed. */ + if (tree a = type_uses_auto (type)) + { + if (AUTO_IS_DECLTYPE (a) && a != type) + { + error_at (typespec_loc, "%qT as type rather than " + "plain %", type); + return error_mark_node; + } + } if (ctype == NULL_TREE && decl_context == FIELD diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 3d4ada8..f738a23 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2019-09-09 Marek Polacek + + PR c++/84374 - diagnose invalid uses of decltype(auto). + * g++.dg/cpp1y/auto-fn57.C: New test. + 2019-09-09 Segher Boessenkool * gcc.target/powerpc/rlwinm-0.c: Adjust expected instruction counts. diff --git a/gcc/testsuite/g++.dg/cpp1y/auto-fn57.C b/gcc/testsuite/g++.dg/cpp1y/auto-fn57.C new file mode 100644 index 0000000..e58df18 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/auto-fn57.C @@ -0,0 +1,18 @@ +// PR c++/84374 - diagnose invalid uses of decltype(auto). +// { dg-do compile { target c++14 } } + +auto l = [](auto* r)->decltype(auto)* { return r; }; // { dg-error "as type rather than plain" } +auto m = [](auto* r)->decltype(auto)& { return *r; }; // { dg-error "as type rather than plain" } + +decltype(auto)* f(); // { dg-error "as type rather than plain" } +decltype(auto)& f2(); // { dg-error "as type rather than plain" } +decltype(auto)* f3() { return 42; } // { dg-error "as type rather than plain" } +decltype(auto)& f4() { return 42; } // { dg-error "as type rather than plain" } + + +class C { + decltype(auto)* g(); // { dg-error "as type rather than plain" } + decltype(auto)& g2(); // { dg-error "as type rather than plain" } + decltype(auto)* g3() { } // { dg-error "as type rather than plain" } + decltype(auto)& g4() { } // { dg-error "as type rather than plain" } +}; -- cgit v1.1