Thread overview
[Issue 21209] scope attribute inference with does not work well with foreach
Aug 29, 2020
Chloé
Feb 19, 2021
Mathias LANG
Feb 19, 2021
ag0aep6g
Jun 01, 2021
Dennis
Jun 01, 2021
Dlang Bot
Jun 02, 2021
Dlang Bot
Jun 03, 2021
Dennis
August 29, 2020
https://issues.dlang.org/show_bug.cgi?id=21209

Chloé <rightfold+bugzilla+dlang@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |rejects-valid, safe
           Severity|enhancement                 |normal

--
February 19, 2021
https://issues.dlang.org/show_bug.cgi?id=21209

Mathias LANG <pro.mathias.lang@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pro.mathias.lang@gmail.com

--- Comment #1 from Mathias LANG <pro.mathias.lang@gmail.com> ---
At first I thought it wasn't a bug because the type of `cs` is specified.
However, after changing `foo` to:
```
void foo(T)(T cs)
{
    foreach (c; cs)
        bar(c);
}
```

The error persist. The strangest part is that not calling `bar` from the foreach makes the error go away.

--
February 19, 2021
https://issues.dlang.org/show_bug.cgi?id=21209

ag0aep6g <ag0aep6g@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ag0aep6g@gmail.com
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=20674,
                   |                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=20150

--- Comment #2 from ag0aep6g <ag0aep6g@gmail.com> ---
(In reply to Mathias LANG from comment #1)
> The strangest part is that not calling `bar` from the foreach makes the error go away.

That's because `bar` is not `pure`. Without the `bar` call, `foo` becomes `pure` and you're hitting issue 20150 (DMD wrongly assumes that parameters of `pure` functions can always be `scope`).

When not relying on issue 20150, inference of `scope` is very limited. See also issue 20674.

--
June 01, 2021
https://issues.dlang.org/show_bug.cgi?id=21209

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dkorpel@live.nl
           Hardware|x86_64                      |All
                 OS|Linux                       |All

--- Comment #3 from Dennis <dkorpel@live.nl> ---
Worth noting that scope inference also fails on foreach loops of alias sequences:

```
@safe:
void foo(A...)(A args)
{
    static int x; x++; // force impure for issue 20150
    foreach(a; args)
    {
        // gets lowered to unrolled loop with `string a = _param_0;`
    }
}

void main()
{
    scope string x = "hey";
    foo(x);
}
```

Error: scope variable `x` assigned to non-scope parameter `_param_0` calling foo!string.foo

--
June 01, 2021
https://issues.dlang.org/show_bug.cgi?id=21209

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

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

--- Comment #4 from Dlang Bot <dlang-bot@dlang.rocks> ---
@dkorpel created dlang/dmd pull request #12620 "Fix issue 21209 - scope attribute inference fails on foreach" fixing this issue:

- fix issue 21209 - scope inference fails with foreach

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

--
June 02, 2021
https://issues.dlang.org/show_bug.cgi?id=21209

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

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

--- Comment #5 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dmd pull request #12620 "Fix issue 21209 - scope attribute inference fails on foreach" was merged into master:

- 8e9ea4ef3ae9eb2cad92eeb8d54189dfb108e8b7 by dkorpel:
  Fix issue 21209 - scope inference fails on foreach

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

--
June 03, 2021
https://issues.dlang.org/show_bug.cgi?id=21209

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=21990

--