c++: Fix constexpr dtors vs invisible ref [PR97388]
authorJakub Jelinek <jakub@redhat.com>
Tue, 20 Oct 2020 07:33:20 +0000 (09:33 +0200)
committerJason Merrill <jason@redhat.com>
Thu, 29 Oct 2020 17:09:18 +0000 (13:09 -0400)
commit5afd90c5f36bf45291ca09ef3791f4a574e90d5d
tree4481aae044449d924fd7b2224343aa43db8cff51
parent2ca6de338d405d8ebe4d8b80526c383540209007
c++: Fix constexpr dtors vs invisible ref [PR97388]

For arguments passed by invisible reference, in the IL until genericization
we have the source types on the callee side and while on the caller side
we already pass references to the actual argument slot in the caller, we
undo that in cxx_bind_parameters_in_call's
      if (TREE_ADDRESSABLE (type))
        /* Undo convert_for_arg_passing work here.  */
        x = convert_from_reference (x);
This works fine most of the time, except when the type also has constexpr
destructor; in that case the destructor is invoked in the caller and thus
the unsharing we do to make sure that the callee doesn't modify caller's
values is in that case undesirable, it prevents the changes done in the
callee propagating to the caller which should see them for the constexpr
dtor evaluation.

The following patch fixes that.  While it could be perhaps done for all
TREE_ADDRESSABLE types, I don't see the need to change the behavior
if there is no constexpr non-trivial dtor.

Jason: And we need to avoid memoizing the call, because a later equivalent
call also needs to modify its argument.  And we don't need to unshare
constructors when we aren't memoizing the call, because we already unshared
them when evaluating the TARGET_EXPR representing the copy-initialization of
the argument.

2020-10-20  Jakub Jelinek  <jakub@redhat.com>
    Jason Merrill  <jason@redhat.com>

PR c++/97388
* constexpr.c (cxx_bind_parameters_in_call): Set non_constant_args
if the parameter type has a non-trivial destructor.
(cxx_eval_call_expression): Only unshare arguments if we're
memoizing this evaluation.

* g++.dg/cpp2a/constexpr-dtor5.C: New test.
* g++.dg/cpp2a/constexpr-dtor6.C: New test.
* g++.dg/cpp2a/constexpr-dtor7.C: New test.
gcc/cp/constexpr.c
gcc/testsuite/g++.dg/cpp2a/constexpr-dtor5.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dtor6.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dtor7.C [new file with mode: 0644]