Thread overview
[Issue 4177] New: __ctfe can't be used in pure functions
Jun 08, 2010
Don
Aug 28, 2010
Walter Bright
May 12, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4177

           Summary: __ctfe can't be used in pure functions
           Product: D
           Version: future
          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


--- Comment #0 from bearophile_hugs@eml.cc 2010-05-12 04:09:05 PDT ---
To define a std.math.log() function that works at compile time (see bug 3749 )
it can be used __ctfe, but there are problems:


pure real log(real x) {
    if (__ctfe)
        return 0.0;
    else
        return 1.0;
}
enum x = log(4.0);
void main() {}


dmd v2.045 prints:

test.d(2): Error: variable __ctfe forward referenced
test.d(2): Error: pure nested function 'log' cannot access mutable data
'__ctfe'
test.d(7): Error: cannot evaluate log(4L) at compile time
test.d(7): Error: cannot evaluate log(4L) at compile time


I'd like __ctfe to work in pure functions too.
I think it can be done because it's an immutable value that I think can't break
the purity of the function/method, even if as in that example the function can
give different outouts at compile and run time.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 08, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4177


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                 CC|                            |clugdbug@yahoo.com.au
            Version|future                      |2.041


--- Comment #1 from Don <clugdbug@yahoo.com.au> 2010-06-08 11:38:37 PDT ---
Since __ctfe is so magical and unique, it seems justified to give it one more special case. Other approaches I tried (changing the storage_class of __ctfe) were far more complicated.

PATCH expression.c, VarExp::semantic(), line 4397.

+            /* Magic variable __ctfe never violates pure or safe
+             */
+            if (v->ident == Id::ctfe)
+                return this;

            /* If ANY of its enclosing functions are pure,
             * it cannot do anything impure.
             * If it is pure, it cannot access any mutable variables other
             * than those inside itself
             */

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 28, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4177


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> 2010-08-28 14:55:16 PDT ---
http://www.dsource.org/projects/dmd/changeset/646

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