The language spec's section on struct invariants [1] states the following: > Any invariants for fields are applied before the struct invariant. However, this has apparently never been true. According to run.dlang.io, all compiler versions since DMD 2.066.1 compile the following program such that it runs to completion successfully, which is only possible if S's invariant is never checked: --- struct S { invariant { assert(0); } } struct T { S s; invariant { assert(1); } void fun() {} } void main() { T t; t.fun(); } --- Earlier versions of DMD are unable to parse the invariant contract, which suggests this behavior has never worked as described in the spec. See also issue 19537 for a similar error in the spec for class invariants. [1] https://dlang.org/spec/struct.html#Invariant
@pbackus created dlang/dlang.org pull request #3405 "Fix issue 23314 - Language spec falsely states that struct field inva…" fixing this issue: - Fix issue 23314 - Language spec falsely states that struct field invariants are checked This behavior has never been implemented, and users should not rely on it in their code. https://github.com/dlang/dlang.org/pull/3405
dlang/dlang.org pull request #3405 "Fix issue 23314 - Language spec falsely states that struct field inva…" was merged into master: - 7239b35eca2809b779a41b9d25f9bd98aa5705ba by Paul Backus: Fix issue 23314 - Language spec falsely states that struct field invariants are checked This behavior has never been implemented, and users should not rely on it in their code. https://github.com/dlang/dlang.org/pull/3405