Thread overview
[Issue 8637] New: Enforcement and purity
Sep 10, 2012
Kenji Hara
Sep 10, 2012
Kenji Hara
Sep 10, 2012
Jonathan M Davis
Sep 10, 2012
Kenji Hara
Sep 10, 2012
Dmitry Olshansky
September 10, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8637

           Summary: Enforcement and purity
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: monarchdodra@gmail.com


--- Comment #0 from monarchdodra@gmail.com 2012-09-10 02:58:42 PDT ---
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...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 10, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8637


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|unspecified                 |D2


--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> 2012-09-10 08:14:02 PDT ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 10, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8637



--- Comment #2 from monarchdodra@gmail.com 2012-09-10 08:43:02 PDT ---
If that is the fix, I can make the pull request. Do you want me to do it, or are you on it?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 10, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8637



--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> 2012-09-10 08:49:24 PDT ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 10, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8637


monarchdodra@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
         AssignedTo|nobody@puremagic.com        |monarchdodra@gmail.com


--- Comment #4 from monarchdodra@gmail.com 2012-09-10 08:57:48 PDT ---
Will commit fix myself then.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 10, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8637


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmx.com


--- Comment #5 from Jonathan M Davis <jmdavisProg@gmx.com> 2012-09-10 09:07:50 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 10, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8637



--- Comment #6 from Kenji Hara <k.hara.pg@gmail.com> 2012-09-10 09:15:20 PDT ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 10, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8637


Dmitry Olshansky <dmitry.olsh@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dmitry.olsh@gmail.com


--- Comment #7 from Dmitry Olshansky <dmitry.olsh@gmail.com> 2012-09-10 12:44:17 PDT ---
Sorry, it had regex somewhere in description so I jumped in ;)

https://github.com/D-Programming-Language/phobos/pull/783

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 10, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8637



--- Comment #8 from monarchdodra@gmail.com 2012-09-10 13:59:53 PDT ---
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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 11, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8637


monarchdodra@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED


--- Comment #9 from monarchdodra@gmail.com 2012-09-11 10:23:36 PDT ---
Fixed by Dmitry in https://github.com/D-Programming-Language/phobos/pull/783

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------