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);
}
---
Comment #1 by bearophile_hugs — 2012-05-08T04:12:12Z
(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.
Comment #2 by oli_r — 2018-05-05T12:34:15Z
The output starting from 2.0.64 includes only the "Bad type size: 8"-message.
Resolving.