D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20156 - [REG2.080] Wrong error about local variable escape
Summary: [REG2.080] Wrong error about local variable escape
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 regression
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2019-08-23 13:24 UTC by anonymous4
Modified: 2022-03-24 11:27 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description anonymous4 2019-08-23 13:24:04 UTC
struct A
{
	int[] b;
}
inout(int[]) f1(ref inout A);
void f2(inout A src)
{
	inout A c=src;
	inout(int[])[] a;
	a~=f1(c);
}

onlineapp.d(10): Error: copying f1(c) into allocated memory escapes a reference to local variable c
Comment 1 Mike Franklin 2019-09-21 08:16:12 UTC
This appears to be the same as Issue 17927 and Issue 20149
Comment 2 RazvanN 2022-03-24 09:58:22 UTC
I cannot reproduce this with the most recent master. Please reopen if you are able to reproduce.
Comment 3 Dennis 2022-03-24 11:27:30 UTC
This is 'fixed' because `inout` doesn't imply `return` anymore (issue 22027).

If you change the function signature to this:
```
inout(int[]) f1(return ref inout A);
```
The error is back when you pass -preview=dip1000, but it's valid, because the return value can be a pointer to the struct A which is allocated on the stack in f2, and you can't store a stack pointer in a dynamic array.

If f1 becomes this (which is likely what the function should be, judging by the types):
```
inout(int[]) f1(ref return scope inout A);
```
Then there's no error, which is correct because local var c is not `scope`. Either way, it's fixed now.