Bug 250 – enum : bool allowed with odd results

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2006-07-12T17:12:00Z
Last change time
2014-02-15T13:19:41Z
Keywords
accepts-invalid
Assigned to
bugzilla
Creator
jpelcis

Comments

Comment #0 by jpelcis — 2006-07-12T17:12:25Z
module test; import std.stdio; enum Bool : bool { False, True, Unknown } void main () { writefln("The maximum value of bool is %d.", bool.max); writefln("The maximum value of Bool is %d.", cast(int)Bool.max); writefln(" Also, should that cast be necessary?\n"); writefln("Using 2 as a bool gives %d.", cast(bool)2); writefln("Using 2 as a Bool gives %d.", cast(int)cast(Bool)2); writefln("Using Bool.Unknown as an int gives %d.", cast(int)Bool.Unknown); writefln("Using Bool.Unknown as a bool gives %d.", cast(bool)Bool.Unknown); } ----------------------------- Running this gives me the following data: ----------------------------- The maximum value of bool is 1. The maximum value of Bool is 1. Also, should that cast be necessary? Using 2 as a bool gives 1. Using 2 as a Bool gives 1. Using Bool.Unknown as an int gives 1. Using Bool.Unknown as a bool gives 2. ----------------------------- This is either accepts-invalid or wrong-code, depending on whether enum : bool is legal. Also, I must admit to being curious why casting Bool.Unknown to an int gives 1 while casting it to a bool gives 2. Is cast ignored for enum to base type?
Comment #1 by jpelcis — 2006-07-18T16:33:55Z
Fixed DMD 0.163. enum : bool can only have two values.
Comment #2 by thomas-dloop — 2006-08-15T04:59:17Z
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 [email protected] schrieb am 2006-07-12: > http://d.puremagic.com/issues/show_bug.cgi?id=250 > module test; > > import std.stdio; > > enum Bool : bool { > False, > True, > Unknown > } > > void main () { > writefln("The maximum value of bool is %d.", bool.max); > writefln("The maximum value of Bool is %d.", cast(int)Bool.max); > writefln(" Also, should that cast be necessary?\n"); > > writefln("Using 2 as a bool gives %d.", cast(bool)2); > writefln("Using 2 as a Bool gives %d.", cast(int)cast(Bool)2); > writefln("Using Bool.Unknown as an int gives %d.", > cast(int)Bool.Unknown); > writefln("Using Bool.Unknown as a bool gives %d.", > cast(bool)Bool.Unknown); > } > > ----------------------------- > > Running this gives me the following data: > > ----------------------------- > > The maximum value of bool is 1. > The maximum value of Bool is 1. > Also, should that cast be necessary? > > Using 2 as a bool gives 1. > Using 2 as a Bool gives 1. > Using Bool.Unknown as an int gives 1. > Using Bool.Unknown as a bool gives 2. > > ----------------------------- > > This is either accepts-invalid or wrong-code, depending on whether enum : bool > is legal. > > Also, I must admit to being curious why casting Bool.Unknown to an int gives 1 > while casting it to a bool gives 2. Is cast ignored for enum to base type? Added to DStress as http://dstress.kuehne.cn/nocompile/e/enum_46_A.d http://dstress.kuehne.cn/compile/e/enum_46_B.d http://dstress.kuehne.cn/compile/e/enum_46_C.d http://dstress.kuehne.cn/compile/e/enum_46_D.d http://dstress.kuehne.cn/compile/e/enum_46_E.d Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFE4aPDLK5blCcjpWoRAn4DAKCWFZpEASGgBc4UiH0zA84U1utXLQCgqAHi SfY3oLAt6lt8DpcJwBlei0w= =n0+o -----END PGP SIGNATURE-----