View mode: basic / threaded / horizontal-split · Log in · Help
June 17, 2011
[Issue 6169] New: [CTFE] pure functions cannot compute constants using functions not marked as pure
http://d.puremagic.com/issues/show_bug.cgi?id=6169

          Summary: [CTFE] pure functions cannot compute constants using
                   functions not marked as pure
          Product: D
          Version: D2
         Platform: All
       OS/Version: All
           Status: NEW
         Keywords: rejects-valid
         Severity: normal
         Priority: P2
        Component: DMD
       AssignedTo: nobody@puremagic.com
       ReportedBy: timon.gehr@gmx.ch


--- Comment #0 from timon.gehr@gmx.ch 2011-06-16 17:09:25 PDT ---
With DMD 2.053:

string impure(){return ";";}

void main() pure{
   enum s = impure(); // fail (cannot call impure function 'impure')
   mixin(impure());   // ditto
}

Removing the pure attribute from 'main' or adding it to 'impure' makes the code
pass.
This restriction is nonsensical and should be removed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 17, 2011
[Issue 6169] [CTFE] pure functions cannot compute constants using functions not marked as pure
http://d.puremagic.com/issues/show_bug.cgi?id=6169


bearophile_hugs@eml.cc changed:

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


--- Comment #1 from bearophile_hugs@eml.cc 2011-06-16 18:42:49 PDT ---
I think you have just made D a bit more complex :-)

In D compile time and run time are fully separated (and computing an enum
inside a CTFE function spans a fully separated and fully enclosed
sub-computation), so there is no way compile-time constants can break the
purity of a pure function. So this looks OK regarding run-time purity.

Currently in D in a function the path of compile-time execution has to be pure,
even if the whole function is not pure, so you are right saying this program
has to compile:


int x = 10;
int foo(bool b) {
   if (b)
       x++;
   return 0;
}
pure void main() {
   enum y = foo(false);
}


If in future D compilers CTFE will be allowed to modify global variables too,
then I think your idea is in troubles. Otherwise at first sight it seems OK,
but more thinking is required because future D compilers are allowed to perform
pure-related optimizations on pure functions even in CTFE. Is nonpurity able to
cause troubles to pure functions at compile-time?

In this program spam is pure, and it calls bar, that's not pure, to compute z.
But this program doesn't cause troubles even if a smart D compiler applies pure
optimization of spam()+spam() replacing it with spam()*2 because z is computed
only once, because bar() is called in a sub-computation that's fully sealed:


pure nothrow int foo() {
   int x = 1;
   nothrow int bar(int y) { // nonpure
       x++;
       return x + y;
   }

   pure nothrow int spam() {
       enum z = bar(1); // calls a nonpure
       return z;
   }
   return spam() + spam();
}

enum r = foo();
void main() {}


So if I am right, then this proposal is safe :-)

One problem left is that this proposal introduces another special case in D,
because the rules of purity have to say a pure function is allowed to call an
impure one at compile-time. Is it worth it? I think it's acceptable.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 17, 2011
[Issue 6169] [CTFE] pure functions cannot compute constants using functions not marked as pure
http://d.puremagic.com/issues/show_bug.cgi?id=6169


Don <clugdbug@yahoo.com.au> changed:

          What    |Removed                     |Added
----------------------------------------------------------------------------
                CC|                            |clugdbug@yahoo.com.au


--- Comment #2 from Don <clugdbug@yahoo.com.au> 2011-06-17 01:02:22 PDT ---
Applies to @safe as well.
CTFE enforces safety and purity, using more relaxed rules than @safe and pure
do.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 17, 2011
[Issue 6169] [CTFE] pure functions cannot compute constants using functions not marked as pure
http://d.puremagic.com/issues/show_bug.cgi?id=6169



--- Comment #3 from timon.gehr@gmx.ch 2011-06-17 06:57:25 PDT ---
Interestingly, it does not apply to nothrow unless interpreting the function
fails :). I think this is a diagnostics bug:

int foo(){return 0;}
int bar(){assert(0);}
void main() nothrow{
   enum f = foo(); // fine
   enum b = bar(); // assert(0) failed / bar is not nothrow 
                   // main is nothrow yet may throw
}



