* [PATCH] c++: Don't allow type-constraint auto(x) [PR104752] @ 2022-03-02 18:31 Marek Polacek 2022-03-08 19:47 ` Jason Merrill 0 siblings, 1 reply; 4+ messages in thread From: Marek Polacek @ 2022-03-02 18:31 UTC (permalink / raw) To: GCC Patches, Jason Merrill 104752 points out that template<class T> concept C = true; auto y = C auto(1); is ill-formed as per [dcl.type.auto.deduct]: "For an explicit type conversion, T is the specified type, which shall be auto." which doesn't allow type-constraint auto. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/104752 gcc/cp/ChangeLog: * semantics.cc (finish_compound_literal): Disallow auto{x} for is_constrained_auto. * typeck2.cc (build_functional_cast_1): Likewise. gcc/testsuite/ChangeLog: * g++.dg/cpp23/auto-fncast12.C: New test. --- gcc/cp/semantics.cc | 1 + gcc/cp/typeck2.cc | 1 + gcc/testsuite/g++.dg/cpp23/auto-fncast12.C | 8 ++++++++ 3 files changed, 10 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp23/auto-fncast12.C diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index a2c0eb050e6..5129b12f00f 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -3148,6 +3148,7 @@ finish_compound_literal (tree type, tree compound_literal, /* C++23 auto{x}. */ else if (is_auto (type) && !AUTO_IS_DECLTYPE (type) + && !is_constrained_auto (type) && CONSTRUCTOR_NELTS (compound_literal) == 1) { if (cxx_dialect < cxx23) diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc index 39d03e4c3c4..c9314bbeb6f 100644 --- a/gcc/cp/typeck2.cc +++ b/gcc/cp/typeck2.cc @@ -2305,6 +2305,7 @@ build_functional_cast_1 (location_t loc, tree exp, tree parms, init = parms; /* C++23 auto(x). */ else if (!AUTO_IS_DECLTYPE (anode) + && !is_constrained_auto (anode) && list_length (parms) == 1) { init = TREE_VALUE (parms); diff --git a/gcc/testsuite/g++.dg/cpp23/auto-fncast12.C b/gcc/testsuite/g++.dg/cpp23/auto-fncast12.C new file mode 100644 index 00000000000..f513f7c9325 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/auto-fncast12.C @@ -0,0 +1,8 @@ +// PR c++/104752 +// { dg-do compile { target c++23 } } + +template<class T> +concept C = true; +auto x = auto(1); // valid (P0849R8) +auto y = C auto(1); // { dg-error "invalid use" } +auto z = C auto{1}; // { dg-error "invalid use" } base-commit: 8977f4bec650bb6975792772245b07b722ee9843 -- 2.35.1 ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] c++: Don't allow type-constraint auto(x) [PR104752] 2022-03-02 18:31 [PATCH] c++: Don't allow type-constraint auto(x) [PR104752] Marek Polacek @ 2022-03-08 19:47 ` Jason Merrill 2022-03-09 15:22 ` [PATCH v2] " Marek Polacek 0 siblings, 1 reply; 4+ messages in thread From: Jason Merrill @ 2022-03-08 19:47 UTC (permalink / raw) To: Marek Polacek, GCC Patches On 3/2/22 14:31, Marek Polacek wrote: > 104752 points out that > > template<class T> > concept C = true; > auto y = C auto(1); > > is ill-formed as per [dcl.type.auto.deduct]: "For an explicit type conversion, > T is the specified type, which shall be auto." which doesn't allow > type-constraint auto. > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > PR c++/104752 > > gcc/cp/ChangeLog: > > * semantics.cc (finish_compound_literal): Disallow auto{x} for > is_constrained_auto. > * typeck2.cc (build_functional_cast_1): Likewise. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp23/auto-fncast12.C: New test. > --- > gcc/cp/semantics.cc | 1 + > gcc/cp/typeck2.cc | 1 + > gcc/testsuite/g++.dg/cpp23/auto-fncast12.C | 8 ++++++++ > 3 files changed, 10 insertions(+) > create mode 100644 gcc/testsuite/g++.dg/cpp23/auto-fncast12.C > > diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc > index a2c0eb050e6..5129b12f00f 100644 > --- a/gcc/cp/semantics.cc > +++ b/gcc/cp/semantics.cc > @@ -3148,6 +3148,7 @@ finish_compound_literal (tree type, tree compound_literal, > /* C++23 auto{x}. */ > else if (is_auto (type) > && !AUTO_IS_DECLTYPE (type) > + && !is_constrained_auto (type) > && CONSTRUCTOR_NELTS (compound_literal) == 1) > { > if (cxx_dialect < cxx23) > diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc > index 39d03e4c3c4..c9314bbeb6f 100644 > --- a/gcc/cp/typeck2.cc > +++ b/gcc/cp/typeck2.cc > @@ -2305,6 +2305,7 @@ build_functional_cast_1 (location_t loc, tree exp, tree parms, > init = parms; > /* C++23 auto(x). */ > else if (!AUTO_IS_DECLTYPE (anode) > + && !is_constrained_auto (anode) > && list_length (parms) == 1) > { > init = TREE_VALUE (parms); We might get a better diagnostic by moving this test inside the block and saying that the auto can't be constrained, instead of "invalid use of auto"? > diff --git a/gcc/testsuite/g++.dg/cpp23/auto-fncast12.C b/gcc/testsuite/g++.dg/cpp23/auto-fncast12.C > new file mode 100644 > index 00000000000..f513f7c9325 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp23/auto-fncast12.C > @@ -0,0 +1,8 @@ > +// PR c++/104752 > +// { dg-do compile { target c++23 } } > + > +template<class T> > +concept C = true; > +auto x = auto(1); // valid (P0849R8) > +auto y = C auto(1); // { dg-error "invalid use" } > +auto z = C auto{1}; // { dg-error "invalid use" } > > base-commit: 8977f4bec650bb6975792772245b07b722ee9843 ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2] c++: Don't allow type-constraint auto(x) [PR104752] 2022-03-08 19:47 ` Jason Merrill @ 2022-03-09 15:22 ` Marek Polacek 2022-03-10 4:19 ` Jason Merrill 0 siblings, 1 reply; 4+ messages in thread From: Marek Polacek @ 2022-03-09 15:22 UTC (permalink / raw) To: Jason Merrill; +Cc: GCC Patches On Tue, Mar 08, 2022 at 02:47:42PM -0500, Jason Merrill wrote: > On 3/2/22 14:31, Marek Polacek wrote: > > 104752 points out that > > > > template<class T> > > concept C = true; > > auto y = C auto(1); > > > > is ill-formed as per [dcl.type.auto.deduct]: "For an explicit type conversion, > > T is the specified type, which shall be auto." which doesn't allow > > type-constraint auto. > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > PR c++/104752 > > > > gcc/cp/ChangeLog: > > > > * semantics.cc (finish_compound_literal): Disallow auto{x} for > > is_constrained_auto. > > * typeck2.cc (build_functional_cast_1): Likewise. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/cpp23/auto-fncast12.C: New test. > > --- > > gcc/cp/semantics.cc | 1 + > > gcc/cp/typeck2.cc | 1 + > > gcc/testsuite/g++.dg/cpp23/auto-fncast12.C | 8 ++++++++ > > 3 files changed, 10 insertions(+) > > create mode 100644 gcc/testsuite/g++.dg/cpp23/auto-fncast12.C > > > > diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc > > index a2c0eb050e6..5129b12f00f 100644 > > --- a/gcc/cp/semantics.cc > > +++ b/gcc/cp/semantics.cc > > @@ -3148,6 +3148,7 @@ finish_compound_literal (tree type, tree compound_literal, > > /* C++23 auto{x}. */ > > else if (is_auto (type) > > && !AUTO_IS_DECLTYPE (type) > > + && !is_constrained_auto (type) > > && CONSTRUCTOR_NELTS (compound_literal) == 1) > > { > > if (cxx_dialect < cxx23) > > diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc > > index 39d03e4c3c4..c9314bbeb6f 100644 > > --- a/gcc/cp/typeck2.cc > > +++ b/gcc/cp/typeck2.cc > > @@ -2305,6 +2305,7 @@ build_functional_cast_1 (location_t loc, tree exp, tree parms, > > init = parms; > > /* C++23 auto(x). */ > > else if (!AUTO_IS_DECLTYPE (anode) > > + && !is_constrained_auto (anode) > > && list_length (parms) == 1) > > { > > init = TREE_VALUE (parms); > > We might get a better diagnostic by moving this test inside the block and > saying that the auto can't be constrained, instead of "invalid use of auto"? Sure. So like this? Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- 104752 points out that template<class T> concept C = true; auto y = C auto(1); is ill-formed as per [dcl.type.auto.deduct]: "For an explicit type conversion, T is the specified type, which shall be auto." which doesn't allow type-constraint auto. PR c++/104752 gcc/cp/ChangeLog: * semantics.cc (finish_compound_literal): Disallow auto{x} for is_constrained_auto. * typeck2.cc (build_functional_cast_1): Likewise. gcc/testsuite/ChangeLog: * g++.dg/cpp23/auto-fncast12.C: New test. --- gcc/cp/semantics.cc | 8 +++++++- gcc/cp/typeck2.cc | 8 +++++++- gcc/testsuite/g++.dg/cpp23/auto-fncast12.C | 8 ++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp23/auto-fncast12.C diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 799ce943279..21740064d3d 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -3150,7 +3150,13 @@ finish_compound_literal (tree type, tree compound_literal, && !AUTO_IS_DECLTYPE (type) && CONSTRUCTOR_NELTS (compound_literal) == 1) { - if (cxx_dialect < cxx23) + if (is_constrained_auto (type)) + { + if (complain & tf_error) + error ("%<auto{x}%> cannot be constrained"); + return error_mark_node; + } + else if (cxx_dialect < cxx23) pedwarn (input_location, OPT_Wc__23_extensions, "%<auto{x}%> only available with " "%<-std=c++2b%> or %<-std=gnu++2b%>"); diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc index 39d03e4c3c4..a4c825fc34d 100644 --- a/gcc/cp/typeck2.cc +++ b/gcc/cp/typeck2.cc @@ -2308,7 +2308,13 @@ build_functional_cast_1 (location_t loc, tree exp, tree parms, && list_length (parms) == 1) { init = TREE_VALUE (parms); - if (cxx_dialect < cxx23) + if (is_constrained_auto (anode)) + { + if (complain & tf_error) + error_at (loc, "%<auto(x)%> cannot be constrained"); + return error_mark_node; + } + else if (cxx_dialect < cxx23) pedwarn (loc, OPT_Wc__23_extensions, "%<auto(x)%> only available with " "%<-std=c++2b%> or %<-std=gnu++2b%>"); diff --git a/gcc/testsuite/g++.dg/cpp23/auto-fncast12.C b/gcc/testsuite/g++.dg/cpp23/auto-fncast12.C new file mode 100644 index 00000000000..2856c2846fc --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/auto-fncast12.C @@ -0,0 +1,8 @@ +// PR c++/104752 +// { dg-do compile { target c++23 } } + +template<class T> +concept C = true; +auto x = auto(1); // valid (P0849R8) +auto y = C auto(1); // { dg-error "cannot be constrained" } +auto z = C auto{1}; // { dg-error "cannot be constrained" } base-commit: bded0d549fdfdc1328b2c0189dc5f8593b4cbe42 -- 2.35.1 ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] c++: Don't allow type-constraint auto(x) [PR104752] 2022-03-09 15:22 ` [PATCH v2] " Marek Polacek @ 2022-03-10 4:19 ` Jason Merrill 0 siblings, 0 replies; 4+ messages in thread From: Jason Merrill @ 2022-03-10 4:19 UTC (permalink / raw) To: Marek Polacek; +Cc: GCC Patches On 3/9/22 11:22, Marek Polacek wrote: > On Tue, Mar 08, 2022 at 02:47:42PM -0500, Jason Merrill wrote: >> On 3/2/22 14:31, Marek Polacek wrote: >>> 104752 points out that >>> >>> template<class T> >>> concept C = true; >>> auto y = C auto(1); >>> >>> is ill-formed as per [dcl.type.auto.deduct]: "For an explicit type conversion, >>> T is the specified type, which shall be auto." which doesn't allow >>> type-constraint auto. >>> >>> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? >>> >>> PR c++/104752 >>> >>> gcc/cp/ChangeLog: >>> >>> * semantics.cc (finish_compound_literal): Disallow auto{x} for >>> is_constrained_auto. >>> * typeck2.cc (build_functional_cast_1): Likewise. >>> >>> gcc/testsuite/ChangeLog: >>> >>> * g++.dg/cpp23/auto-fncast12.C: New test. >>> --- >>> gcc/cp/semantics.cc | 1 + >>> gcc/cp/typeck2.cc | 1 + >>> gcc/testsuite/g++.dg/cpp23/auto-fncast12.C | 8 ++++++++ >>> 3 files changed, 10 insertions(+) >>> create mode 100644 gcc/testsuite/g++.dg/cpp23/auto-fncast12.C >>> >>> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc >>> index a2c0eb050e6..5129b12f00f 100644 >>> --- a/gcc/cp/semantics.cc >>> +++ b/gcc/cp/semantics.cc >>> @@ -3148,6 +3148,7 @@ finish_compound_literal (tree type, tree compound_literal, >>> /* C++23 auto{x}. */ >>> else if (is_auto (type) >>> && !AUTO_IS_DECLTYPE (type) >>> + && !is_constrained_auto (type) >>> && CONSTRUCTOR_NELTS (compound_literal) == 1) >>> { >>> if (cxx_dialect < cxx23) >>> diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc >>> index 39d03e4c3c4..c9314bbeb6f 100644 >>> --- a/gcc/cp/typeck2.cc >>> +++ b/gcc/cp/typeck2.cc >>> @@ -2305,6 +2305,7 @@ build_functional_cast_1 (location_t loc, tree exp, tree parms, >>> init = parms; >>> /* C++23 auto(x). */ >>> else if (!AUTO_IS_DECLTYPE (anode) >>> + && !is_constrained_auto (anode) >>> && list_length (parms) == 1) >>> { >>> init = TREE_VALUE (parms); >> >> We might get a better diagnostic by moving this test inside the block and >> saying that the auto can't be constrained, instead of "invalid use of auto"? > > Sure. So like this? > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? OK. > -- >8 -- > 104752 points out that > > template<class T> > concept C = true; > auto y = C auto(1); > > is ill-formed as per [dcl.type.auto.deduct]: "For an explicit type conversion, > T is the specified type, which shall be auto." which doesn't allow > type-constraint auto. > > PR c++/104752 > > gcc/cp/ChangeLog: > > * semantics.cc (finish_compound_literal): Disallow auto{x} for > is_constrained_auto. > * typeck2.cc (build_functional_cast_1): Likewise. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp23/auto-fncast12.C: New test. > --- > gcc/cp/semantics.cc | 8 +++++++- > gcc/cp/typeck2.cc | 8 +++++++- > gcc/testsuite/g++.dg/cpp23/auto-fncast12.C | 8 ++++++++ > 3 files changed, 22 insertions(+), 2 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp23/auto-fncast12.C > > diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc > index 799ce943279..21740064d3d 100644 > --- a/gcc/cp/semantics.cc > +++ b/gcc/cp/semantics.cc > @@ -3150,7 +3150,13 @@ finish_compound_literal (tree type, tree compound_literal, > && !AUTO_IS_DECLTYPE (type) > && CONSTRUCTOR_NELTS (compound_literal) == 1) > { > - if (cxx_dialect < cxx23) > + if (is_constrained_auto (type)) > + { > + if (complain & tf_error) > + error ("%<auto{x}%> cannot be constrained"); > + return error_mark_node; > + } > + else if (cxx_dialect < cxx23) > pedwarn (input_location, OPT_Wc__23_extensions, > "%<auto{x}%> only available with " > "%<-std=c++2b%> or %<-std=gnu++2b%>"); > diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc > index 39d03e4c3c4..a4c825fc34d 100644 > --- a/gcc/cp/typeck2.cc > +++ b/gcc/cp/typeck2.cc > @@ -2308,7 +2308,13 @@ build_functional_cast_1 (location_t loc, tree exp, tree parms, > && list_length (parms) == 1) > { > init = TREE_VALUE (parms); > - if (cxx_dialect < cxx23) > + if (is_constrained_auto (anode)) > + { > + if (complain & tf_error) > + error_at (loc, "%<auto(x)%> cannot be constrained"); > + return error_mark_node; > + } > + else if (cxx_dialect < cxx23) > pedwarn (loc, OPT_Wc__23_extensions, > "%<auto(x)%> only available with " > "%<-std=c++2b%> or %<-std=gnu++2b%>"); > diff --git a/gcc/testsuite/g++.dg/cpp23/auto-fncast12.C b/gcc/testsuite/g++.dg/cpp23/auto-fncast12.C > new file mode 100644 > index 00000000000..2856c2846fc > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp23/auto-fncast12.C > @@ -0,0 +1,8 @@ > +// PR c++/104752 > +// { dg-do compile { target c++23 } } > + > +template<class T> > +concept C = true; > +auto x = auto(1); // valid (P0849R8) > +auto y = C auto(1); // { dg-error "cannot be constrained" } > +auto z = C auto{1}; // { dg-error "cannot be constrained" } > > base-commit: bded0d549fdfdc1328b2c0189dc5f8593b4cbe42 ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-03-10 4:19 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2022-03-02 18:31 [PATCH] c++: Don't allow type-constraint auto(x) [PR104752] Marek Polacek 2022-03-08 19:47 ` Jason Merrill 2022-03-09 15:22 ` [PATCH v2] " Marek Polacek 2022-03-10 4:19 ` Jason Merrill
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).