Bug 20416 – [Regression 2.073.2] compiler complains about escaping reference in certain cases
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-11-24T16:51:48Z
Last change time
2019-12-07T07:06:27Z
Keywords
pull, rejects-valid, safe
Assigned to
No Owner
Creator
Steven Schveighoffer
Comments
Comment #0 by schveiguy — 2019-11-24T16:51:48Z
In some cases, when a function returns the result of a ref function by value, the compiler complains that it's escaping a local reference.
An example case:
struct S
{
string x;
}
ref S foo(return ref S s) { return s; }
S bar()
{
S result;
return foo(result);
}
Error: returning foo(result) escapes a reference to local variable result
Note the return attribute in foo is significant. Without that it succeeds. Also significant is that S contains a string. If you replace it with an int, it works. Probably has something to do with the indirection in the string.
Clearly, we aren't returning by ref from bar, but the compiler fails to see this.
Seems to be a regression for 2.073 (where dip1000 was introduced). With -dip1000 switch, it still fails.
Workaround is to call foo, and then return result directly, which oddly shows no complaint even though it's equivalent.
Comment #1 by bugzilla — 2019-12-06T11:03:58Z
A simpler test case:
alias P = int*;
ref P foo(return ref P);
P bar() {
P result;
return foo(result);
}
dmd -c test -dip1000
test.d(12): Error: returning foo(result) escapes a reference to local variable result
@WalterBright updated dlang/dmd pull request #10640 "fix Issue 20416 - [Regression 2.073.2] compiler complains about escap…" fixing this issue:
- fix Issue 20416 - [Regression 2.073.2] compiler complains about escaping reference in certain cases
https://github.com/dlang/dmd/pull/10640
Comment #4 by dlang-bot — 2019-12-07T07:06:27Z
dlang/dmd pull request #10640 "fix Issue 20416 - [Regression 2.073.2] compiler complains about escap…" was merged into stable:
- a1d82c6eec6d793e0334a71f63a22589a43d0cb0 by Walter Bright:
fix Issue 20416 - [Regression 2.073.2] compiler complains about escaping reference in certain cases
https://github.com/dlang/dmd/pull/10640