Bug 7401 – Pure contracts Unnecessarily strict

Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-01-30T14:23:53Z
Last change time
2021-01-24T06:59:33Z
Assigned to
No Owner
Creator
Era Scarecrow

Comments

Comment #0 by rtcvb32 — 2012-01-30T14:23:53Z
The compiler complaining to me that my function isn't 'pure' by calling a non-pure function, specifically to!string(). However the unpure functions used are only accessed in the contracts (and only if it failed). The contracts shouldn't be considered as part of the pure contract. This is because they are totally excluded during the release builds (and shouldn't have any side effects anyways). Error: pure function 'offset' cannot call impure function 'to' struct X { int size; ... const pure int offset(int field) out(o) { assert(o >= 0, "Negative value! Check structure:" ~ to!string(size) ~ "\n"); } body { ... } }
Comment #1 by bearophile_hugs — 2012-01-30T14:43:21Z
See also issue 7224 (that asks kind of the opposite)
Comment #2 by rtcvb32 — 2012-01-30T14:55:06Z
(In reply to comment #1) > See also issue 7224 (that asks kind of the opposite) In his remark, it seems rather silly. Of course it won't raise an exception when it's in release mode, the in contract won't even be present. However with the pure contract, I don't want to have to encompass a debug{} block to show my assert details. Then I need the -debug flag on as well otherwise it won't be checked, unless you go around it... in { string message = "use -debug or pure gets in the way"; debug { message = to!string(); } assert(something, x); } or in { debug { assert(something, "with message"); } assert(something); //same assert as above but doesn't give a useful message. }
Comment #3 by bearophile_hugs — 2012-01-30T15:07:54Z
I think the right solution for this problem is to wait for to!string(int) to become pure.
Comment #4 by maxhaton — 2021-01-24T06:59:33Z
Exact std.conv has been fixed; debug statements are a reasonable compromise.