Skip to content

Incorrect behavior when promoting bit-field of bit-precise type #87641

@Halalaluyafail3

Description

@Halalaluyafail3

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    c23clang:frontendLanguage frontend issues, e.g. anything involving "Sema"confirmedVerified by a second party

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions