Bug 11211 – Use of uninitialized struct allowed in a subclass
Status
RESOLVED
Resolution
LATER
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-10-09T13:22:19Z
Last change time
2021-01-24T13:13:16Z
Keywords
accepts-invalid
Assigned to
No Owner
Creator
Max Samukha
Comments
Comment #0 by samukha — 2013-10-09T13:22:19Z
struct S {
@disable this();
bool cted;
this(int x) {
cted = true;
}
void foo() {
assert(cted);
}
}
class A {
S s;
this(int x) {
s = S(1);
}
}
class B : A {
this() {
s.foo(); // shouldn't compile
super(1);
}
}
void main() {
auto b = new B;
}
The base class constructor should have been called before the struct can be accessed in the subclass.
Comment #1 by maxhaton — 2021-01-24T07:23:25Z
The struct is in a valid state
Comment #2 by maxhaton — 2021-01-24T07:25:44Z
-1 to me. It is however a language change to disallow this I think.
Comment #3 by maxsamukha — 2021-01-24T09:07:11Z
(In reply to mhh from comment #1)
> The struct is in a valid state
How is that? The default constructor is explicitly disabled to tell the compiler that the init state is *not valid* for that struct type (it is even dubbed "best practice" by p.6 of https://dlang.org/spec/struct.html#field-init).
Comment #4 by maxhaton — 2021-01-24T13:13:16Z
The invalid state thing was me misreading the test case, apologies.
I have brought up some kind of substrctural types in a meting so who knows if this behaviour may one day be properly fixed.