On 3/11/21 3:52 AM, Jakub Jelinek wrote:
Hi!
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.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
ok. Maybe a comment about '// unwrap the convert from reference'?
2021-03-11 Jakub Jelinek <ja...@redhat.com>
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.
--- gcc/cp/call.c.jj 2021-03-06 10:17:01.687304578 +0100
+++ gcc/cp/call.c 2021-03-10 13:51:29.121445195 +0100
@@ -9504,6 +9504,8 @@ build_over_call (struct z_candidate *can
if (immediate_invocation_p (fndecl, nargs))
{
tree obj_arg = NULL_TREE;
+ 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))
@@ -9527,6 +9529,7 @@ build_over_call (struct z_candidate *can
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;
--- gcc/testsuite/g++.dg/cpp2a/consteval19.C.jj 2021-03-10 14:20:58.018835190
+0100
+++ gcc/testsuite/g++.dg/cpp2a/consteval19.C 2021-03-10 14:20:16.642294167
+0100
@@ -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 ()};
Jakub
--
Nathan Sidwell