c2compiler icon indicating copy to clipboard operation
c2compiler copied to clipboard

field-init of bitfields gives false 'duplicate initialization' error

Open bvdberg opened this issue 8 months ago • 3 comments

type Foo struct {
    u32 a : 1;
    u32 b : 3;
}

Foo f1 = { 1, 2 }      // ok
Foo f2 = { .a=1, .b=2 } // gives error: duplicate initialization of field 'b'

Needs to be fixed in StructFieldInitChecker

bvdberg avatar May 21 '25 20:05 bvdberg

2 is an invalid initializer for bit-fieldb of length 1. Both initializers should give an error :)

chqrlie avatar May 21 '25 20:05 chqrlie

The bit-width used to be 3, but I changed it to 1 without changing the init-value. Example should be good now.. The issue still remains.

bvdberg avatar May 22 '25 06:05 bvdberg

We use an InitChecker to check for multiple initializers, but we should not use the offset alone.

We should use a range of bit offsets, which is supported by InitCheckers for case ranges. If StructTypeDecl.findAny would return the bit offset and bit width, we could use the add2 and find2 methods to check for multiple initialization of the same bits.

This limits structure sizes to 512 MB which is not a real concern.

chqrlie avatar May 22 '25 07:05 chqrlie

fixed

bvdberg avatar Jul 02 '25 17:07 bvdberg