Skip to content

Commit 40461b0

Browse files
authored
[spec] Add warning about struct .init not initializing context pointer (#3825)
1 parent 2255429 commit 40461b0

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

spec/property.dd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ static assert(Foo.init.a == 0);
115115
static assert(Foo.init.b == 7);
116116
----------------
117117
)
118+
119+
$(H3 $(LNAME2 init-vs-construction, `.init` vs Default Construction))
120+
118121
$(P Note that $(D .init) produces a default initialized object, not a
119122
default constructed one. If there is a default constructor for an object,
120123
it may produce a different value.)
@@ -134,6 +137,8 @@ void main()
134137
}
135138
S s1; // OK. S() correctly initialize its context pointer.
136139
S s2 = S(); // OK. same as s1
140+
s1.foo(); // OK
141+
137142
S s3 = S.init; // Bad. the context pointer in s3 is null
138143
s3.foo(); // Access violation
139144
}

spec/struct.dd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2323,7 +2323,7 @@ $(H2 $(LNAME2 nested, Nested Structs))
23232323
---
23242324
)
23252325

2326-
$(P The static attribute will prevent a struct from being nested. As such,
2326+
$(P The `static` attribute will prevent a struct from being nested. As such,
23272327
the struct will not have access to its enclosing scope.)
23282328

23292329
$(SPEC_RUNNABLE_EXAMPLE_FAIL
@@ -2343,6 +2343,10 @@ $(H2 $(LNAME2 nested, Nested Structs))
23432343
---
23442344
)
23452345

2346+
$(P $(B Warning): For nested structs, $(DDSUBLINK spec/property, init-vs-construction,
2347+
`.init` is not the same as default construction).)
2348+
2349+
23462350
$(H2 $(LNAME2 unions_and_special_memb_funct, Unions and Special Member Functions))
23472351

23482352
$(P Unions may not have postblits, destructors, or invariants.)

0 commit comments

Comments
 (0)