https://gcc.gnu.org/g:70cea067dd2b101edc6b3710678529eb8ba2eec2

commit r14-11177-g70cea067dd2b101edc6b3710678529eb8ba2eec2
Author: Patrick Palka <ppa...@redhat.com>
Date:   Thu Jan 9 10:50:12 2025 -0500

    c++: constexpr potentiality of CAST_EXPR [PR117925]
    
    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 <ja...@redhat.com>
    (cherry picked from commit 76d1061237b5cd57a274cd8bc8fe02a6f407baa9)

Diff:
---
 gcc/cp/constexpr.cc                             | 11 ++++++++---
 gcc/testsuite/g++.dg/template/non-dependent35.C |  8 ++++++++
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 6025b4c29437..79dd6a7423c0 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -10279,9 +10279,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
index 000000000000..7e3ba99b0235
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/non-dependent35.C
@@ -0,0 +1,8 @@
+// PR c++/117925
+
+typedef int(*FnPtr)();
+
+template<class T>
+void fnICE(void* fnPtr) {
+  ((FnPtr)fnPtr)();
+}

Reply via email to