Goto skips declaration

luckoverthere luckoverthere at gmail.cm
Tue Oct 30 23:31:46 UTC 2018


On Tuesday, 30 October 2018 at 21:21:22 UTC, Michelle Long wrote:
>
> You take examples WAY to literal. Sheesh, do you really think 
> life is that simple? Do you need every detail spelled out for 
> things to make sense?
>
> foreach(x; Y)
> {
>     if (something or another which I won't spell out no matter 
> how much you cry)
>         goto Q; // compiler complains here about x
>     int x;
> }
> Q:
>
> There is no reason why the compiler should have a problem with 
> this.. the fact that using brackets around int x; satisfies the 
> compiler proves this.


struct Magic
{
     ~Magic()
     {
         writeln("blah");
     }
]

void test()
{
     if(cond)
         goto Y;

     Magic magic;

     Y:
     writeln("stuff");

     // calls ~Magic();
}

You'd need a bunch of extra information to know what is and 
wasn't actually declared and initialized.

"Putting brackets around it proves it".

void test()
{
     if(cond)
         goto Y;

     {
         Magic magic;

         // calls ~Magic();
     }

     Y:
     writeln("stuff");

}

There is no more conflict with brackets, yes. But expecting the 
compiler to do this changes the behavior of the code. Sure if 
there's no destructor it could work around it, but it would 
complicated code generation. Adding a bunch of time looking for 
these circumstances.

It's just not worth it, use brackets like you are now or move the 
declarations of your variables up to the top of the scope before 
any gotos. Pretty simple workarounds.



More information about the Digitalmars-d mailing list