]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Don't support auto{x} and auto(x) for C++ < 23 in SFINAE contexts [PR120685]
authorJakub Jelinek <jakub@redhat.com>
Tue, 17 Feb 2026 07:59:06 +0000 (08:59 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Tue, 17 Feb 2026 07:59:06 +0000 (08:59 +0100)
The following testcase ICEs in C++ 20 and older, because during
diagnostics dump_template_bindings attempts to tsubst with tf_none
something and we try to emit a pedwarn during that.
While the pedwarn could be just guarded with if (complain & tf_warning),
I thought we shouldn't support extensions for tf_none and should
instead return error_mark_node.

The following patch does that.

2026-02-17  Jakub Jelinek  <jakub@redhat.com>

PR c++/120685
* typeck2.cc (build_functional_cast_1): For C++23 auto(x)
without tf_warning or tf_error return error_mark_node instead of
emitting pedwarn and handling it.
* semantics.cc (finish_compound_literal): Similarly for C++26
auto{x}.

* g++.dg/cpp23/auto-fncast19.C: New test.

gcc/cp/semantics.cc
gcc/cp/typeck2.cc
gcc/testsuite/g++.dg/cpp23/auto-fncast19.C [new file with mode: 0644]

index 236bc625c25d204083971d36770b63eceb65ee46..ba5c8fa1b55233b4b0db92ec6b343a4978f6f74d 100644 (file)
@@ -3874,9 +3874,13 @@ finish_compound_literal (tree type, tree compound_literal,
          return error_mark_node;
        }
       else if (cxx_dialect < cxx23)
-       pedwarn (input_location, OPT_Wc__23_extensions,
-                "%<auto{x}%> only available with "
-                "%<-std=c++23%> or %<-std=gnu++23%>");
+       {
+         if ((complain & tf_warning_or_error) == 0)
+           return error_mark_node;
+         pedwarn (input_location, OPT_Wc__23_extensions,
+                  "%<auto{x}%> only available with "
+                  "%<-std=c++23%> or %<-std=gnu++23%>");
+       }
       type = do_auto_deduction (type, compound_literal, type, complain,
                                adc_variable_type);
       if (type == error_mark_node)
index a5567e7de180bce7ce5b4a12c9065b0a5890e3eb..664dd0990bf8981a5c868c6ccb533209199d6660 100644 (file)
@@ -2610,9 +2610,13 @@ build_functional_cast_1 (location_t loc, tree exp, tree parms,
              return error_mark_node;
            }
          else if (cxx_dialect < cxx23)
-           pedwarn (loc, OPT_Wc__23_extensions,
-                    "%<auto(x)%> only available with "
-                    "%<-std=c++23%> or %<-std=gnu++23%>");
+           {
+             if ((complain & tf_warning_or_error) == 0)
+               return error_mark_node;
+             pedwarn (loc, OPT_Wc__23_extensions,
+                      "%<auto(x)%> only available with "
+                      "%<-std=c++23%> or %<-std=gnu++23%>");
+           }
        }
       else
        {
diff --git a/gcc/testsuite/g++.dg/cpp23/auto-fncast19.C b/gcc/testsuite/g++.dg/cpp23/auto-fncast19.C
new file mode 100644 (file)
index 0000000..f14e814
--- /dev/null
@@ -0,0 +1,12 @@
+// PR c++/120685
+// { dg-do compile { target c++11 } }
+// { dg-options "" }
+
+template <typename T>
+void foo (decltype (auto (T ())) x) {} // { dg-warning "'auto\\\(x\\\)' only available with" "" { target c++20_down } }
+
+int
+main ()
+{
+  foo <int> (1);                       // { dg-error "no matching function for call to 'foo<int>\\\(int\\\)'" "" { target c++20_down } }
+}