<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/101299>101299</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Clang][C] What is the type of a bit-field? (For generic selection / constexpr initialization)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
MitalAshok
</td>
</tr>
</table>
<pre>
Seen at https://github.com/llvm/llvm-project/pull/78112#pullrequestreview-2205422888, this compiles surprisingly in C23:
```c++
struct C {
signed long long i : 8;
};
constexpr struct C c = { 255 };
```
My reading of N3096:
§6.7.1p6:
> If an object or subobject declared with storage-class specifier `constexpr` has [...] integer [..] type, any explicit initializer value for it shall be [.,.] an integer constant expression
§6.6p4:
> Each constant expression shall evaluate to a constant that is in the range of representable values for its type.
§6.7.2.1p12:
> A bit-field is interpreted as having a signed or unsigned integer type consisting of the specified number of bits.
... So the member `i` has a signed type with 8 bits, which 255 is not representable in, and Clang should reject the initializer.
Two other places I can think of where the type of something is observed is `typeof` (which explicitly bans bit-fields), and `_Generic`. Clang currently allows:
```c++
struct C {
signed long long i : 8;
} c;
static_assert(_Generic(c.i, long long: 1));
```
which according to §6.7.2.1p12, it shouldn't? The bit-field type with width 8 should not be compatible with a 64 bit wide type. Unless the lvalue conversion done converts the bit-field type to long long, but that doesn't seem to be allowed by §6.3.2.1
CC @AaronBallman @Fznamznon
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VcGO2zYQ_Rr6MlhBGsmyfPBh1xsXOaSXpOgxoKixxIYmVQ5lx_n6gpTs3Wy2hx4KGJZIDWfem_dISmbdW6KdWD-J9fNKTmFwfvdJB2keeXDfVq3rrrvPRBZkgCGEkUX5KPAg8NDrMExtptxJ4MGY8-3xMHr3F6kg8DBOxgg8bJqiQIFlHHr6eyIOns6aLg-I-bpCbJpG4B7CoBmUO43aEANPfvSate3NFb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