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.
Comment #1 by k.hara.pg — 2014-06-14T11:49:34Z
Comment #2 by github-bugzilla — 2014-06-15T02:53:11Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/b8a9cad38adb096046b12df5509473118af8436c fix Issue 12901 - `in`/`out` contracts on struct constructor must require function body https://github.com/D-Programming-Language/dmd/commit/1686e60ec81d00d4a1f6fb91a2700d723986034a Merge pull request #3664 from 9rnsr/fix12901 Issue 12901 - `in`/`out` contracts on struct constructor must require function body
Comment #3 by hsteoh — 2017-12-28T23:23:21Z
Comment #4 by hsteoh — 2017-12-28T23:31:31Z
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
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/f8b524688ff2a762b8427d1c673dfb1c53c806f7 Re-fix Issue 12901 in/out contracts on struct constructor must require function body https://github.com/dlang/dmd/commit/d0a0b9b01a709b6e18151deafbae49a89ea93d9f Merge pull request #7543 from ibuclaw/reg12901 Re-fix Issue 12901 in/out contracts on struct constructor must require function body merged-on-behalf-of: Mike Franklin <[email protected]>
Comment #6 by dlang-bot — 2021-01-07T12:35:26Z
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