Comment #0 by qs.il.paperinik — 2024-07-03T13:53:39Z
The `is` binary operator (`lhs is rhs`) for built-in types should always test the whole bit pattern of whatever it compares. For `bool` it tests only the last bit of the `bool` value. This means that an invalid `bool` (e.g. created by accident in `@system` code) is needlessly hard to debug. Bare-metal tools such as `is` should be able to tell an invalid `bool` apart from a valid `bool` by having an invalid bool be `!is true` and `!is false`, and instead have it `is N` with 2 ≤ `N` ≤ `ubyte.max`.
Comment #1 by qs.il.paperinik — 2024-07-03T13:53:47Z
Masking the `bool` for the `is` test is wrong. A struct with padding bits has its padding bits compared by `is` as well. For all intents and purposes, a `bool` is a struct around a `ubyte` with the invariant that its value is only ever 0 or 1.
Comment #2 by dkorpel — 2024-07-03T15:04:27Z
There is no defined behavior for `is` on invalid bools, so you can't say it's broken when dmd doesn't do a particular thing. It is true that dmd still does redundant masking for bool from a previous safety fix, which can now be removed since safe values for `bool` have been agreed upon and specified since, so I'll reframe this as a performance issue.
This code:
```D
bool eq(bool a, bool b)
{
return a is b;
}
```
Generates this code with `-O`:
```
mov CL,SIL
and CL,1
mov DL,DIL
and DL,1
cmp CL,DL
setz AL
pop RBP
```
Which could have the `and` instructions removed.
Comment #3 by robert.schadek — 2024-12-13T19:36:11Z