diff options
author | Patrick Palka <ppalka@redhat.com> | 2020-10-22 07:33:58 -0400 |
---|---|---|
committer | Patrick Palka <ppalka@redhat.com> | 2020-10-22 07:33:58 -0400 |
commit | 46fdced6a9f936ae4d5b42347d7d87f69875683a (patch) | |
tree | 48a00a7d0c7c39ebd3de4dd68bf15f1465dfbe4d | |
parent | b083a14dfe1f53446722f488885078e419238d24 (diff) | |
download | gcc-46fdced6a9f936ae4d5b42347d7d87f69875683a.zip gcc-46fdced6a9f936ae4d5b42347d7d87f69875683a.tar.gz gcc-46fdced6a9f936ae4d5b42347d7d87f69875683a.tar.bz2 |
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.
-rw-r--r-- | gcc/cp/constexpr.c | 14 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp1z/constexpr-96575.C | 19 |
2 files changed, 25 insertions, 8 deletions
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index a118f8a..c566376 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 0000000..5820c18 --- /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); |