]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: constexpr potentiality of CAST_EXPR [PR117925]
authorPatrick Palka <ppalka@redhat.com>
Thu, 9 Jan 2025 15:50:12 +0000 (10:50 -0500)
committerPatrick Palka <ppalka@redhat.com>
Thu, 9 Jan 2025 15:50:12 +0000 (10:50 -0500)
We're incorrectly treating the templated callee (FnPtr)fnPtr, represented
as CAST_EXPR with TREE_LIST operand, as potentially constant here due to
neglecting to look through the TREE_LIST in the CAST_EXPR case of p_c_e_1.

PR c++/117925

gcc/cp/ChangeLog:

* constexpr.cc (potential_constant_expression_1) <case CAST_EXPR>:
Fix check for class conversion to literal type to properly look
through the TREE_LIST operand of a CAST_EXPR.

gcc/testsuite/ChangeLog:

* g++.dg/template/non-dependent35.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/template/non-dependent35.C [new file with mode: 0644]

index 7b02b51f756a7f81721e59cb80b51cf2b1ca2617..1345bc124efbc7901d77a96745e4dc34e625da3b 100644 (file)
@@ -10369,9 +10369,14 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
               && (dependent_type_p (TREE_TYPE (t))
                   || !COMPLETE_TYPE_P (TREE_TYPE (t))
                   || literal_type_p (TREE_TYPE (t)))
-              && TREE_OPERAND (t, 0))
-       {
-         tree type = TREE_TYPE (TREE_OPERAND (t, 0));
+              && TREE_OPERAND (t, 0)
+              && (TREE_CODE (t) != CAST_EXPR
+                  || !TREE_CHAIN (TREE_OPERAND (t, 0))))
+       {
+         tree from = TREE_OPERAND (t, 0);
+         if (TREE_CODE (t) == CAST_EXPR)
+           from = TREE_VALUE (from);
+         tree type = TREE_TYPE (from);
          /* If this is a dependent type, it could end up being a class
             with conversions.  */
          if (type == NULL_TREE || WILDCARD_TYPE_P (type))
diff --git a/gcc/testsuite/g++.dg/template/non-dependent35.C b/gcc/testsuite/g++.dg/template/non-dependent35.C
new file mode 100644 (file)
index 0000000..7e3ba99
--- /dev/null
@@ -0,0 +1,8 @@
+// PR c++/117925
+
+typedef int(*FnPtr)();
+
+template<class T>
+void fnICE(void* fnPtr) {
+  ((FnPtr)fnPtr)();
+}