c++: constexpr evaluation and bare EMPTY_CLASS_EXPR [PR96575]
In the testcase below, folding of the initializer for 'ret' inside the instantiated f<lambda>::lambda ends up yielding an initializer for which potential_constant_expression returns false. This causes finish_function to mark the lambda as non-constexpr, which ultimately causes us to reject 'f(g)' as a call to a non-constexpr function. The initializer for 'ret' inside f<lambda>::lambda, prior to folding, is the CALL_EXPR <lambda(S)>::operator() (&cb, ({}, <<< Unknown tree: empty_class_expr >>>;)) where the second argument is a COMPOUND_EXPR whose second operand is an EMPTY_CLASS_EXPR that was formed by build_class_a. cp_fully_fold_init is able to only partially fold this initializer: it gets rid of the side-effectless COMPOUND_EXPR to obtain <lambda(S)>::operator() (&cb, <<< Unknown tree: empty_class_expr >>>) as the final initializer for 'ret'. This initializer no longer satifies potential_constant_expression due to the bare EMPTY_CLASS_EXPR which is not wrapped in a COMPOUND_EXPR. (cp_fully_fold_init first tries maybe_constant_value on the original CALL_EXPR, but constexpr evaluation punts upon seeing __builtin_is_constant_evaluated, since manifestly_const_eval is false.) To fix this, it seems we could either make cp_fold preserve the COMPOUND_EXPR trees produced by build_call_a, or we could improve the constexpr machinery to treat EMPTY_CLASS_EXPR trees as first-class citizens. Assuming it's safe to continue folding away these COMPOUND_EXPRs, the second approach seems cleaner, so this patch implements the second approach. gcc/cp/ChangeLog: PR c++/96575 * constexpr.c (cxx_eval_constant_expression) <case EMPTY_CLASS_EXPR>: Lower it to a CONSTRUCTOR. (potential_constant_expression_1) <case COMPOUND_EXPR>: Remove now-redundant handling of COMPOUND_EXPR with EMPTY_CLASS_EXPR second operand. <case EMPTY_CLASS_EXPR>: Return true instead of false. gcc/testsuite/ChangeLog: PR c++/96575 * g++.dg/cpp1z/constexpr-96575.C: New test.
This commit is contained in:
parent
b083a14dfe
commit
46fdced6a9
|
@ -6403,9 +6403,9 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
|
|||
break;
|
||||
|
||||
case EMPTY_CLASS_EXPR:
|
||||
/* This is good enough for a function argument that might not get
|
||||
used, and they can't do anything with it, so just return it. */
|
||||
return t;
|
||||
/* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
|
||||
it to an appropriate CONSTRUCTOR. */
|
||||
return build_constructor (TREE_TYPE (t), NULL);
|
||||
|
||||
case STATEMENT_LIST:
|
||||
new_ctx = *ctx;
|
||||
|
@ -8186,13 +8186,11 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
|
|||
case COMPOUND_EXPR:
|
||||
{
|
||||
/* check_return_expr sometimes wraps a TARGET_EXPR in a
|
||||
COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
|
||||
introduced by build_call_a. */
|
||||
COMPOUND_EXPR; don't get confused. */
|
||||
tree op0 = TREE_OPERAND (t, 0);
|
||||
tree op1 = TREE_OPERAND (t, 1);
|
||||
STRIP_NOPS (op1);
|
||||
if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
|
||||
|| TREE_CODE (op1) == EMPTY_CLASS_EXPR)
|
||||
if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
|
||||
return RECUR (op0, want_rval);
|
||||
else
|
||||
goto binary;
|
||||
|
@ -8321,7 +8319,7 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
|
|||
return true;
|
||||
|
||||
case EMPTY_CLASS_EXPR:
|
||||
return false;
|
||||
return true;
|
||||
|
||||
case GOTO_EXPR:
|
||||
{
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
// PR c++/96575
|
||||
// { dg-do compile { target c++17 } }
|
||||
|
||||
struct S { };
|
||||
|
||||
constexpr auto g = [] (S s) {
|
||||
if (__builtin_is_constant_evaluated())
|
||||
return s;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
constexpr auto f (T cb) {
|
||||
return [=] {
|
||||
auto ret = cb({});
|
||||
return ret;
|
||||
}();
|
||||
}
|
||||
|
||||
constexpr auto x = f(g);
|
Loading…
Reference in New Issue