From: Patrick Palka Date: Thu, 9 Jan 2025 15:50:12 +0000 (-0500) Subject: c++: constexpr potentiality of CAST_EXPR [PR117925] X-Git-Tag: releases/gcc-14.3.0~622 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=70cea067dd2b101edc6b3710678529eb8ba2eec2;p=thirdparty%2Fgcc.git 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) : 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 (cherry picked from commit 76d1061237b5cd57a274cd8bc8fe02a6f407baa9) --- 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 +void fnICE(void* fnPtr) { + ((FnPtr)fnPtr)(); +}