Bug 15366 – Enum typed as bool behaves as bool even when cast

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-11-20T05:16:34Z
Last change time
2020-03-21T03:56:35Z
Keywords
pull, rejects-valid
Assigned to
No Owner
Creator
Lionello Lunesu

Comments

Comment #0 by lio+bugzilla — 2015-11-20T05:16:34Z
enum Enum : bool { A, B }; struct I{ void func(Enum e); void func2(Enum a, Enum b) {func(cast(Enum)(a&&b));} //line 4 } test.d(4): Error: function test.I.func (Enum e) is not callable using argument types (bool) There are many changes to this code that will get rid of the error: * placing any one, or both, of the functions on global scope * using bitwise & instead of logic && * removing the type from the enum, or typing as byte/int/...
Comment #1 by b2.temp — 2015-11-20T21:09:59Z
That's funny because this compiles: --- enum Enum : bool { A, B } struct I{ void func(Enum e){} void func4(Enum a, Enum b) { (&func)(cast(Enum)(a && b)); } void func3(Enum a, Enum b) { Enum aa = cast(Enum)(a && b); func(aa); } } --- the error message seems totally meaningless when you consider func4. func3 suggests that the real problem is that it needs a lvalue.
Comment #2 by lio+bugzilla — 2015-11-21T11:14:33Z
Adding an explicit "this." fixed the issue: enum Enum : bool { A, B }; struct I{ void func(Enum e); void func2(Enum a, Enum b) {this.func(cast(Enum)(a&&b));} } Something happens when the compiler adds the "this" in your behalf. The resolution changes.
Comment #3 by k.hara.pg — 2015-11-21T14:25:57Z
Comment #4 by github-bugzilla — 2015-11-21T16:41:17Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/67a21a9ec222e63ad5bab445662de9114878bc90 fix Issue 15366 - Enum typed as bool behaves as bool even when cast From a CastExp 'a && b', its `semantic` returns an `AndAndExp` with the 'Enum' type. But if its `semantic` will be called once more, it will repaint its `type` to 'bool'. Same problem exists in `OrOrExp`. https://github.com/D-Programming-Language/dmd/commit/913ffbe000e99e3a2aa6caaea9994a103503d6ac Merge pull request #5279 from 9rnsr/fix15366 Issue 15366 - Enum typed as bool behaves as bool even when cast
Comment #5 by github-bugzilla — 2016-01-03T14:02:31Z