Hi! finish_bitfield_representative has an early out if the field after a bitfield has error_mark_node type, but that early out leads to TREE_TYPE of the DECL_BIT_FIELD_REPRESENTATIVE being NULL, which breaks assumptions on code that uses the DECL_BIT_FIELD_REPRESENTATIVE during error-recovery.
The following patch instead sets TREE_TYPE of the representative to error_mark_node, something the users can deal with better. At this point the representative can be set as DECL_BIT_FIELD_REPRESENTATIVE for multiple bitfields, so making sure that we clear the DECL_BIT_FIELD_REPRESENTATIVE instead would be harder (but doable, e.g. with the error_mark_node TREE_TYPE set by this patch set some flag in the caller and if the flag is there, walk all the fields once again and clear all DECL_BIT_FIELD_REPRESENTATIVE that have error_mark_node TREE_TYPE). Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2021-06-24 Jakub Jelinek <ja...@redhat.com> PR middle-end/101172 * stor-layout.c (finish_bitfield_representative): If nextf has error_mark_node type, set repr type to error_mark_node too. * gcc.dg/pr101172.c: New test. --- gcc/stor-layout.c.jj 2021-06-21 09:39:21.798485474 +0200 +++ gcc/stor-layout.c 2021-06-23 11:10:22.617680051 +0200 @@ -2086,7 +2086,10 @@ finish_bitfield_representative (tree rep /* If there was an error, the field may be not laid out correctly. Don't bother to do anything. */ if (TREE_TYPE (nextf) == error_mark_node) - return; + { + TREE_TYPE (repr) = error_mark_node; + return; + } maxsize = size_diffop (DECL_FIELD_OFFSET (nextf), DECL_FIELD_OFFSET (repr)); if (tree_fits_uhwi_p (maxsize)) --- gcc/testsuite/gcc.dg/pr101172.c.jj 2021-06-23 11:15:19.934670004 +0200 +++ gcc/testsuite/gcc.dg/pr101172.c 2021-06-23 11:15:07.255841009 +0200 @@ -0,0 +1,20 @@ +/* PR middle-end/101172 */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +union U +{ + int a[3]; + struct + { + int a : 3; + struct this_struct var; /* { dg-error "field 'var' has incomplete type" } */ + } b; +}; + +const union U hello = {.a = {1, 2, 3}}; + +void foo() +{ + int x = hello.b.a; +} Jakub