www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Goto skipping declarations

reply Ben Jones <fake fake.fake> writes:
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;
}
```
May 03
next sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
May 03
next sibling parent Ben Jones <fake fake.fake> writes:
On Friday, 3 May 2024 at 20:38:31 UTC, Jonathan M Davis wrote:

https://issues.dlang.org/show_bug.cgi?id=24535
May 03
prev sibling parent "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
On 04/05/2024 8:38 AM, Jonathan M Davis wrote:
 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.
It is very simple code. A reverse search over the double linked list for the goto from the label. I looked into it fairly recently as an example of type state analysis D is already designed against.
May 03
prev sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Friday, May 3, 2024 2:38:31 PM MDT Jonathan M Davis via Digitalmars-d-learn 
wrote:
 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.
Here. I reported it: https://issues.dlang.org/show_bug.cgi?id=24534 - Jonathan M Davis
May 03