Bug 8637 – Enforcement and purity

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-09-10T02:58:00Z
Last change time
2012-09-14T19:12:04Z
Assigned to
monarchdodra
Creator
monarchdodra

Comments

Comment #0 by monarchdodra — 2012-09-10T02:58:42Z
The argument taken by enforce must be "castable to bool", so the the implementation can do the cast. However, enforce is declared pure, so if the cast operator is not pure, the compilation fails: -------- import std.regex; import std.exception; void main() { auto m = match("hello world", regex("world")); assert(m); //<- Fine enforce(m); //Here } -------- Error: pure function 'enforce' cannot call impure function '~this' Error: pure function 'enforce' cannot call impure function 'opCast' -------- The messages come from: -------- T enforce(T)(T value, lazy const(char)[] msg = null, string file = __FILE__, size_t line = __LINE__) @safe pure { if (!value) bailOut(file, line, msg); return value; } -------- "if(!value)": This makes an impure call to opCast. "enforce(T)(T value..." This uses pass by value, and makes an impure call to the destructor I have no idea what a good fix would be. Regarding pass by value, wouldn't this be a textbook example of using "auto ref" with "auto return"? I have no idea...
Comment #1 by k.hara.pg — 2012-09-10T08:14:02Z
(In reply to comment #0) > The argument taken by enforce must be "castable to bool", so the the > implementation can do the cast. However, enforce is declared pure, so if the > cast operator is not pure, the compilation fails: [snip] > -------- > Error: pure function 'enforce' cannot call impure function '~this' > Error: pure function 'enforce' cannot call impure function 'opCast' > -------- The cause might be the explicit annotation with pure. > The messages come from: > > -------- > T enforce(T)(T value, lazy const(char)[] msg = null, string file = __FILE__, > size_t line = __LINE__) @safe pure > { > if (!value) bailOut(file, line, msg); > return value; > } > -------- > "if(!value)": This makes an impure call to opCast. > > "enforce(T)(T value..." This uses pass by value, and makes an impure call to > the destructor > > I have no idea what a good fix would be. Regarding pass by value, wouldn't this > be a textbook example of using "auto ref" with "auto return"? I have no idea... The strict pure annotation is introduced by the pull #263. https://github.com/D-Programming-Language/phobos/pull/263 As you can see, the pure annotations were just for the documentation. At this point, impure destructor had not been considered at all in the discussion. Then, now, the pure annotation causes this problem, so I think we should remove it and rely on the pure attribute inference.
Comment #2 by monarchdodra — 2012-09-10T08:43:02Z
If that is the fix, I can make the pull request. Do you want me to do it, or are you on it?
Comment #3 by k.hara.pg — 2012-09-10T08:49:24Z
(In reply to comment #2) > If that is the fix, I can make the pull request. Do you want me to do it, or > are you on it? Of course I would welcome your contribution.
Comment #4 by monarchdodra — 2012-09-10T08:57:48Z
Will commit fix myself then.
Comment #5 by issues.dlang — 2012-09-10T09:07:50Z
Both the annotation for pure and @safe need to go, because T's destructor and opCast could be impure or non-@safe. When the explicit annotations were added, clearly the destructor and opCast were not taken into account, since they're not called directly and are easily forgotten.
Comment #6 by k.hara.pg — 2012-09-10T09:15:20Z
(In reply to comment #5) > Both the annotation for pure and @safe need to go, because T's destructor and > opCast could be impure or non-@safe. When the explicit annotations were added, > clearly the destructor and opCast were not taken into account, since they're > not called directly and are easily forgotten. Oh, that's right. We should remove both 'pure' and '@safe', and need to depends on fully attribute inference.
Comment #7 by dmitry.olsh — 2012-09-10T12:44:17Z
Sorry, it had regex somewhere in description so I jumped in ;) https://github.com/D-Programming-Language/phobos/pull/783
Comment #8 by monarchdodra — 2012-09-10T13:59:53Z
Darn! Kidding asside, thanks. BTW, I just made touch on std.regex itself. Seems like you may have wanted to know: https://github.com/D-Programming-Language/phobos/pull/784
Comment #9 by monarchdodra — 2012-09-11T10:23:36Z
Comment #10 by github-bugzilla — 2012-09-14T19:12:04Z