From 46fdced6a9f936ae4d5b42347d7d87f69875683a Mon Sep 17 00:00:00 2001 From: Patrick Palka Date: Thu, 22 Oct 2020 07:33:58 -0400 Subject: [PATCH] c++: constexpr evaluation and bare EMPTY_CLASS_EXPR [PR96575] In the testcase below, folding of the initializer for 'ret' inside the instantiated f::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, prior to folding, is the CALL_EXPR ::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 ::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) : Lower it to a CONSTRUCTOR. (potential_constant_expression_1) : Remove now-redundant handling of COMPOUND_EXPR with EMPTY_CLASS_EXPR second operand. : Return true instead of false. gcc/testsuite/ChangeLog: PR c++/96575 * g++.dg/cpp1z/constexpr-96575.C: New test. --- gcc/cp/constexpr.c | 14 ++++++-------- gcc/testsuite/g++.dg/cpp1z/constexpr-96575.C | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 8 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..c566376e4ee 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -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: { 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..5820c18bec3 --- /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 +constexpr auto f (T cb) { + return [=] { + auto ret = cb({}); + return ret; + }(); +} + +constexpr auto x = f(g); -- 2.30.2