Is this a bug? +goto

MatheusBN m at gmail.com
Tue Nov 6 00:33:56 UTC 2018


On Tuesday, 6 November 2018 at 00:14:26 UTC, Jonathan M Davis 
wrote:
> 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

Just to be clear, when you say "x exists at the label Q", you 
mean at the same scope, right?

That's interesting but a bit confusing isn't?

And I found a bit strange that in such code, since "x" is never 
used, why it isn't skipped.

I know it's another language but in C at least in GCC there is no 
error over such code, so that's my confusion.

Thanks,

MatheusBN.


More information about the Digitalmars-d-learn mailing list