D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 8063 - Purity of assert's second parameter
Summary: Purity of assert's second parameter
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-05-08 02:39 UTC by dransic
Modified: 2018-05-05 12:34 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 dransic 2012-05-08 02:39:16 UTC
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 bearophile_hugs 2012-05-08 04:12:12 UTC
(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 Oliver Rümpelein 2018-05-05 12:34:15 UTC
The output starting from 2.0.64 includes only the "Bad type size: 8"-message.
Resolving.