Bug 22396 – Assignments from function value parameters should pass by move when possible

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
Linux
Creation time
2021-10-16T15:39:55Z
Last change time
2024-12-13T19:18:46Z
Assigned to
No Owner
Creator
Per Nordlöw
Moved to GitHub: dmd#19993 →

Comments

Comment #0 by per.nordlow — 2021-10-16T15:39:55Z
The number of cases where a variable (including function parameters passed by value) is be passed by move should be extend to include at least assignments if that assignment statement is the last reference of the variable. This enables non-copyable (r-value) ranges to be passed to common range-based Phobos algorithms. Such r-value ranges are typically created when a non-copyable C++/Rust-style containers is passed by move _into_ a specific range I propose to name `UniqueInputRange`. `UniqueInputRange`, itself non-copyable, can then with this change be passed to Phobos' higher-order algorithms, typically `map`, `filter`, `reduce` etc. This idea has already be realized in Rust using [IntoIterator](https://doc.rust-lang.org/std/iter/trait.IntoIterator.html). Specifically, range algorithm constructor should be able to take a non-copyable range as its source range parameter as in ```d struct SomeRange { this(R range) { source = range; // TODO `range` should be passed by move to `source` } } ``` when `range` is not referenced after it being moved in the assignment. If the compiler is modified to automatically allow such assignments to perform move all the details described above can be implemented in phobos. See also https://github.com/dlang/phobos/pull/4971.
Comment #1 by razvan.nitu1305 — 2021-10-19T07:17:38Z
This sort of analysis was proposed in the move constructor DIP: https://github.com/dlang/DIPs/blob/a9c553b0dbab1c2983a801b5e89b51c5c33d5180/DIPs/DIP1040.md (Last Use chapter). Until the DIP is accepted and implemented this will probably not be solved.
Comment #2 by robert.schadek — 2024-12-13T19:18:46Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19993 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB