Bug 22606 – init differs from ctor call for structs

Status
RESOLVED
Resolution
INVALID
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
Windows
Creation time
2021-12-18T20:56:54Z
Last change time
2022-12-09T11:59:10Z
Assigned to
No Owner
Creator
Temtaime
See also
https://issues.dlang.org/show_bug.cgi?id=22562

Comments

Comment #0 by temtaime — 2021-12-18T20:56:54Z
import std.stdio; class C { auto foo() { auto self = this; struct S { auto foobar() { return self.bar(); } } return S.init; // lead to crash, S() works OK } int bar() { return 666; } } void main() { auto c = new C; c.foo().foobar.writeln; }
Comment #1 by dfj1esp02 — 2021-12-20T15:40:46Z
You mean you want it for the sake of consistency? FWIW init is now static data that always exists to initialize a variable, but a nested struct needs a dynamic initializer to set the context pointer, init can't do it, but a constructor does it.
Comment #2 by razvan.nitu1305 — 2022-12-09T11:59:10Z
Yes, anonymous4 is right. .init is represented by statically known data. The context pointer has the init value of every other pointer, "null". So in this case, this is a programming mistake.