July 05
https://issues.dlang.org/show_bug.cgi?id=24649

          Issue ID: 24649
           Summary: Upper-bound-inclusive range foreach
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: qs.il.paperinik@gmail.com

A `foreach` loop never includes the upper bound. That is fine most of the time, but when it’s not trivial to generate the sentinel value which is the first not included, writing a such a loop is needlessly hard in D. Examples include cases where the (inclusive) upper bound may realistically be the maximum of its type, which in particular happens for `enum` types.

A simple fix would be to recognize the pattern `+ 1` for the upper bound of `foreach`:

```diff
    UprExpression:
        Expression
+       AddExpression + 1
```

A `L .. U + 1` loop is defined to be an upper-bound-inclusive iteration and works without evaluating or even type-checking `AddExpression + 1`, it only type-checks and evaluates the `AddExpression`. E.g. `ulong.max + 1` does not overflow and `MyEnum.max + 1` does not fail the type checker.

The same for `foreach_reverse`.

(Maybe, but I’m not sure about this extension, this could be generalized to recognizing any `AddExpression + MulExpression`, trying to CTFE the `MulExpression` and if it evaluates to 0 or 1, generate an upper-bound-exclusive or -inclusive loop, and only in other cases, type-check and evaluate the whole `AddExpression + MulExpression`.)

--