Bug 8788 – The super constructor call can be prevented by mentioning "return".
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2012-10-09T07:20:00Z
Last change time
2015-06-09T05:11:51Z
Assigned to
nobody
Creator
david.eckardt
Comments
Comment #0 by david.eckardt — 2012-10-09T07:20:46Z
Mentioning a "return" prevents DMD from rejecting code where a constructor has an explicit super() call which is done conditionally or not at all. Also it is possible to return or throw before doing the super() call.
This example code compiles although it should be rejected:
---
class A { this ( ) { } }
class B : A
{
this ( )
{
return;
super();
}
this(int x)
{
try
{
throw new Exception;
super();
}
catch ( ) { }
}
this(float f)
{
if (x == 3)
return;
else
super();
}
this(char c)
{
if (c == 'x')
super();
else
return;
}
}
---
However, if the "return" statement in the second and third constructor is omitted, the compile error is triggered correctly.
Comment #1 by david.eckardt — 2012-10-09T07:27:39Z
Of course the "if" condition in the third constructor should be f >= 3, not x == 3.
Comment #2 by clugdbug — 2012-10-10T02:58:34Z
statement.c, ReturnStatement::semantic() contains this comment:
/* BUG: need to issue an error on:
* this
* { if (x) return;
* super();
* }
*/
Funny that this never got entered in Bugzilla before. The bug however is in the treatment of CSXreturn, mainly in scope.c.
For each branch of code there are 5 cases:
1. neither constructor calls nor returns have been made.
2. at least one subbranch has returned without calling a constructor.
3. all subbranches have called a constructor. Some may have returned afterwards, but at least one has not returned yet.
4. all subbranches have exited after calling a constructor
5. some subbranches have returned after calling a constructor, the others have not yet made constructor calls.
The case with the 'throw' is more difficult than the others. Ideally the first example below would be OK but the second would be an error:
try {
thow SomeFunkyException;
}
catch (SomeFunkyException) {
super();
}
try {
thow SomeFunkyException;
}
catch (DifferentFunkyException) {
super();
}
It might be worth forbidding 'super()' inside catch clauses.
I don't think it's worth the effort. But the 'return' cases should be detected.
Comment #3 by github-bugzilla — 2012-10-20T15:51:36Z