]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: fix wrong-code with constexpr prvalue opt [PR118396]
authorMarek Polacek <polacek@redhat.com>
Thu, 16 Jan 2025 16:22:59 +0000 (11:22 -0500)
committerMarek Polacek <polacek@redhat.com>
Tue, 21 Jan 2025 14:57:46 +0000 (09:57 -0500)
commitf3f02493dfa8858c3fb2bc0da0d6d7320921408a
tree08284ee5e5b3b0753c3387f4708192a112bcacdc
parentda75309c635c54a6010b146514d456d2a4c6ab33
c++: fix wrong-code with constexpr prvalue opt [PR118396]

The recent r15-6369 unfortunately caused a bad wrong-code issue.
Here we have

  TARGET_EXPR <D.2996, (void) (D.2996 = {.status=0, .data={._vptr.Foo=&_ZTV3Foo + 16}})>

and call cp_fold_r -> maybe_constant_init with object=D.2996.  In
cxx_eval_outermost_constant_expr we now take the type of the object
if present.  An object can't have type 'void' and so we continue to
evaluate the initializer.  That evaluates into a VOID_CST, meaning
we disregard the whole initializer, and terrible things ensue.

For non-simple TARGET_EXPRs, we should return ctx.ctor rather than
the result of cxx_eval_constant_expression.

PR c++/118396
PR c++/118523

gcc/cp/ChangeLog:

* constexpr.cc (cxx_eval_outermost_constant_expr): For non-simple
TARGET_EXPRs, return ctx.ctor rather than the result of
cxx_eval_constant_expression.  If TYPE and the type of R don't
match, return the original expression.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/constexpr-prvalue4.C: New test.
* g++.dg/cpp1y/constexpr-prvalue3.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp0x/constexpr-prvalue4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/constexpr-prvalue3.C [new file with mode: 0644]