D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 18143 - in/out contracts should be implicitly 'this' const
Summary: in/out contracts should be implicitly 'this' const
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-12-30 00:51 UTC by Iain Buclaw
Modified: 2021-01-25 11:18 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Iain Buclaw 2017-12-30 00:51:38 UTC
The same already applies for invariants, you shouldn't be able to modify internal state inside a in/out contract body.


e.g:
---

struct S
{
    int a;

    invariant
    { a = 42; }  // fails, cannot modify const 'this'

    this(int n)
    in
    { a = 42; }  // compiles -> should fail
    out(result)
    { a = 42; }  // compiles -> should fail
    body
    { }
}

---

This is particularly problematic because program behaviour changes in release mode.
Comment 1 Iain Buclaw 2017-12-30 00:52:01 UTC
See also comments in issue 12901
Comment 2 github-bugzilla 2018-01-06 21:46:07 UTC
Commits pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/3d024cb20277826a428ac616a7324be027843122
fix Issue 18143 - in/out contracts should be implicitly 'this' const

https://github.com/dlang/dmd/commit/4c7ea172c8f1c714fe85a3fda54ec2ddfc0598f1
Merge pull request #7553 from ibuclaw/issue18143

fix Issue 18143 - in/out contracts should be implicitly 'this' const
merged-on-behalf-of: Mike Franklin <JinShil@users.noreply.github.com>
Comment 3 Dlang Bot 2021-01-25 11:18:38 UTC
dlang/dmd pull request #12153 "[dmd-cxx] Backport fixes for issue 18143 and 18719." was merged into dmd-cxx:

- b6e4baefe04777e905a61c7d1291a9ed691a8698 by Iain Buclaw:
  fix Issue 18143 - in/out contracts should be implicitly 'this' const

https://github.com/dlang/dmd/pull/12153