D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7994 - Impure mixin generator of pure code inside pure functions too
Summary: Impure mixin generator of pure code inside pure functions too
Status: RESOLVED DUPLICATE of issue 6169
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2012-04-26 17:33 UTC by bearophile_hugs
Modified: 2012-04-27 22:02 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2012-04-26 17:33:22 UTC
This is yet another small refinement for the implementation of purity in D (I think this is more than an enhancement request, because I think the second example is supposed to work in a good purity implementation).

This is wrong D2 code:

import std.stdio;
string foo() pure {
    return `writeln("hello");`;
}
void main() pure {
    mixin(foo());
}


And the error message is correct because mixin(foo) causes main() call an impure function (DMD 2.060alpha):

test.d(6): Error: pure function 'main' cannot call impure function 'writeln'


But in this case:


string foo() {
    import std.string;
    return xformat("%d", 1);
}
void main() pure {
    int x = mixin(foo());
}


It gives:
test.d(6): Error: pure function 'main' cannot call impure function 'foo'


This time the error message is wrong, because desite foo() itself is not pure, mixin() calls it at compile-time, and the resulting main is equivalent to this, that is pure and correct:

void main() pure {
    int x = 1;
}

So mixin() needs to be allowed to call impure functions too, even inside pure functions, as long as the resulting mixed-in code is pure.

This is quite handy, because the generation of text to mix-in often calls functions like xformat and text that currently are not pure.
Comment 1 yebblies 2012-04-27 22:02:06 UTC

*** This issue has been marked as a duplicate of issue 6169 ***