Bug 20156 – [REG2.080] Wrong error about local variable escape

Status
RESOLVED
Resolution
WORKSFORME
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-08-23T13:24:04Z
Last change time
2022-03-24T11:27:30Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
anonymous4
See also
https://issues.dlang.org/show_bug.cgi?id=17927, https://issues.dlang.org/show_bug.cgi?id=20149

Comments

Comment #0 by dfj1esp02 — 2019-08-23T13:24:04Z
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 by slavo5150 — 2019-09-21T08:16:12Z
This appears to be the same as Issue 17927 and Issue 20149
Comment #2 by razvan.nitu1305 — 2022-03-24T09:58:22Z
I cannot reproduce this with the most recent master. Please reopen if you are able to reproduce.
Comment #3 by dkorpel — 2022-03-24T11:27:30Z
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.