D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12901 - Assignments to outside members in `in`/`out` contracts shouldn't be allowed
Summary: Assignments to outside members in `in`/`out` contracts shouldn't be allowed
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 major
Assignee: No Owner
URL:
Keywords: accepts-invalid, pull
Depends on:
Blocks:
 
Reported: 2014-06-13 06:07 UTC by Denis Shelomovskii
Modified: 2021-01-07 12:35 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Denis Shelomovskii 2014-06-13 06:07:50 UTC
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 2 github-bugzilla 2014-06-15 02:53:11 UTC
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 hsteoh 2017-12-28 23:23:21 UTC
Broken by https://github.com/dlang/dmd/pull/7527
Comment 4 hsteoh 2017-12-28 23:31:31 UTC
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 github-bugzilla 2017-12-30 00:21:09 UTC
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 <JinShil@users.noreply.github.com>
Comment 6 Dlang Bot 2021-01-07 12:35:26 UTC
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