From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 379B23870899; Fri, 19 Mar 2021 23:30:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 379B23870899 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-9484] c++: Fix up calls to immediate functions returning reference [PR99507] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-10 X-Git-Oldrev: b0d1a533d62ff137e3d54ccf32cf876e5b49d2ab X-Git-Newrev: 3ef0d71f64408b527169a740ea7dc5f4d169b0b6 Message-Id: <20210319233030.379B23870899@sourceware.org> Date: Fri, 19 Mar 2021 23:30:30 +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: Fri, 19 Mar 2021 23:30:30 -0000 https://gcc.gnu.org/g:3ef0d71f64408b527169a740ea7dc5f4d169b0b6 commit r10-9484-g3ef0d71f64408b527169a740ea7dc5f4d169b0b6 Author: Jakub Jelinek Date: Fri Mar 12 10:11:24 2021 +0100 c++: Fix up calls to immediate functions returning reference [PR99507] build_cxx_call calls convert_from_reference at the end, so if an immediate function returns a reference, we were constant evaluating not just that call, but that call wrapped in an INDIRECT_REF. That unfortunately means it can constant evaluate to something non-addressable, so if code later needs to take its address it will fail. The following patch fixes that by undoing the convert_from_reference wrapping for the cxx_constant_value evaluation and readdding it ad the end. 2021-03-12 Jakub Jelinek PR c++/99507 * call.c (build_over_call): For immediate evaluation of functions that return references, undo convert_from_reference effects before calling cxx_constant_value and call convert_from_reference afterwards. * g++.dg/cpp2a/consteval19.C: New test. (cherry picked from commit 425afe1f0c907e6469cef1672160c9c95177e71a) Diff: --- gcc/cp/call.c | 4 ++++ gcc/testsuite/g++.dg/cpp2a/consteval19.C | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 33a95cbc958..aa8d84fa90c 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -9178,6 +9178,9 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) || !current_binding_level->immediate_fn_ctx_p)) { tree obj_arg = NULL_TREE; + /* Undo convert_from_reference called by build_cxx_call. */ + if (REFERENCE_REF_P (call)) + call = TREE_OPERAND (call, 0); if (DECL_CONSTRUCTOR_P (fndecl)) obj_arg = cand->first_arg ? cand->first_arg : (*args)[0]; if (obj_arg && is_dummy_object (obj_arg)) @@ -9201,6 +9204,7 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) call = cxx_constant_value (call, obj_arg); if (obj_arg && !error_operand_p (call)) call = build2 (INIT_EXPR, void_type_node, obj_arg, call); + call = convert_from_reference (call); } } return call; diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval19.C b/gcc/testsuite/g++.dg/cpp2a/consteval19.C new file mode 100644 index 00000000000..d742f59070f --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/consteval19.C @@ -0,0 +1,6 @@ +// PR c++/99507 +// { dg-do compile { target c++20 } } + +constexpr int i{0}; +consteval const int &iref () { return i; } +const int *a{&iref ()};