Bug 24565 – out contract variable is implicitly const

Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P1
Component
dlang.org
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2024-05-24T19:41:23Z
Last change time
2024-05-28T12:36:45Z
Keywords
pull
Assigned to
No Owner
Creator
Nick Treleaven

Comments

Comment #0 by nick — 2024-05-24T19:41:23Z
The spec does not mention this: https://dlang.org/spec/function.html#postconditions struct R { bool empty(); // only logically const, not D const } R f() // returns a mutable R out(r; r.empty) // Error: mutable method `R.empty` is not callable using a `const` object { R r; // not const return r; } For the above case, an `out` contract cannot be used. Note that inserting `assert(r.empty);` before the return statement does work.
Comment #1 by dlang-bot — 2024-05-25T11:00:12Z
@ntrel created dlang/dlang.org pull request #3836 "[spec/function] Improve contract docs" fixing this issue: - [spec/function] Improve contract docs Move in/out grammar to respective subheadings. Use list for contract expression arguments. Fix grammar for invalid state sentences. Fix wording of postcondition best practice. Mention postcondition identifier is implicitly const. Add anchor for example subheading. Fix Bugzilla 24565 - out contract variable is implicitly const https://github.com/dlang/dlang.org/pull/3836
Comment #2 by timon.gehr — 2024-05-25T11:22:12Z
FWIW, I would prefer fixing the implementation instead of documenting this annoying behavior that makes contracts less useful.
Comment #3 by dlang-bot — 2024-05-28T12:36:45Z
dlang/dlang.org pull request #3836 "[spec/function] Improve contract docs" was merged into master: - 6ee6e8cf6d997803145ce81415bab16cb95841c0 by Nick Treleaven: [spec/function] Improve contract docs Move in/out grammar to respective subheadings. Use list for contract expression arguments. Fix grammar for invalid state sentences. Fix wording of postcondition best practice. Mention postcondition identifier is implicitly const. Add anchor for example subheading. Fix Bugzilla 24565 - out contract variable is implicitly const https://github.com/dlang/dlang.org/pull/3836