D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7401 - Pure contracts Unnecessarily strict
Summary: Pure contracts Unnecessarily strict
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 minor
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-01-30 14:23 UTC by Era Scarecrow
Modified: 2021-01-24 06:59 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 Era Scarecrow 2012-01-30 14:23:53 UTC
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 bearophile_hugs 2012-01-30 14:43:21 UTC
See also issue 7224  (that asks kind of the opposite)
Comment 2 Era Scarecrow 2012-01-30 14:55:06 UTC
(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 bearophile_hugs 2012-01-30 15:07:54 UTC
I think the right solution for this problem is to wait for  to!string(int)  to become pure.
Comment 4 mhh 2021-01-24 06:59:33 UTC
Exact std.conv has been fixed; debug statements are a reasonable compromise.