D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 22023 - adding `return` to escaped argument of a variadic defeats @safe
Summary: adding `return` to escaped argument of a variadic defeats @safe
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 major
Assignee: No Owner
URL:
Keywords: accepts-invalid, live, pull, safe
Depends on:
Blocks:
 
Reported: 2021-06-14 15:23 UTC by Nicholas Wilson
Modified: 2022-02-23 22:26 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Nicholas Wilson 2021-06-14 15:23:14 UTC
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 Dennis 2021-09-08 17:21:26 UTC
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.
Comment 2 Walter Bright 2022-02-23 08:04:50 UTC
These are called typesafe variadic functions:

https://dlang.org/spec/function.html#typesafe_variadic_functions
Comment 3 Dlang Bot 2022-02-23 08:51:42 UTC
@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 Dlang Bot 2022-02-23 22:26:02 UTC
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