Bug 12901 – Assignments to outside members in `in`/`out` contracts shouldn't be allowed
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-06-13T06:07:50Z
Last change time
2021-01-07T12:35:26Z
Keywords
accepts-invalid, pull
Assigned to
No Owner
Creator
Denis Shelomovskii
Comments
Comment #0 by verylonglogin.reg — 2014-06-13T06:07:50Z
This code must not compile:
---
struct S
{
int a;
this(int n)
in // or `out`
{ a = n; }
}
void main()
{
assert(S(5).a == 5); // will be false with `-release`
}
---
This issue is a source of nasty release-only bugs.
Arguably, the original fix was invalid: it doesn't stop you from writing:
````
struct S
{
int a;
this(int n) in { a = n; } body {} // or do {}
}
````
The problem really is allowing assignment to struct members from inside a contract, which is abuse of the contract feature. Perhaps the solution should be to enforce purity on the contract, or, less intrusively, make the contract body const (i.e., `this` is const inside the contract body).
Comment #5 by github-bugzilla — 2017-12-30T00:21:09Z
dlang/dmd pull request #12106 "[dmd-cxx] Implement new syntax for contracts and invariants" was merged into dmd-cxx:
- fe242591c47c2151f787a5b600af9317008da013 by Iain Buclaw:
[dmd-cxx] Re-fix Issue 12901 in/out contracts on struct constructor must require function body
https://github.com/dlang/dmd/pull/12106