Thread overview
[Issue 1399] New: Wrapping a case statement in a version statement gives a shadowing declaration error.
Aug 04, 2007
d-bugmail
Aug 05, 2007
BCS
Aug 06, 2007
d-bugmail
Aug 07, 2007
d-bugmail
Aug 07, 2007
BCS
Aug 07, 2007
d-bugmail
Aug 07, 2007
d-bugmail
August 04, 2007
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: bugzilla@digitalmars.com
        ReportedBy: aziz.kerim@gmail.com


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:
  }
}


-- 

August 05, 2007
Reply to d-bugmail@puremagic.com,

> 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: bugzilla@digitalmars.com
> ReportedBy: aziz.kerim@gmail.com
> 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


August 06, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1399


smjg@iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg@iname.com
           Keywords|rejects-valid               |diagnostic




------- Comment #2 from smjg@iname.com  2007-08-06 11:00 -------
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.


-- 

August 07, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1399





------- Comment #3 from aziz.kerim@gmail.com  2007-08-07 02:46 -------
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:
}


-- 

August 07, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1399





------- Comment #4 from aziz.kerim@gmail.com  2007-08-07 02:54 -------
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.


-- 

August 07, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1399





------- Comment #5 from smjg@iname.com  2007-08-07 04:40 -------
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


-- 

August 07, 2007
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.