Thread overview
[Issue 22988] no short-circuiting when constant folding ternary operator
[Issue 22988] Error: shift by -1 is outside the range `0..31`
Apr 05, 2022
Dennis
Apr 05, 2022
Max Samukha
Apr 05, 2022
Max Samukha
Apr 05, 2022
Max Samukha
Apr 05, 2022
Dennis
Apr 05, 2022
Max Samukha
Apr 07, 2022
Dlang Bot
Apr 07, 2022
Dlang Bot
April 05, 2022
https://issues.dlang.org/show_bug.cgi?id=22988

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |dkorpel@live.nl
         Resolution|---                         |INVALID

--- Comment #1 from Dennis <dkorpel@live.nl> ---
Because AddExpression has precedence over ShiftExpression, it's parsed as:
```
enum b = a ? 1 << (a - 1) : 0;
```
Since a = 0, it results in a shift by -1, which gives an error. To fix, add
parentheses around the shift:
```
enum b = a ? (1 << a) - 1 : 0;
```

--
April 05, 2022
https://issues.dlang.org/show_bug.cgi?id=22988

Max Samukha <maxsamukha@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |---

--- Comment #2 from Max Samukha <maxsamukha@gmail.com> ---
The precedence is intended to be (a ? (1 << (a - 1)) : 0).

Since 'a' is 0, the first branch of the conditional shouldn't be evaluated, and the expected result is 0.

Compare that to the runtime version, which behaves as expected:

void main()
{
    int a = 0;
    int b = a ? 1 << a - 1 : 0;
    assert(b == 0);
}

--
April 05, 2022
https://issues.dlang.org/show_bug.cgi?id=22988

--- Comment #3 from Max Samukha <maxsamukha@gmail.com> ---
Similar case:

enum l = 0;
enum int[l] a = [];
enum b = l ? a[l] : 0;

Error: array index 0 is out of bounds `a[0 .. 0]`


Or simply:

enum a = 0 ? 1 << -1 : 0;

enum int[0] a = [];
enum b = 0 ? a[0] : 0;

--
April 05, 2022
https://issues.dlang.org/show_bug.cgi?id=22988

--- Comment #4 from Max Samukha <maxsamukha@gmail.com> ---
(In reply to Max Samukha from comment #3)

> enum b = l ? a[l] : 0;

Should be:
enum b = l ? a[l - 1] : 0;

--
April 05, 2022
https://issues.dlang.org/show_bug.cgi?id=22988

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Error: shift by -1 is       |no short-circuiting when
                   |outside the range `0..31`   |constant folding ternary
                   |                            |operator

--- Comment #5 from Dennis <dkorpel@live.nl> ---
Ah, thanks for clearing that up. I think this test illustrates the problem
best:
```
enum a = false && (1 << -1); // passes
enum b = false ? (1 << -1) : 0; // fails

```
I've updated the title

--
April 05, 2022
https://issues.dlang.org/show_bug.cgi?id=22988

--- Comment #6 from Max Samukha <maxsamukha@gmail.com> ---
(In reply to Dennis from comment #5)

> ```
> I've updated the title

Great, thanks!

--
April 07, 2022
https://issues.dlang.org/show_bug.cgi?id=22988

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #7 from Dlang Bot <dlang-bot@dlang.rocks> ---
@BorisCarvajal created dlang/dmd pull request #13961 "Fix Issue 22988 - no short-circuiting when constant folding ternary operator" fixing this issue:

- Fix Issue 22988 - no short-circuiting when constant folding ternary operator

https://github.com/dlang/dmd/pull/13961

--
April 07, 2022
https://issues.dlang.org/show_bug.cgi?id=22988

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #8 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dmd pull request #13961 "Fix Issue 22988 - no short-circuiting when constant folding ternary operator" was merged into master:

- befbc46fd7309ffb6e640822fa74280a750cf65e by Boris Carvajal:
  Fix Issue 22988 - no short-circuiting when constant folding ternary operator

https://github.com/dlang/dmd/pull/13961

--