From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id A7CB438346B3; Tue, 10 May 2022 08:22:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A7CB438346B3 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r10-10668] c++: Fix handling of temporaries with consteval ctors and non-trivial dtors [PR104055] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-10 X-Git-Oldrev: ac26cfeca99c488213eaf76d767ec4470ba3cee9 X-Git-Newrev: 1c3c885e1bba86f01917445928006d26062c5b15 Message-Id: <20220510082254.A7CB438346B3@sourceware.org> Date: Tue, 10 May 2022 08:22:54 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 May 2022 08:22:54 -0000 https://gcc.gnu.org/g:1c3c885e1bba86f01917445928006d26062c5b15 commit r10-10668-g1c3c885e1bba86f01917445928006d26062c5b15 Author: Jakub Jelinek Date: Wed Jan 19 00:42:18 2022 +0100 c++: Fix handling of temporaries with consteval ctors and non-trivial dtors [PR104055] The following testcase is miscompiled. We see the constructor is immediate, in build_over_call we trigger: if (obj_arg && is_dummy_object (obj_arg)) { call = build_cplus_new (DECL_CONTEXT (fndecl), call, complain); obj_arg = NULL_TREE; } which makes call a TARGET_EXPR with the dtor in TARGET_EXPR_CLEANUP, but then call cxx_constant_value on it. In cxx_eval_outermost_constant_expr it triggers the: else if (TREE_CODE (t) != CONSTRUCTOR) { r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup); TREE_CONSTANT (r) = true; } which wraps the CONSTRUCTOR r into a new TARGET_EXPR, but one without dtors (I think we need e.g. the TREE_CONSTANT for the callers), and finally build_over_call uses that. The following patch fixes that by using get_target_expr instead of get_target_expr_sfinae + TREE_CONSTANT (r) = true if t is a TARGET_EXPR with non-NULL TARGET_EXPR_CLEANUP. 2022-01-19 Jakub Jelinek PR c++/104055 * constexpr.c (cxx_eval_outermost_constant_expr): If t is a TARGET_EXPR with TARGET_EXPR_CLEANUP, use get_target_expr rather than get_target_expr_sfinae with tf_no_cleanup, and don't set TREE_CONSTANT. * g++.dg/cpp2a/consteval27.C: New test. (cherry picked from commit 1a5145f1e3adf8b2ba4ad416a5ddef59a1e34d48) Diff: --- gcc/cp/constexpr.c | 6 +++++- gcc/testsuite/g++.dg/cpp2a/consteval27.C | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 2efa0a1d8ec..cd8ef069dde 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -6973,7 +6973,11 @@ cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_INITIAL (t) == r) return t; - else if (TREE_CODE (t) != CONSTRUCTOR) + else if (TREE_CODE (t) == CONSTRUCTOR) + ; + else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t)) + r = get_target_expr (r); + else { r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup); TREE_CONSTANT (r) = true; diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval27.C b/gcc/testsuite/g++.dg/cpp2a/consteval27.C new file mode 100644 index 00000000000..72d56df9dc5 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/consteval27.C @@ -0,0 +1,18 @@ +// PR c++/104055 +// { dg-do run { target c++20 } } + +int g; + +struct A { + ~A () { if (a != 17 || b != 26) __builtin_abort (); g = 42; } + consteval A () : a (17), b (26) {} + int a, b; +}; + +int +main () +{ + A{}; + if (g != 42) + __builtin_abort (); +}