D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 11211 - Use of uninitialized struct allowed in a subclass
Summary: Use of uninitialized struct allowed in a subclass
Status: RESOLVED LATER
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2013-10-09 13:22 UTC by Max Samukha
Modified: 2021-01-24 13:13 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Max Samukha 2013-10-09 13:22:19 UTC
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 mhh 2021-01-24 07:23:25 UTC
The struct is in a valid state
Comment 2 mhh 2021-01-24 07:25:44 UTC
-1 to me. It is however a language change to disallow this I think.
Comment 3 Max Samukha 2021-01-24 09:07:11 UTC
(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 mhh 2021-01-24 13:13:16 UTC
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.