<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/142566>142566</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Clang] Incorrect "Case value is not a constant expression"
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
kezyr
</td>
</tr>
</table>
<pre>
**Problem:**
Clang (both `clang++` and Clangd) incorrectly produces an error "Case value is not a constant expression" when attempting to use a static enumerator (e.g., `State::EnumType::A`) accessed through an instance of its containing struct (`state.A`) within a `switch` statement's `case` label. This should be a valid constant expression according to the C++ standard.
**Expected Behavior:**
The code should compile successfully, as `state.A` refers to the constant enumerator `State::EnumType::A` and is suitable for a `case` label. GCC compiles this code without issues.
**Reproducer Code:**
```c++
struct State {
enum EnumType { A, B };
EnumType value;
constexpr operator EnumType() const noexcept { return value; }
};
struct Foo {
State state;
void workaround() {
auto stateCopy = state;
switch (stateCopy) {
case stateCopy.A: // This compiles successfully with Clang.
case stateCopy.B:
break;
}
}
void shouldCompile() {
switch (state) {
case state.A: // Clang error: "Case value is not a constant expression clang(expr_not_cce)"
case state.B:
break;
}
}
};
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVV-P4jYQ_zTmZXSR1wkQHvIA7FH1rWr3_eTYA3E32JH_sEs_fTVOWEK77d1FkSDOeH5_ZjKWIZiTRWzYcseWzwuZYud884p_Xf2idfraMLFlYvubd22PZ1Zux2fGt_te2hMwUbcudsBWXNECEzu6Vxyk1ZBjNBMbMFY571HF_gqDdzopDCAtoPfOAxNiLwPCRfYJwQSwLoIE5WyI0kbA98FjCMZZJgS8dWhBxojnIRp7guggBQQJIcpoFKBNZ_Qy5sQ1FqeCiT0x_CPKiKSh3H616fxyHaanLVtxYimVwhBQQ-y8S6eOGJrMQSG4I5gYiFSUxhJwiD6pSCBsxQkci1umNxM7Y0ESbHgz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