[Issue 18712] [Reg 2.072] bogus "switch skips declaration" error with case in mixin
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon May 14 19:31:40 UTC 2018
https://issues.dlang.org/show_bug.cgi?id=18712
Walter Bright <bugzilla at digitalmars.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution|--- |WONTFIX
--- Comment #4 from Walter Bright <bugzilla at digitalmars.com> ---
Here's what's happening. After a case/default statement, the parser makes the
code between one case/default and the next into a scope. So it looks like:
int test(int n)
{
switch(n)
{
mixin("case 0:");
int x = 1;
return x;
case 1:
{
int y = 2;
return y;
}
default:
{
return -1;
}
}
}
and, of course, now the error message makes sense. `x` is visible to the
following two scopes, whether or not the error message is generated. Any time
the case/default is not directly in the switch body (not nested via { } or a
mixin) the implicit { } scope is not generated. Oops.
Try putting { } in various combinations, and you'll see how it all comes
unglued.
I can't think of any solution that 1) works in all cases and 2) doesn't break a
lot of existing code. So we're just stuck with it. Fortunately, there is a
workaround. Recode the switch like this:
int test(int n)
{
switch(n)
{
mixin("case 0:");
{
int x = 1;
return x;
}
case 1:
int y = 2;
return y;
default:
return -1;
}
}
I'm going to close this as WONTFIX. If anyone has a brainwave on how to make it
work in all cases without breaking code, reopen with proof.
Note that the fundamental problem is a combination of:
1. allowing case/default statements to appear inside nested scopes
2. implicit generation of scopes between case/default pairs
--
More information about the Digitalmars-d-bugs
mailing list