<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/150833>150833</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang] Incorrect -fstrict-enums optimization of enumeration without fixed underlying type in the presense of common initial sequence rule.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          keinflue
      </td>
    </tr>
</table>

<pre>
    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.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJx8VMuO6zYM_RplQ8RQ5DiZLLLIy8BFF92064Fs0bFaWXIlKtPcry9oTzKPogUCB9IhDw8fok7JXj3iXlRHUZ0XOlMf4v5PtL5zGRdNMPd9HSJQj9AF58Kb9Vc4CXUU6ghjDNeoBzg57a9gfRtixJbcHcZoPSUQGynFRoL1iVAbCB1frfjqzVLPh-WvCpZdomhbWqLPQ2JUqJeECD3RmER5EKoWqr4G0wRHRYhXoeqfQtW_aGy37frSCLVjMzn9NnL-tePIR1Va37psUJQnGxJF1IMoL98huo_4SlFbSu-oPLAeuIDYHuEiQ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