310-tag-shadow-inner-scope.c (920B)
1 /* C 6.7.2.3p2 + 6.2.1: a struct/union tag introduced inside a nested 2 * block scope must not redefine the same-named tag in an outer scope. 3 * cc.scm's parse-aggregate-spec calls tag-lookup (which walks all 4 * frames) and then complete-agg! mutates the result in place — so an 5 * inner `struct S { ... };` clobbers the outer tag's fields/size. 6 */ 7 8 struct S { int x; }; 9 10 int before_size = sizeof(struct S); /* 4 */ 11 12 int dummy(void) { 13 /* Shadow with a strictly larger struct so the bug, if present, 14 * would mutate the outer ctype's size to 24. */ 15 struct S { long y; long z; long w; }; 16 struct S s; 17 s.y = 1; s.z = 2; s.w = 3; 18 return (int)(s.y + s.z + s.w); 19 } 20 21 int after_size = sizeof(struct S); /* must still be 4 */ 22 23 int main(int argc, char **argv) { 24 if (before_size != 4) return 1; 25 if (dummy() != 6) return 2; 26 if (after_size != 4) return 3; 27 if (sizeof(struct S) != 4) return 4; 28 return 0; 29 }