Bug 111226 - constexpr doesn't detect change of union to empty member
Summary: constexpr doesn't detect change of union to empty member
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 14.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid, rejects-valid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2023-08-29 13:21 UTC by Nathaniel Shead
Modified: 2024-03-03 08:12 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-09-07 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nathaniel Shead 2023-08-29 13:21:26 UTC
While working on a patch for PR101631, I found that the following code is currently incorrectly handled by GCC: (https://godbolt.org/z/1YevacMK3)


struct Empty {};

union U {
  int x;
  Empty e;
};

constexpr int foo() {
  U u{ 10 };
  u.e = {};
  return u.x;  // incorrectly accepted, even pre-C++20
}
constexpr auto y = foo();

constexpr Empty bar() {
  U u{ 10 };
  u.e = {};
  return u.e;  // incorrectly errors, thinks active member is still 'x'
}
constexpr auto x = bar();


The cause seems to be that the zero-sized trivial assignment is removed in call.cc (since PR43075) and after constant folding is no longer in the tree that the constexpr handling machinery receives.
Comment 1 Patrick Palka 2023-09-07 19:01:17 UTC
Confirmed.