On 10/21/20 12:37 PM, Patrick Palka wrote:
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, formed by build_class_a. cp_fully_fold_init is able
to only partially fold this initializer, doing away with the COMPOUND_EXPR
to yield
<lambda(S)>::operator() (&cb, <<< Unknown tree: empty_class_expr >>>)
as the final initializer for 'ret'. This initializer no longer satifies
potential_constant_expression because this predicate returns false when
it sees a bare EMPTY_CLASS_EXPR that's 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 to me we could either make cp_fold preserve
the COMPOUND_EXPR trees produced by build_call_a, or we could
modify potential_constant_expression and friends to handle "bare"
EMPTY_CLASS_EXPR trees. Assuming it's safe to continue folding
away these COMPOUND_EXPRs, the second approach seems cleaner, so this
patch implements the second approach.
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?
gcc/cp/ChangeLog:
PR c++/96575
* constexpr.c (cxx_eval_constant_expression)
<case COMPOUND_EXPR>: Remove now-redundant handling of
COMPOUND_EXPR with EMPTY_CLASS_EXPR second operand.
I don't think this is entirely redundant; it still avoids building a new
CONSTRUCTOR when we don't need to, and controlling memory use in the
constexpr code has largely been about avoiding extra CONSTRUCTORs.
OK without this hunk.
<case EMPTY_CLASS_EXPR>: Lower it into 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.
---
gcc/cp/constexpr.c | 20 ++++++++------------
gcc/testsuite/g++.dg/cpp1z/constexpr-96575.C | 19 +++++++++++++++++++
2 files changed, 27 insertions(+), 12 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-96575.C
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index a118f8a810b..0c13ff4db71 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -6070,13 +6070,11 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx,
tree t,
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))
r = cxx_eval_constant_expression (ctx, op0,
lval, non_constant_p, overflow_p,
jump_target);
@@ -6403,9 +6401,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 +8184,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 +8317,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:
{
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-96575.C
b/gcc/testsuite/g++.dg/cpp1z/constexpr-96575.C
new file mode 100644
index 00000000000..3a2fc48d7db
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-96575.C
@@ -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);