Bug 18688 – Constructors shouldn't have implicit super call if it throws

Status
REOPENED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-03-28T14:43:59Z
Last change time
2024-12-13T18:58:01Z
Assigned to
No Owner
Creator
Yuxuan Shui
See also
https://issues.dlang.org/show_bug.cgi?id=18736, https://issues.dlang.org/show_bug.cgi?id=19205
Moved to GitHub: dmd#19415 →

Comments

Comment #0 by yshuiv7 — 2018-03-28T14:43:59Z
This example doesn't compile: class A { this(int x){} @disable this(); } class B: A { this(int x) { super(x); } this(string b) { switch(b) { case "a":break; default: assert(false); } this(1); } } Possibly because the compile decides 'this(1)' is not always reachable, and tries to implicitly call super() in that case. But if 'this(1)' is not reachable, the constructor is guaranteed to throw, thus super call should not be required. Also, this program has almost the same behavior (SwitchError is thrown instead of AssertError), but it compiles: class A { this(int x){} @disable this(); } class B: A { this(int x) { super(x); } this(string b) { final switch(b) { case "a":break; } this(1); } }
Comment #1 by razvan.nitu1305 — 2018-03-29T11:07:05Z
Comment #2 by github-bugzilla — 2018-03-31T00:29:58Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/978cb051c2f4b3f716a330032280aee234214db8 Fix Issue 18688 - Constructors shouldn't have implicit super call if it throws https://github.com/dlang/dmd/commit/830f9e0ed422887193b6c7454a940c7a92b05de8 Merge pull request #8100 from RazvanN7/Issue_18688 Fix Issue 18688 - Constructors shouldn't have implicit super call if it throws merged-on-behalf-of: Andrei Alexandrescu <[email protected]>
Comment #3 by bugzilla — 2018-04-04T01:17:19Z
The real problem is that delegating constructor calls are not allowed after labels, and the case and default statements are labels. The compiler should detect those. The trouble with labels is the primitive flow analysis done by the compiler cannot handle them. So they are just disallowed. Maybe a future enhancement could cover it.
Comment #4 by robert.schadek — 2024-12-13T18:58:01Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19415 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB