www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19205] New: Cannot call superclass ctor after end of switch

https://issues.dlang.org/show_bug.cgi?id=19205

          Issue ID: 19205
           Summary: Cannot call superclass ctor after end of switch
                    statement
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: regression
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: hsteoh quickfur.ath.cx

This code used to work (dmd circa 2.077):
-------
class B {
        int w, h;
        this(int width, int height) {
                w = width;
                h = height;
        }
}
class C : B {
        this(int size) {
                int w, h;
                if (size == 0) {
                        w = 10;
                        h = 20;
                } else {
                        w = 20;
                        h = 10;
                }
                super(w, h); // OK
        }
}
class D : B {
        enum E { portrait, landscape }
        this(E type) {
                int w, h;
                final switch (type) {
                        case E.portrait:
                                w = 10;
                                h = 20;
                                break;
                        case E.landscape:
                                w = 20;
                                h = 10;
                                break;
                }
                super(w, h); // NG (this is line 37)
        }
}
-------

Now (on dmd git master) it triggers the following error:
-------
test.d(37): Error: constructor calls not allowed in loops or after labels
-------

Expected behaviour: since the switch statement has ended, the fact that there
are switch labels ought not to influence whether calls to the superclass ctor
are allowed or not, since it's not possible to jump out of the switch statement
somewhere else.  Furthermore, there are no loops involved, so there is no
reason the superclass ctor can't be called at this point.

The switch is also functionally equivalent to the if-statement in class C's
ctor, so there is no reason why it should make a difference in whether the
superclass ctor can be called.

--
Aug 29 2018