D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2998 - ICE(expression.c) with floating point enum
Summary: ICE(expression.c) with floating point enum
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: ice-on-valid-code, patch
Depends on:
Blocks:
 
Reported: 2009-05-17 03:46 UTC by Shin Fujishiro
Modified: 2015-06-09 01:27 UTC (History)
2 users (show)

See Also:


Attachments
enum.c for DMD2.032 (9.30 KB, patch)
2009-09-02 00:33 UTC, Don
Details | Diff
enum.c for DMD2.032 (9.20 KB, text/plain)
2009-09-03 00:05 UTC, Don
Details

Note You need to log in before you can comment on or make changes to this issue.
Description Shin Fujishiro 2009-05-17 03:46:37 UTC
--------------------
enum E : real { a, b }
--------------------
assert expression.c(1392) 0
--------------------

This error does not occur when all members are explicitly initialized, or there is only one member in the enum.
Comment 1 Don 2009-09-02 00:32:14 UTC
PATCH: enum.c, in EnumDeclaration::semantic(), around line 195.
The interpret optimisation should be done BEFORE the cast. Just swap the order.
Otherwise, constfold functions such as Add() think it's an enum type, not a real, and so they default to integer, and chaos ensues.

	    // Now set e to (elast + 1)
	    e = new AddExp(em->loc, elast, new IntegerExp(em->loc, 1, Type::tint32));
	    e = e->semantic(sce);
+	    e = e->optimize(WANTvalue | WANTinterpret);
	    e = e->castTo(sce, elast->type);
-	    e = e->optimize(WANTvalue | WANTinterpret);


----
However, there are other problems in this function. If you try to use a struct inside an enum, you get garbage error messages without line number; you find that you need to define a .max() property for the struct,  and error messages are repeated. I attach a revised enum.c which fixes these problems, and allows the code below to work correctly:

enum G : real { a, b }
enum E : real { a=18.0, b }
enum F : real { c=E.b, d }

struct S{
   int x;
   S opAdd(int q) { return S(x+1);} 
   int opCmp(S s) { return x < s.x; }
}
enum H : S { a=S(0), b}
Comment 2 Don 2009-09-02 00:33:56 UTC
Created attachment 443 [details]
enum.c for DMD2.032

Fixes many of the problems with enums. Tested with the pre-release DMD2.032 beta.
D2 only.
Comment 3 Don 2009-09-03 00:05:58 UTC
Created attachment 444 [details]
enum.c for DMD2.032

Revised enum.c. The version I posted was incorrect, and failed one of the test suite tests. This version passes.
Comment 4 Walter Bright 2009-10-01 10:42:10 UTC
The order in which those two functions are called shouldn't matter. The actual problem is the TypeEnum doesn't have proper overrides for isreal, isimaginary, etc. Will fix.
Comment 5 Walter Bright 2009-10-06 02:20:50 UTC
Fixed dmd 2.033