www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7994] New: Impure mixin generator of pure code inside pure functions too

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7994

           Summary: Impure mixin generator of pure code inside pure
                    functions too
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 26 2012
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7994


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies gmail.com
         Resolution|                            |DUPLICATE



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

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