D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 23314 - Language spec falsely states that struct field invariants are checked
Summary: Language spec falsely states that struct field invariants are checked
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dlang.org (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2022-08-29 16:50 UTC by Paul Backus
Modified: 2022-09-05 03:14 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Paul Backus 2022-08-29 16:50:31 UTC
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
Comment 1 Dlang Bot 2022-09-05 02:39:28 UTC
@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
Comment 2 Dlang Bot 2022-09-05 03:14:20 UTC
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