D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 15366 - Enum typed as bool behaves as bool even when cast
Summary: Enum typed as bool behaves as bool even when cast
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull, rejects-valid
Depends on:
Blocks:
 
Reported: 2015-11-20 05:16 UTC by Lionello Lunesu
Modified: 2020-03-21 03:56 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Lionello Lunesu 2015-11-20 05:16:34 UTC
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 basile-z 2015-11-20 21:09:59 UTC
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 Lionello Lunesu 2015-11-21 11:14:33 UTC
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 4 github-bugzilla 2015-11-21 16:41:17 UTC
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 github-bugzilla 2016-01-03 14:02:31 UTC
Commits pushed to stable 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

https://github.com/D-Programming-Language/dmd/commit/913ffbe000e99e3a2aa6caaea9994a103503d6ac
Merge pull request #5279 from 9rnsr/fix15366