(In reply to comment #1)
> ...
> If in future D compilers CTFE will be allowed to modify global variables too,
> then I think your idea is in troubles.
> [snip.]

I think letting CTFE mutate static storage is a bad idea anyways, but actually
that does not matter for this. You are just computing some manifest constant
during compile time that is later used to influence the function's behavior.
This cannot possibly make the function return different results when passed the
same arguments (even during compile time), ergo it is still pure.

> ...
> One problem left is that this proposal introduces another special case in D,
> because the rules of purity have to say a pure function is allowed to call an
> impure one at compile-time. Is it worth it? I think it's acceptable.
> [snip.]

Actually I think this is the same 'special case' as the one that says manifest
constants are evaluated during compile time. The behavior we have now is a
special case of this 'special case'. I think we remove some special casing (and
therefore complexity) by fixing this.

Oh, and this should work too:
string impure(){return ";";}
void main() pure{static s = impure();}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 29, 2012
[Issue 6169] [CTFE] pure functions cannot compute constants using functions not marked as pure
http://d.puremagic.com/issues/show_bug.cgi?id=6169


yebblies <yebblies@gmail.com> changed:

          What    |Removed                     |Added
----------------------------------------------------------------------------
          Keywords|                            |patch
                CC|                            |yebblies@gmail.com
        AssignedTo|nobody@puremagic.com        |yebblies@gmail.com


--- Comment #4 from yebblies <yebblies@gmail.com> 2012-01-29 15:27:06 EST ---
https://github.com/D-Programming-Language/dmd/pull/652

Please test and review if you're interested.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 28, 2012
[Issue 6169] [CTFE] pure functions cannot compute constants using functions not marked as pure
http://d.puremagic.com/issues/show_bug.cgi?id=6169



--- Comment #5 from yebblies <yebblies@gmail.com> 2012-04-28 15:02:07 EST ---
*** Issue 7994 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 31, 2013
[Issue 6169] [CTFE] pure functions cannot compute constants using functions not marked as pure
http://d.puremagic.com/issues/show_bug.cgi?id=6169


monarchdodra@gmail.com changed:

          What    |Removed                     |Added
----------------------------------------------------------------------------
                CC|                            |monarchdodra@gmail.com


--- Comment #6 from monarchdodra@gmail.com 2013-01-31 02:27:40 PST ---
Just wanted to add that I just hit this bug. Any news on the progression of
this bug fix?

I'm hitting this on a rather trivial use case, where I'm just trying to
generate a compile-time-known error message:

//----
import std.string : format;

struct S
{
   int* p;
   ref inout(int) get() inout nothrow pure @safe
   {
       enum message = format("Called %s on null %s.", "get", S.stringof);
       assert(p, message);
       return *p;
   }
}
//----

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 31, 2013
[Issue 6169] [CTFE] pure functions cannot compute constants using functions not marked as pure
http://d.puremagic.com/issues/show_bug.cgi?id=6169



--- Comment #7 from yebblies <yebblies@gmail.com> 2013-01-31 23:03:40 EST ---
This bug is exactly where it has been for the last year - awaiting approval
from Walter.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 17, 2013
[Issue 6169] [CTFE] pure functions cannot compute constants using functions not marked as pure
http://d.puremagic.com/issues/show_bug.cgi?id=6169


yebblies <yebblies@gmail.com> changed:

          What    |Removed                     |Added
----------------------------------------------------------------------------
                CC|                            |hsteoh@quickfur.ath.cx


--- Comment #8 from yebblies <yebblies@gmail.com> 2013-02-17 12:47:19 EST ---
*** Issue 9517 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 04, 2013
[Issue 6169] [CTFE] pure functions cannot compute constants using functions not marked as pure
http://d.puremagic.com/issues/show_bug.cgi?id=6169



--- Comment #9 from github-bugzilla@puremagic.com 2013-05-04 09:48:17 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/42d1af1635fec1c43bbda6c4a3894e148ca6fc22
Fix Issue 6169 - [CTFE] pure functions cannot compute constants using functions
not marked as pure

When running semantic on an expression used anywhere that forces compile time
evaluation, use a scope flag to prevent purity and safety checks on function
calls.
This allows better purity/safety inferrence as well.

https://github.com/D-Programming-Language/dmd/commit/15a57832d0a9ed18eb619c58261db6c2eedc663e
Merge pull request #652 from yebblies/issue6169

Issue 6169 - [CTFE] pure functions cannot compute constants using functions not
marked as pure

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home