c++: Don't allow type-constraint auto(x) [PR104752]
authorMarek Polacek <polacek@redhat.com>
Wed, 2 Mar 2022 17:12:33 +0000 (12:12 -0500)
committerMarek Polacek <polacek@redhat.com>
Thu, 10 Mar 2022 16:22:40 +0000 (11:22 -0500)
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
gcc/cp/typeck2.cc
gcc/testsuite/g++.dg/cpp23/auto-fncast12.C [new file with mode: 0644]

index 799ce94..2174006 100644 (file)
@@ -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%>");
index 39d03e4..a4c825f 100644 (file)
@@ -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 (file)
index 0000000..2856c28
--- /dev/null
@@ -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" }