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
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.
These are called typesafe variadic functions: https://dlang.org/spec/function.html#typesafe_variadic_functions
@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
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