[Issue 1118] New: weird switch statement behaviour

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Apr 9 15:59:31 PDT 2007


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

           Summary: weird switch statement behaviour
           Product: D
           Version: 1.010
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Keywords: accepts-invalid, spec
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: manuelk89 at gmx.net


The documentation for the switch statement says:

    SwitchStatement:
            switch ( Expression ) ScopeStatement

So there must not be 'case', 'default' or scoping brackets after the switch.
Hence this would be valid code (and actually compiles, throwing a 'Switch
Default' error for the first example):

        switch (1)
                for (int i=0; i<5; i++) writefln(i);

        // another, yet acceptable but ugly looking example example
        switch (true)
        case true:  writefln("foo");

        switch (5)
        {
                // do anything but no switch/case
                writefln("foo");
        }

But beside a ScopeStatement, even a normal Statement gets accepted by the
compiler (at least I could not figure out a transition from a ScopeStatement to
an ExpressionStatement):

    switch(2)
        writefln("foo");


_________________________________________________________________
Examples were tested on Ubuntu Linux with
    * dmd 1.010
    * gdc 0.23

_________________________________________________________________
PS: I think a definition like this would do the job:

    SwitchStatement:
            switch ( Expression ) { SwitchItemList }

    SwitchItemList:
            SwitchItem
            SwitchItem SwitchItemList

    SwitchItem:
            CaseStatement
            DefaultStatement


-- 



More information about the Digitalmars-d-bugs mailing list