Issue |
150833
|
Summary |
[clang] Incorrect -fstrict-enums optimization of enumeration without fixed underlying type in the presense of common initial sequence rule.
|
Labels |
clang
|
Assignees |
|
Reporter |
keinflue
|
For the following C++ program Clang incorrectly prints `0` instead of `1` with `-O2 -fstrict-enums` (see https://godbolt.org/z/Kaec7c4Eb):
```cpp
#include<iostream>
#include<type_traits>
enum E { E0 } e;
enum F { F0, F1, F2, F3 };
static_assert(std::is_same_v<std::underlying_type_t<E>, std::underlying_type_t<F>>);
struct A { E e; };
struct B { F f; };
union U {
A a;
B b;
} u;
bool test() {
return u.a.e == 2;
}
auto ptest = test;
int main() {
u.b.f = F2;
std::cout << ptest();
}
```
Because the two enumerations have the same underlying type, they are layout-compatible and therefore it is permitted to use the inactive `u.a.e` to read the active `u.b.f` corresponding to it in the common initial sequence. The read should happen as if `u.b.f` was nominated, meaning it should read the value `2` even if `E` is not able to represent that value.
I am not sure whether this is intentional or should be considered a defect in the standard, though.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs