Is this a bug? +goto

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Nov 6 00:14:26 UTC 2018


On Monday, November 5, 2018 4:54:59 PM MST MatheusBN via Digitalmars-d-learn 
wrote:
> Hi,
>
> I posted this in another thread but without any response.
>
> This code:
>
> void main(){
>       goto Q;
>       int x;
>       Q:
>       writeln("a");
> }
>
> Gives me this error: "source_file.d(4): Error: goto skips
> declaration of variable source.main.x at source_file.d(5)"
>
>
> Now, if I add a pair of brackets:
>
> void main(){
>       {
>           goto Q;
>           int x;
>       }
>       Q:
>       writeln("a");
> }
>
> It works. So Is this a bug?

All the spec says on the matter is that

"It is illegal for a GotoStatement to be used to skip initializations."

https://dlang.org/spec/statement.html#goto-statement

In the first case, x exists at the label Q, and its initialization was
skipped, so it's clearly illegal. However, in the second case, because of
the braces, x does _not_ exist at the label Q, so its initialization was not
skipped, so I don't see why it wouldn't be legal based on what the spec
says, and I don't see any reason to make it illegal. Conceptually, it's
doing exactly what you'd get with a break if the braces were for a loop.
However, it is true that the spec could (and probably should) be more
specific on the matter.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list