This should compile but does not on dmd 2.100, using -preview=dip1000 flag.
-------------
@safe int[] fun()
{ import std : array, map;
scope r = [1,2,3].map!"a+3";
return r.array;
}
-------------
Were I returning `r` directly, this would be escaping a reference to scoped data, but `array` copies the data. Returning `array`ed range should be allowed, `scope` or no.
Comment #1 by razvan.nitu1305 — 2022-08-24T12:36:40Z
(In reply to Ate Eskola from comment #0)
> This should compile but does not on dmd 2.100, using -preview=dip1000 flag.
>
> -------------
> @safe int[] fun()
> { import std : array, map;
> scope r = [1,2,3].map!"a+3";
> return r.array;
> }
> -------------
>
> Were I returning `r` directly, this would be escaping a reference to scoped
> data, but `array` copies the data. Returning `array`ed range should be
> allowed, `scope` or no.
The problem here is that `r` is `scope` so for it to be able to be passed to `array`, `array` also needs to receive a scope parameter. However, `array`'s parameter cannot be `scope` because it is copied into allocated memory.
By the current scope rules this seems to be correct behavior, making this bug report invalid.
Comment #2 by dkorpel — 2022-08-24T15:22:42Z
(In reply to RazvanN from comment #1)
> However, `array`'s
> parameter cannot be `scope` because it is copied into allocated memory.
The parameter to `array` should infer `scope` unless the range has non-scope range primitives. It's not escaping the input array, it's dereferencing it and copying the elements into a new GC array which is not scope.
`scope` inference fails and it falls back on `return scope` inference from `pure`. By making it impure, the newest DMD reports:
test_.d(12): Error: scope variable `r` assigned to non-scope parameter `r` calling `array`
std/array.d(112): which is not `scope` because of `__r353 = r`
https://github.com/dlang/phobos/blob/b578dfad94770574d7e522557a77276c35943daa/std/array.d#L112
So the underlying problem is that `scope` inference is defeated by `foreach` on a struct with range primitives.
Comment #3 by Ajieskola — 2022-09-10T22:41:24Z
I narrowed down what causes the inference to fail
------
auto fun(int* ptr)
{ // comment out this line and this compiles
// as does if you explicitly mark either it or the argument scope
auto r = ptr;
return new int;
}
@safe int* gun(scope int* ptr){return ptr.fun;}
------
Is the scope inference supposed to behave like this?
Comment #4 by ag0aep6g — 2022-09-17T05:14:32Z
(In reply to Ate Eskola from comment #3)
> ------
> auto fun(int* ptr)
> { // comment out this line and this compiles
> // as does if you explicitly mark either it or the argument scope
> auto r = ptr;
> return new int;
> }
>
> @safe int* gun(scope int* ptr){return ptr.fun;}
> ------
>
> Is the scope inference supposed to behave like this?
That's issue 20674 ("[DIP1000] inference of `scope` is easily confused").
Comment #5 by Ajieskola — 2022-09-17T12:14:25Z
Thanks. The current attitude of Walter seems to be that yes, the inference is working as it should. I do aknowledge that this isn't necessarily set in stone - we might still reach consensus to change the language.
But as long as the status quo stays what it is now, this is a Phobos bug. `array` should be changed so that it compiles my first example with the current language rules.
Comment #6 by dkorpel — 2023-01-27T13:36:14Z
*** Issue 23657 has been marked as a duplicate of this issue. ***
@dkorpel created dlang/phobos pull request #9026 "Bugzilla 23300 - add testcase for std.array on scope InputRange" mentioning this issue:
- Bugzilla 23300 - add testcase for std.array on scope InputRange
https://github.com/dlang/phobos/pull/9026
Comment #11 by dlang-bot — 2024-07-19T12:46:28Z
dlang/dmd pull request #16724 "Fix bugzilla 23300 - std.array : array wrongly propagates scopeness o…" was merged into master:
- 6a041f8fc712bffd3a35326be99650ac28361477 by Dennis Korpel:
Fix bugzilla 23300 - std.array : array wrongly propagates scopeness of source
https://github.com/dlang/dmd/pull/16724
Comment #12 by dlang-bot — 2024-07-26T21:00:20Z
dlang/phobos pull request #9026 "Bugzilla 23300 - add testcase for std.array on scope InputRange" was merged into master:
- 2959a226413076670120a76631aa85cc5f1291c6 by Dennis Korpel:
Bugzilla 23300 - add testcase for std.array on scope InputRange
https://github.com/dlang/phobos/pull/9026