[Issue 603] Undocumented behaviour: case and default create a scope

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jul 7 11:50:13 PDT 2009


http://d.puremagic.com/issues/show_bug.cgi?id=603





--- Comment #7 from Christian Kamm <kamm-removethis at incasoftware.de>  2009-07-07 11:50:12 PDT ---
The fact that default and case statements create a new scope is evident in the
frontend code:

statements = new Statements();
while (token.value != TOKcase &&
       token.value != TOKdefault &&
       token.value != TOKrcurly)
{
    statements->push(parseStatement(PSsemi | PScurlyscope));
}
s = new CompoundStatement(loc, statements);
s = new ScopeStatement(loc, s);

With this in mind, it would make sense to add this to the section on switch
statements:

Case and default statements create a new scope that contains all statements up
until the next case or default statement with the same parent, or the end of
the enclosing scope.

Example:

switch(i) {
  case 1:
     ...
  case 2:
    if (i) {
      case 3:
        ...
      case 4:
        ...
    }
  case 5:
}

is equivalent to

switch(i) {
  case 1:
  {  ...  }
  case 2:
  {
    if (i) {
      case 3:
      { ... }
      case 4:
      { ... }
    }
  }
  case 5:
}

I'm not marking this as 'patch' because I'm not happy with 'with the same
parent'. Suggestions? Also, can someone suggest a grammar change that would
explain this behavior? Replacing

case ExpressionList : Statement
with
case ExpressionList : ScopeStatement

isn't right as ScopeStatement is either BlockStatement or NonEmptyStatement. I
think we need a new ScopeCaseStatement here.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list