Bug 22023 – adding `return` to escaped argument of a variadic defeats @safe
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-06-14T15:23:14Z
Last change time
2022-02-23T22:26:02Z
Keywords
accepts-invalid, live, pull, safe
Assigned to
No Owner
Creator
Nicholas Wilson
Comments
Comment #0 by iamthewilsonator — 2021-06-14T15:23:14Z
Given
```
Foo createFoo() @safe {
return Foo(1, 2, 3);
}
void main() @safe {
import std.stdio : writeln;
auto foo = createFoo();
foreach (f; foo.e) writeln(f, " ");
}
```
Defining Foo as:
```
struct Foo {
this(int[] e...) @safe {
this.e = e;
}
int[] e;
}
```
correctly errors as "Error: scope variable `e` assigned to `this` with longer lifetime"
Adding return to the variadic argument
```
struct Foo {
this(return int[] e...) @safe {
this.e = e;
}
int[] e;
}
```
Compiles and prints garbage: e.g.
1
0
2003568368
Comment #1 by dkorpel — 2021-09-08T17:21:26Z
Possible solutions:
- disallow `return` on variadic array arguments
- strip `return` on variadic array arguments, making it a no-op
- allocate the array with the GC on the call site when `return` is present
Not sure which is best, I'm leaning towards the first.
@WalterBright created dlang/dmd pull request #13710 "fix Issue 22023 - adding to escaped argument of a variadic defeats @…" fixing this issue:
- fix Issue 22023 - adding to escaped argument of a variadic defeats @safe
https://github.com/dlang/dmd/pull/13710
Comment #4 by dlang-bot — 2022-02-23T22:26:02Z
dlang/dmd pull request #13710 "fix Issue 22023 - adding to escaped argument of a variadic defeats @…" was merged into master:
- 24b2037256cdbe757e993899063c1c404828b045 by Walter Bright:
fix Issue 22023 - adding to escaped argument of a variadic defeats @safe
https://github.com/dlang/dmd/pull/13710