-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Description
Clang promotes to the incorrect type when applying integer promotions to bit-fields of bit-precise type, for example:
#include<stdio.h>
int main(void){
puts(_Generic(
+(struct{unsigned _BitInt(7)x:2;}){}.x,
unsigned _BitInt(2):"A",unsigned _BitInt(7):"B",int:"C"
));
}
This program should output "B", but clang outputs "C".
The value from a bit-field of a bit-precise integer type is converted to the corresponding bit-precise
integer type. If the original type is not a bit-precise integer type (6.2.5): if an int can represent all
values of the original type (as restricted by the width, for a bit-field), the value is converted to an
int; otherwise, it is converted to an unsigned int. These are called the integer promotions. All
other types are unchanged by the integer promotions(footnote) E.g. unsigned _BitInt(7): 2 is a bit-field that can hold the values 0, 1, 2, 3, and converts to unsigned _BitInt(7).
The promoted type of unsigned _BitInt(7)x:2
isn't int
, it should be unsigned _BitInt(7)
as the footnote says.