[PATCH v2] c++: Check invalid use of constrained auto with trailing return type [PR100589]
Add check for constrained auto type specifier in function declaration or function type declaration with trailing return type. Issue error if such usage is detected. Test file renamed, and added a new test for type declaration. Successfully bootstrapped and regretested on x86_64-pc-linux-gnu: Added 6 passed and 4 unsupported tests. PR c++/100589 gcc/cp/ChangeLog: * decl.cc (grokdeclarator): Issue an error for a declarator with constrained auto type specifier and trailing return types. Include function names if available. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-pr100589.C: New test. Signed-off-by: Da Xie --- gcc/cp/decl.cc | 13 + gcc/testsuite/g++.dg/cpp2a/concepts-pr100589.C | 9 + 2 files changed, 22 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-pr100589.C diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 9ca8c6c4481..337ee65752e 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -14037,6 +14037,19 @@ grokdeclarator (const cp_declarator *declarator, "invalid use of %"); return error_mark_node; } + else if (is_constrained_auto (type)) + { + if (funcdecl_p) + error_at (typespec_loc, + "%qs function with trailing return type " + "has constrained % type specifier " + "rather than plain %", + name); + else + error_at (typespec_loc, + "invalid use of constrained % type"); + return error_mark_node; + } tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node); if (!tmpl) if (tree late_auto = type_uses_auto (late_return_type)) diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-pr100589.C b/gcc/testsuite/g++.dg/cpp2a/concepts-pr100589.C new file mode 100644 index 000..0c60d31f29b --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-pr100589.C @@ -0,0 +1,9 @@ +// PR c++/100589 +// { dg-do compile { target c++20 } } + +template +concept false_concept = false; + +false_concept auto f() -> int; // { dg-error "'f' .* trailing return type.* constrained 'auto'" } + +using type = false_concept auto() -> int; // { dg-error "invalid use of constrained 'auto' type"} -- 2.34.1
Re: Re: [PATCH v2] c++: Check invalid use of constrained auto with trailing return type [PR100589]
* Patrick Palka [2025-03-05 11:15:06]: > On Tue, 4 Mar 2025, Patrick Palka wrote: > > > On Sun, 2 Mar 2025, Da Xie wrote: > > > > > Add check for constrained auto type specifier in function declaration or > > > function type declaration with trailing return type. Issue error if such > > > usage is detected. > > > > > > Test file renamed, and added a new test for type declaration. > > > > > > Successfully bootstrapped and regretested on x86_64-pc-linux-gnu: > > > Added 6 passed and 4 unsupported tests. > > > > > > PR c++/100589 > > > > > > gcc/cp/ChangeLog: > > > > > > * decl.cc (grokdeclarator): Issue an error for a declarator with > > > constrained auto type specifier and trailing return types. Include > > > function names if available. > > > > > > gcc/testsuite/ChangeLog: > > > > > > * g++.dg/cpp2a/concepts-pr100589.C: New test. > > > > LGTM, thanks! Jason, shall I push this to trunk? > > Pushed as r15-7834-g7439febd94368f. > Many thanks! I'm a GCC newbie, and this reviewing process has taught me a lot. > > > > > > > > Signed-off-by: Da Xie > > > --- > > > gcc/cp/decl.cc | 13 + > > > gcc/testsuite/g++.dg/cpp2a/concepts-pr100589.C | 9 + > > > 2 files changed, 22 insertions(+) > > > create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-pr100589.C > > > > > > diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc > > > index 9ca8c6c4481..337ee65752e 100644 > > > --- a/gcc/cp/decl.cc > > > +++ b/gcc/cp/decl.cc > > > @@ -14037,6 +14037,19 @@ grokdeclarator (const cp_declarator *declarator, > > > "invalid use of %"); > > > return error_mark_node; > > > } > > > + else if (is_constrained_auto (type)) > > > + { > > > + if (funcdecl_p) > > > + error_at (typespec_loc, > > > + "%qs function with trailing return type " > > > + "has constrained % type specifier " > > > + "rather than plain %", > > > + name); > > > + else > > > + error_at (typespec_loc, > > > + "invalid use of constrained % type"); > > > + return error_mark_node; > > > + } > > > tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node); > > > if (!tmpl) > > > if (tree late_auto = type_uses_auto (late_return_type)) > > > diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-pr100589.C > > > b/gcc/testsuite/g++.dg/cpp2a/concepts-pr100589.C > > > new file mode 100644 > > > index 000..0c60d31f29b > > > --- /dev/null > > > +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-pr100589.C > > > @@ -0,0 +1,9 @@ > > > +// PR c++/100589 > > > +// { dg-do compile { target c++20 } } > > > + > > > +template > > > +concept false_concept = false; > > > + > > > +false_concept auto f() -> int; // { dg-error "'f' .* trailing return > > > type.* constrained 'auto'" } > > > > I can replace the use of .* with \[^\r\n\]* before pushing. > > > > > + > > > +using type = false_concept auto() -> int; // { dg-error "invalid use of > > > constrained 'auto' type"} > > N.B. I added a missing space between the " and } that caused this > dircetive to get ignored. > Sorry for the misses. I fixed and checked it locally, and the test passed. > > > -- > > > 2.34.1 > > > > > > > >
[PATCH] c++: Check invalid use of constrained auto with trailing return type [PR100589]
This bug comes from a missing check when a function declaration has a late-specified return type and a constrained auto type specifier. By adding such a check, we can catch such uses and diagnose them as errors. Successfully bootstrapped and regretested on x86_64-pc-linux-gnu: adds 6 new test results and 4 UNSUPPORTED results to g++.sum. The 4 UNSUPPORTED results are due to the fact that concepts are not supported before c++20, so tests cannot pass with c++17 and lower standards. PR c++/100589 gcc/cp/ChangeLog: * decl.cc (grokdeclarator): Issue an error for a declarator with constrained auto type specifier and trailing return types. gcc/testsuite/ChangeLog: * g++.dg/other/pr100589.C: New test. Signed-off-by: Da Xie --- gcc/cp/decl.cc| 7 +++ gcc/testsuite/g++.dg/other/pr100589.C | 7 +++ 2 files changed, 14 insertions(+) create mode 100644 gcc/testsuite/g++.dg/other/pr100589.C diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 9ca8c6c4481..f283ed996a2 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -14037,6 +14037,13 @@ grokdeclarator (const cp_declarator *declarator, "invalid use of %"); return error_mark_node; } + else if (is_constrained_auto (type)) + { + error_at (typespec_loc, "invalid use of " + "constrained auto type specifier " + "in function with trailing return type"); + return error_mark_node; + } tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node); if (!tmpl) if (tree late_auto = type_uses_auto (late_return_type)) diff --git a/gcc/testsuite/g++.dg/other/pr100589.C b/gcc/testsuite/g++.dg/other/pr100589.C new file mode 100644 index 000..3fa054ed34d --- /dev/null +++ b/gcc/testsuite/g++.dg/other/pr100589.C @@ -0,0 +1,7 @@ +// PR c++/100589 +// { dg-do compile { target c++20 } } + +template +concept false_concept = false; + +false_concept auto f() -> int; // { dg-error "constrained auto type specifier.* trailing return type" } \ No newline at end of file -- 2.34.1