HuubGitHub / dwscript

Automatically exported from code.google.com/p/dwscript
1 stars 0 forks source link

Overflow in flags (enum) #240

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Code:

Type TEnum = Flags (f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12,
                    f13, f14, f15, f16, f17, f18, f19, f20, f21, f22,
                    f23, f24, f25, f26, f27, f28, f29, f30, f31, f32); // Bits-field is overflowed (for 32Bit), error message has been expected

PrintLn(TEnum.f32); // Output: 0. DWS-integer 64bit, why enum flags has limit 
32 elements?

Original issue reported on code.google.com by kazantse...@mail.ru on 24 Mar 2012 at 2:02

GoogleCodeExporter commented 8 years ago
This issue was closed by revision r1373.

Original comment by zar...@gmail.com on 26 Mar 2012 at 3:09

GoogleCodeExporter commented 8 years ago
and still:

Type TEnum = flags (e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13,
                    e14, e15, e16, e17, e18, e19, e20, e21, e22, e23, e24, e25,
                    e26, e27, e28, e29, e30, e31, e32, e33, e34, e35, e36, e37,
                    e38, e39, e40, e41, e42, e43, e44, e45, e46, e47, e48, e49,
                    e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61); // High(TEnum) = 0

PrintLn(High(TEnum)); // Output: 0

Type TEnum2 = flags (e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13,
                     e14, e15, e16, e17, e18, e19, e20, e21, e22, e23, e24, e25,
                     e26, e27, e28, e29, e30, e31, e32, e33, e34, e35, e36, e37,
                     e38, e39, e40, e41, e42, e43, e44, e45, e46, e47, e48, e49,
                     e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61, e62); // 63 elements only

// Syntax Error: Enumeration element overflow [line: 13, column: 82]

Original comment by kazantse...@mail.ru on 26 Mar 2012 at 3:44

GoogleCodeExporter commented 8 years ago

Original comment by zar...@gmail.com on 26 Mar 2012 at 3:48

GoogleCodeExporter commented 8 years ago
Fixed off-by-one error

Original comment by zar...@gmail.com on 27 Mar 2012 at 10:11

GoogleCodeExporter commented 8 years ago
Code:

Type TEnum = flags (e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13,
                    e14, e15, e16, e17, e18, e19, e20, e21, e22, e23, e24, e25,
                    e26, e27, e28, e29, e30, e31, e32, e33, e34, e35, e36, e37,
                    e38, e39, e40, e41, e42, e43, e44, e45, e46, e47, e48, e49,
                    e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61, e62, e63); // 64 elements

Output:

Syntax Error: Enumeration element overflow [line: 5, column: 86]

Original comment by kazantse...@mail.ru on 27 Mar 2012 at 12:55

GoogleCodeExporter commented 8 years ago
Yes, Integers are signed, so 2^64 can't be expressed.

One reason being that one would expect e0 < e48, e32 < e62, etc.
If 2^64 was allowed, the above couldn't be verified without introducing 
unsigned integers as well.

Original comment by zar...@gmail.com on 27 Mar 2012 at 2:50

GoogleCodeExporter commented 8 years ago
then other:

Type TEnum = flags (e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13,
                    e14, e15, e16, e17, e18, e19, e20, e21, e22, e23, e24, e25,
                    e26, e27, e28, e29, e30, e31, e32, e33, e34, e35, e36, e37,
                    e38, e39, e40, e41, e42, e43, e44, e45, e46, e47, e48, e49,
                    e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61, e62);

PrintLn(TEnum.e62);
PrintLn(High(TEnum));

Output:

4611686018427387904
0

Original comment by kazantse...@mail.ru on 27 Mar 2012 at 2:58

GoogleCodeExporter commented 8 years ago
Oops, bounds were still 32bits, should now be 64

Original comment by zar...@gmail.com on 27 Mar 2012 at 3:16