c++: Fix complex constexpr virtual cases [PR93310].
authorJason Merrill <jason@redhat.com>
Thu, 4 Jun 2020 03:50:50 +0000 (23:50 -0400)
committerJason Merrill <jason@redhat.com>
Thu, 4 Jun 2020 19:11:42 +0000 (15:11 -0400)
commit7ece3bd8088983289731450826c238eb2bdd2db5
treed48b471b9169fe130e1faea914228b58853128c1
parent0ddb93ce77374004c49cdfbd748ba35867620cf1
c++: Fix complex constexpr virtual cases [PR93310].

The code in constexpr for looking up the actual type of the object and then
getting the virtual function from there broke for both of these tests: for
16, it assumed incorrectly that the DECL_VINDEX would apply to the most
derived type's vtable; for 17, it failed to consider that during
construction the base subobject is treated as being of the base type.

Fixed by just doing constant evaluation of the expression that looks up the
function in the vtable.  This means that a virtual call will involve loading
the vptr, so we will reject some calls through non-constexpr variables that
we previously accepted, but this seems appropriate to me.  None of our
testcases were affected.

gcc/cp/ChangeLog:

PR c++/93310
* constexpr.c (cxx_eval_constant_expression) [OBJ_TYPE_REF]:
Evaluate OBJ_TYPE_REF_EXPR.

gcc/testsuite/ChangeLog:

PR c++/93310
* g++.dg/cpp2a/constexpr-virtual16.C: New test.
* g++.dg/cpp2a/constexpr-virtual17.C: New test.
* g++.dg/cpp2a/constexpr-new12.C: Adjust diagnostic.
gcc/cp/constexpr.c
gcc/testsuite/g++.dg/cpp2a/constexpr-new12.C
gcc/testsuite/g++.dg/cpp2a/constexpr-virtual16.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-virtual17.C [new file with mode: 0644]