]> gcc.gnu.org Git - gcc.git/commitdiff
c++: some missing-SFINAE fixes
authorPatrick Palka <ppalka@redhat.com>
Tue, 13 Sep 2022 13:48:04 +0000 (09:48 -0400)
committerPatrick Palka <ppalka@redhat.com>
Tue, 13 Sep 2022 13:48:04 +0000 (09:48 -0400)
It looks like we aren't respecting SFINAE for:

  * an invalid/non-constant conditional explicit-specifier
  * a non-constant conditional noexcept-specifier
  * a non-constant argument to __integer_pack

This patch fixes these in the usual way, by passing complain and
propagating error_mark_node appropriately.

gcc/cp/ChangeLog:

* decl.cc (build_explicit_specifier): Pass complain to
cxx_constant_value.
* except.cc (build_noexcept_spec): Likewise.
* pt.cc (expand_integer_pack): Likewise.
(tsubst_function_decl): Propagate error_mark_node returned
from build_explicit_specifier.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1z/noexcept-type26.C: New test.
* g++.dg/cpp2a/explicit19.C: New test.
* g++.dg/ext/integer-pack6.C: New test.

gcc/cp/decl.cc
gcc/cp/except.cc
gcc/cp/pt.cc
gcc/testsuite/g++.dg/cpp1z/noexcept-type26.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/explicit19.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/integer-pack6.C [new file with mode: 0644]

index 936f1cf0197aa315f50ecdf32f5d66d253539b0e..5404d7e084cc3f83dbdb1a1b13246d503a7cac70 100644 (file)
@@ -18557,7 +18557,7 @@ build_explicit_specifier (tree expr, tsubst_flags_t complain)
 
   expr = build_converted_constant_bool_expr (expr, complain);
   expr = instantiate_non_dependent_expr (expr, complain);
-  expr = cxx_constant_value (expr);
+  expr = cxx_constant_value (expr, NULL_TREE, complain);
   return expr;
 }
 
index 7fdbc747c228d071c96d7d48d2b83f34d5ca6368..4d7f0ce102da03603be88c5afd638fb346297022 100644 (file)
@@ -1257,7 +1257,7 @@ build_noexcept_spec (tree expr, tsubst_flags_t complain)
     {
       expr = build_converted_constant_bool_expr (expr, complain);
       expr = instantiate_non_dependent_expr (expr, complain);
-      expr = cxx_constant_value (expr);
+      expr = cxx_constant_value (expr, NULL_TREE, complain);
     }
   if (TREE_CODE (expr) == INTEGER_CST)
     {
index 1c6f4b84612b8f5597b3730ecf9625b07a751794..074179288b6079c1bd3eae20b47e108cb1e5da6a 100644 (file)
@@ -3869,7 +3869,7 @@ expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
   else
     {
       hi = instantiate_non_dependent_expr (hi, complain);
-      hi = cxx_constant_value (hi);
+      hi = cxx_constant_value (hi, NULL_TREE, complain);
       int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
 
       /* Calculate the largest value of len that won't make the size of the vec
@@ -14312,6 +14312,8 @@ tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
                                    /*function_p=*/false,
                                    /*i_c_e_p=*/true);
       spec = build_explicit_specifier (spec, complain);
+      if (spec == error_mark_node)
+       return error_mark_node;
       if (instantiation_dependent_expression_p (spec))
        store_explicit_specifier (r, spec);
       else
diff --git a/gcc/testsuite/g++.dg/cpp1z/noexcept-type26.C b/gcc/testsuite/g++.dg/cpp1z/noexcept-type26.C
new file mode 100644 (file)
index 0000000..491df4d
--- /dev/null
@@ -0,0 +1,12 @@
+// Verify a non-constant conditional noexcept-specifier in a function type
+// respects SFINAE.
+// { dg-do compile { target c++17 } }
+
+template<class T> void f(void() noexcept(T::value)) = delete;
+template<class T> void f(...);
+
+struct B { static bool value; };
+
+int main() {
+  f<B>(nullptr);
+}
diff --git a/gcc/testsuite/g++.dg/cpp2a/explicit19.C b/gcc/testsuite/g++.dg/cpp2a/explicit19.C
new file mode 100644 (file)
index 0000000..4790381
--- /dev/null
@@ -0,0 +1,12 @@
+// Verify a conditional explicit-specifier is a SFINAE context.
+// { dg-do compile { target c++20 } }
+
+struct A {
+  template<class T> explicit(T::value) A(T) = delete;
+  A(...);
+};
+
+struct B { static bool value; };
+
+A x(0);
+A y(B{});
diff --git a/gcc/testsuite/g++.dg/ext/integer-pack6.C b/gcc/testsuite/g++.dg/ext/integer-pack6.C
new file mode 100644 (file)
index 0000000..dc43116
--- /dev/null
@@ -0,0 +1,13 @@
+// Verify a non-constant argument to __integer_pack respects SFINAE.
+// { dg-do compile { target c++11 } }
+
+template<int...> struct A { };
+
+template<class T> auto f(int) -> A<__integer_pack(T::value)...> = delete;
+template<class T> void f(...);
+
+struct B { static int value; };
+
+int main() {
+  f<B>(0);
+}
This page took 0.127523 seconds and 5 git commands to generate.