c++: Fix constexpr access to union member through pointer-to-member [PR98122]
authorJakub Jelinek <jakub@redhat.com>
Sat, 5 Dec 2020 00:30:08 +0000 (01:30 +0100)
committerJakub Jelinek <jakub@redhat.com>
Sat, 5 Dec 2020 00:30:08 +0000 (01:30 +0100)
commit43e84ce7d62be121445e17cc0ee009a81fb285d7
tree4c30da1fb62d47cc0934531732b7335b6eb9b3ce
parentc5fd8a9157c440c2039e7c7015f012800d1e524d
c++: Fix constexpr access to union member through pointer-to-member [PR98122]

We currently incorrectly reject the first testcase, because
cxx_fold_indirect_ref_1 doesn't attempt to handle UNION_TYPEs.
As the second testcase shows, it isn't that easy, because I believe we need
to take into account the active member and prefer that active member over
other members, because if we pick a non-active one, we might reject valid
programs.

2020-12-05  Jakub Jelinek  <jakub@redhat.com>

PR c++/98122
* constexpr.c (cxx_union_active_member): New function.
(cxx_fold_indirect_ref_1): Add ctx argument, pass it through to
recursive call.  Handle UNION_TYPE.
(cxx_fold_indirect_ref): Add ctx argument, pass it to recursive calls
and cxx_fold_indirect_ref_1.
(cxx_eval_indirect_ref): Adjust cxx_fold_indirect_ref calls.

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