www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3021] New: D1 CTFE and enums produces error '... constant expression expected'

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

           Summary: D1 CTFE and enums produces error '... constant
                    expression expected'
           Product: D
           Version: 1.044
          Platform: PC
               URL: http://www.digitalmars.com/d/1.0/enum.html
        OS/Version: All
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: gide nwawudu.com


Using CTFE and enums produces an error in D1, both examples compile in D2.

test1.d
------
int b() { return 10; }
enum { a = b() }

C:>dmd test1.d
test1.d(2): Error: Integer constant expression expected instead of b()


test2.d
------
int b() { return 10; }
enum { a = b }

C:>dmd test2.d
test2.d(2): Error: cannot implicitly convert expression (b) of type int() to
int
test2.d(2): Error: Integer constant expression expected instead of cast(int)b


The following is OK in D1.
int b() { return 10; }
const int x = b();
enum { a = x }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 23 2009
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3021


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                 CC|                            |clugdbug yahoo.com.au




--- Comment #1 from Don <clugdbug yahoo.com.au>  2009-08-25 04:18:40 PDT ---
Patch for the first case: enum.c, EnumDeclaration::semantic, line 109

        assert(e->dyncast() == DYNCAST_EXPRESSION);
        e = e->semantic(sce);
-        e = e->optimize(WANTvalue);
+        e = e->optimize(WANTvalue | WANTinterpret);
        // Need to copy it because we're going to change the type
        e = e->copy();
        e = e->implicitCastTo(sc, memtype);
-        e = e->optimize(WANTvalue);
+        e = e->optimize(WANTvalue | WANTinterpret);
        number = e->toInteger();
        e->type = t;

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