Thread overview
[Issue 8063] New: Purity of assert's second parameter
May 08, 2012
Nicolas Sicard
May 08, 2012
Nicolas Sicard
May 08, 2012
Jonathan M Davis
May 08, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8063

           Summary: Purity of assert's second parameter
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: dransic@gmail.com


--- Comment #0 from Nicolas Sicard <dransic@gmail.com> 2012-05-08 02:39:16 PDT ---
Calling impure functions in the second parameter of an assert statement within the body of a pure pure is an error, even in release mode. Since such a call is just about displaying an error message, should purity be checked here? Idem for static asserts.  Maybe it is the same for 'nothrowness' (I didn't try).

Example:
---
import std.conv : text; // text is not pure

pure int foo(int value) {
    assert(value <= 10, text(value, " is greater than ", 10)); // Error: pure
function 'foo' cannot call impure function 'text'
    return value;
}

pure int foo2(int value) {
    debug assert(value <= 10, text(value, " is greater than ", 10)); // OK
    return value;
}

pure T bar(T)(T value) {
    static assert(T.sizeof == 4, text("Bad type size: ", T.sizeof)); // Error:
pure function 'bar' cannot call impure function 'text'
    return value;
}

void main() {
    int f = foo(42);
    int f2 = foo2(42);
    auto b = bar(42L);
}
---

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


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #1 from bearophile_hugs@eml.cc 2012-05-08 04:12:12 PDT ---
(In reply to comment #0)
> Calling impure functions in the second parameter of an assert statement within the body of a pure pure is an error, even in release mode.

This is good.


> Idem for static asserts.

For static arrays I think it will be OK to call impure functions in the message part.



>     assert(value <= 10, text(value, " is greater than ", 10)); // Error: pure
> function 'foo' cannot call impure function 'text'

The solution is to have a pure text(), not to compromise on purity.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 08, 2012
> --- Comment #1 from bearophile_hugs@eml.cc 2012-05-08 04:12:12 PDT ---
> (In reply to comment #0)
>> Calling impure functions in the second parameter of an assert statement within
>> the body of a pure pure is an error, even in release mode.
>
> This is good.

Since it is allowed to call impure functions in debug blocks inside pure functions, I thought it would be coherent that assert could do the same.

May 08, 2012
On Tuesday, May 08, 2012 15:08:42 Nicolas Sicard wrote:
> > --- Comment #1 from bearophile_hugs@eml.cc 2012-05-08 04:12:12
> > PDT ---
> > (In reply to comment #0)
> > 
> >> Calling impure functions in the second parameter of an assert
> >> statement within
> >> the body of a pure pure is an error, even in release mode.
> > 
> > This is good.
> 
> Since it is allowed to call impure functions in debug blocks inside pure functions, I thought it would be coherent that assert could do the same.

Please don't post directly to the bug list. Respond in bugzilla please. The buglist if for receiving reports from bugzilla not for posting to directly.

- Jonathan M Davis