Thread overview
[Issue 23300] std.array : array wrongly propagates scopeness of source
Aug 24, 2022
RazvanN
Aug 24, 2022
Dennis
Sep 10, 2022
Ate Eskola
Sep 17, 2022
ag0aep6g
Sep 17, 2022
Ate Eskola
Jan 27, 2023
Dennis
Jan 27, 2023
Dlang Bot
August 24, 2022
https://issues.dlang.org/show_bug.cgi?id=23300

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305@gmail.com

--- Comment #1 from RazvanN <razvan.nitu1305@gmail.com> ---
(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.

--
August 24, 2022
https://issues.dlang.org/show_bug.cgi?id=23300

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dkorpel@live.nl

--- Comment #2 from Dennis <dkorpel@live.nl> ---
(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.

--
September 10, 2022
https://issues.dlang.org/show_bug.cgi?id=23300

--- Comment #3 from Ate Eskola <Ajieskola@gmail.com> ---
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?

--
September 17, 2022
https://issues.dlang.org/show_bug.cgi?id=23300

ag0aep6g <ag0aep6g@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ag0aep6g@gmail.com

--- Comment #4 from ag0aep6g <ag0aep6g@gmail.com> ---
(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").

--
September 17, 2022
https://issues.dlang.org/show_bug.cgi?id=23300

--- Comment #5 from Ate Eskola <Ajieskola@gmail.com> ---
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.

--
January 27, 2023
https://issues.dlang.org/show_bug.cgi?id=23300

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |johanengelen@weka.io

--- Comment #6 from Dennis <dkorpel@live.nl> ---
*** Issue 23657 has been marked as a duplicate of this issue. ***

--
January 27, 2023
https://issues.dlang.org/show_bug.cgi?id=23300

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

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

--- Comment #7 from Dlang Bot <dlang-bot@dlang.rocks> ---
@dkorpel updated dlang/dmd pull request #14492 "Fix 20674, 23208, 23300 - improve `scope` inference" fixing this issue:

- Fix 20674, 23208, 23300, 23294 - improve `scope` inference

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

--
January 27, 2023
https://issues.dlang.org/show_bug.cgi?id=23300

johanengelen@weka.io changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |industry

--
April 04
https://issues.dlang.org/show_bug.cgi?id=23300

--- Comment #8 from johanengelen@weka.io ---
Note that this bug does not require DIP1000, and breaks compilation since
2.101.
https://issues.dlang.org/show_bug.cgi?id=23657

--