On Friday, May 3, 2024 1:15:16 PM MDT Ben Jones via Digitalmars-d-learn wrote: > In general, you can't skip a declaration with goto, but it seems > to be allowed if the declaration you're skipping is labelled... > Is that expected or an accepts invalid bug? > > https://godbolt.org/z/4qx8Pf6G7 > > ```d > void f1(){ //fails with error about skipping a declaration > int x; > goto Label; > int y; > Label: > int z; > } > > void f2(){ //compiles fine > int x; > goto Label; > Dummy: > int y; > Label: > int z; > } > ```
It has to be a bug, and taking it a step further shows that. If you print out y, you'll get a seemingly random number. E.G. On the first run, I got 554440803 and on the second I got 549310547 Presumably, it's a garbage value from whatever happened to be on the stack. I'm quite sure that the spec doesn't have anything about being allowed to skip a declaration just because it has a label on it (honestly, if we _did_ want that to be the case, the spec would probably be missing it, since it tends to fall on the side of having too few details rather than too many), but even if it did, the code is clearly doing something that should not be happening with initialization without explicitly using = void. So, _something_ here would nee to be fixed. In any case, I expect that the compiler is just going dumb here because of the label for some reason, and one or more of the checks that it's supposed to be doing is being missed. - Jonathan M Davis