@@ -3174,10 +3174,12 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
31743174 if (dsym.semanticRun >= PASS .semanticdone)
31753175 return ;
31763176
3177- if (dsym.isAnonymous() && dsym._init)
3177+ const bool isAnonymous = dsym.isAnonymous();
3178+ if (isAnonymous && dsym._init)
31783179 {
3179- .error(dsym.loc, " anonymous bitfield cannot have default initializer" );
3180+ .error(dsym._init. loc, " anonymous bitfield cannot have default initializer" );
31803181 dsym._init = null ;
3182+ dsym.errors = true ;
31813183 }
31823184
31833185 visit(cast (VarDeclaration)dsym);
@@ -3201,29 +3203,47 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
32013203 if (! dsym.type.isIntegral())
32023204 {
32033205 // C11 6.7.2.1-5
3204- error(dsym.loc, " bitfield type `%s` is not an integer type" , dsym.type.toChars());
3206+ if (isAnonymous)
3207+ error(dsym.loc, " anonymous bitfield cannot be of non-integral type `%s`" , dsym.type.toChars());
3208+ else
3209+ error(dsym.loc, " bitfield `%s` cannot be of non-integral type `%s`" , dsym.toChars(), dsym.type.toChars());
32053210 dsym.errors = true ;
3211+ return ;
32063212 }
32073213 if (! width.isIntegerExp())
32083214 {
32093215 error(width.loc, " bitfield width `%s` is not an integer constant" , dsym.width.toChars());
32103216 dsym.errors = true ;
32113217 }
32123218 const uwidth = width.toInteger(); // uwidth is unsigned
3213- if (uwidth == 0 && ! dsym. isAnonymous() )
3219+ if (uwidth == 0 && ! isAnonymous)
32143220 {
3215- error(width .loc, " bitfield `%s` has zero width" , dsym.toChars());
3221+ error(dsym .loc, " bitfield `%s` cannot have zero width" , dsym.toChars());
32163222 dsym.errors = true ;
32173223 }
3218- const sz = dsym.type.size();
3219- if (sz == SIZE_INVALID )
3220- dsym.errors = true ;
3221- const max_width = sz * 8 ;
3222- if (uwidth > max_width)
3224+ if (cast (long )uwidth < 0 )
32233225 {
3224- error(width.loc, " width `%lld` of bitfield `%s` does not fit in type `%s`" , cast (long )uwidth, dsym.toChars(), dsym.type.toChars());
3226+ if (isAnonymous)
3227+ error(width.loc, " anonymous bitfield has negative width `%lld`" , cast (long )uwidth);
3228+ else
3229+ error(width.loc, " bitfield `%s` has negative width `%lld`" , dsym.toChars(), cast (long )uwidth);
32253230 dsym.errors = true ;
32263231 }
3232+ else
3233+ {
3234+ const sz = dsym.type.size();
3235+ if (sz == SIZE_INVALID )
3236+ dsym.errors = true ;
3237+ const max_width = sz * 8 ;
3238+ if (uwidth > max_width)
3239+ {
3240+ if (isAnonymous)
3241+ error(width.loc, " width `%lld` of anonymous bitfield does not fit in type `%s`" , cast (long )uwidth, dsym.type.toChars());
3242+ else
3243+ error(width.loc, " width `%lld` of bitfield `%s` does not fit in type `%s`" , cast (long )uwidth, dsym.toChars(), dsym.type.toChars());
3244+ dsym.errors = true ;
3245+ }
3246+ }
32273247 dsym.fieldWidth = cast (uint )uwidth;
32283248 }
32293249
@@ -7046,10 +7066,20 @@ bool determineFields(AggregateDeclaration ad)
70467066 {
70477067 if (ad == tvs.sym)
70487068 {
7069+ if (ad.type.ty == Terror || ad.errors)
7070+ return 1 ; // failed already
7071+
70497072 const (char )* psz = (v.type.toBasetype().ty == Tsarray) ? " static array of " : " " ;
7050- .error(ad.loc, " %s `%s` cannot have field `%s` with %ssame struct type" , ad.kind, ad.toPrettyChars, v.toChars(), psz);
7051- ad.type = Type.terror;
7052- ad.errors = true ;
7073+ if (! v.isAnonymous())
7074+ .error(v.loc, " %s `%s` cannot have field `%s` with %ssame struct type" , ad.kind, ad.toPrettyChars, v.toChars(), psz);
7075+ else
7076+ .error(v.loc, " %s `%s` cannot have anonymous field with %ssame struct type" , ad.kind, ad.toPrettyChars, psz);
7077+ // Don't cache errors from speculative semantic
7078+ if (! global.gag)
7079+ {
7080+ ad.type = Type.terror;
7081+ ad.errors = true ;
7082+ }
70537083 return 1 ;
70547084 }
70557085 }
@@ -8439,10 +8469,9 @@ private extern(C++) class SetFieldOffsetVisitor : Visitor
84398469 uint memalignsize = target.fieldalign(t); // size of member for alignment purposes
84408470 if (log) printf(" memsize: %u memalignsize: %u\n " , memsize, memalignsize);
84418471
8442- if (bfd.fieldWidth == 0 && ! anon)
8443- error(bfd.loc, " named bit fields cannot have 0 width" );
8444- if (bfd.fieldWidth > memsize * 8 )
8445- error(bfd.loc, " bit field width %d is larger than type" , bfd.fieldWidth);
8472+ // Handled in dsymbolSemantic as errors
8473+ assert (bfd.fieldWidth != 0 || anon, " named bit fields cannot have 0 width" );
8474+ assert (bfd.fieldWidth <= memsize * 8 , " bit field width is larger than type" );
84468475
84478476 const style = target.c.bitFieldStyle;
84488477 if (style != TargetC.BitFieldStyle.MS && style != TargetC.BitFieldStyle.Gcc_Clang)
0 commit comments