Bug 1399 – Wrapping a case statement in a version statement gives a shadowing declaration error.

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Linux
Creation time
2007-08-04T12:17:09Z
Last change time
2019-08-12T10:24:25Z
Keywords
diagnostic
Assigned to
No Owner
Creator
Aziz Köksal

Comments

Comment #0 by aziz.koeksal — 2007-08-04T12:17:09Z
When compiling the snippet below with -version=D2 I get the following error: "Error: shadowing declaration func.a is deprecated" void func() { switch(1) { case 1: auto a = 2; break; version(D2) { case 2: auto a = 2; // error break; } default: } }
Comment #1 by lovesyao — 2007-08-05T16:56:04Z
Reply to [email protected], > http://d.puremagic.com/issues/show_bug.cgi?id=1399 > > Summary: Wrapping a case statement in a version statement > gives a > shadowing declaration error. > Product: D > Version: 1.019 > Platform: PC > OS/Version: Linux > Status: NEW > Keywords: rejects-valid > Severity: normal > Priority: P2 > Component: DMD > AssignedTo: [email protected] > ReportedBy: [email protected] > When compiling the snippet below with -version=D2 I get the following > error: "Error: shadowing declaration func.a is deprecated" > > void func() > { > switch(1) > { > case 1: > auto a = 2; > break; > version(D2) > { > case 2: > auto a = 2; // error > break; > } > default: > } > } Invalid: version blocks are not a naming scope. Well it might be a bug because it should be a "can't have two a's" error, not a shadowing error
Comment #2 by smjg — 2007-08-06T11:00:19Z
Indeed, the bug is that it delivers the wrong error message. The correct message would be bz1399.d(11): Error: declaration bz1399.func.a is already defined However, if I get rid of the version block, the code compiles without error, but this is due to issue 603. And Nazo, please don't quote the entire message when replying.
Comment #3 by aziz.koeksal — 2007-08-07T02:46:29Z
I think I can explain how the compiler understands the code I provided. Stewart has observed correctly, that when you remove the version block the code compiles without errors. This is because every case and default block introduces a new scope (the switch block has its own scope as well.) The two case blocks are similar to this code: void func() { // This is valid code. { auto a = 0; } { auto a = 0; } } The problem arises when you wrap the second case statement into a version block, because then the version block plus the nested case block is contained by the first case block. The reason for this is that the parses parses every statement until it hits another case or default block. The code with the version block would thus be seen by the compiler as: void func() { { auto a = 0; { auto a = 0; // Error: shadowing outer a } } } The following code generates the same error: switch (1) { auto a = 0; case 1: auto a = 1; // Error: shadowing declaration default: }
Comment #4 by aziz.koeksal — 2007-08-07T02:54:33Z
You actually don't need the version block to demonstrate this bug. This will do as well: switch(1) { case 1: auto a = 1; { case 2: auto a = 2; } default: } By the way, regarding the last code snippet I showed in my previous post: this isn't a bug, the compiler behaves correctly.
Comment #5 by smjg — 2007-08-07T04:40:43Z
That's different, because when it isn't the body of a CC statement, { } opens a new scope. In this case, the compiler is behaving correctly according to the spec as I try it. bz1399c4.d(8): Error: shadowing declaration bz1399c4.main.a is deprecated
Comment #6 by lovesyao — 2007-08-07T10:15:17Z
This might have something to do with an error I keep running into where if you have a static if statement with nothing but a case label, then sometime things go strange. I don't have a test case handy but I'll try to work one up sooner or later.
Comment #7 by razvan.nitu1305 — 2019-08-12T10:24:25Z
This the correct behavior. The compiler identifies the end of a case statement if it has encountered another case/default statement or the ending `}` of the switch statement. Once a version identifier or even a static if is encountered, the parser sees it as part of the case statement that it is analyzing at that time. A workaround would be to add the version block in the default statement and lose the `case 2` statement. Closing as invalid.