Thread overview
[Issue 21341] [REG2.092] Wrong reference escape error when parameter is stored in this
May 09, 2023
Dennis
October 25, 2020
https://issues.dlang.org/show_bug.cgi?id=21341

johanengelen@weka.io changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |industry, rejects-valid
           Hardware|x86                         |All
                 OS|Mac OS X                    |All

--
October 25, 2020
https://issues.dlang.org/show_bug.cgi?id=21341

--- Comment #1 from johanengelen@weka.io ---
Workaround: use explicit pointer parameter (`Watcher*`) instead of reference
(`ref Watcher`).

--
May 09, 2023
https://issues.dlang.org/show_bug.cgi?id=21341

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |safe
             Status|NEW                         |RESOLVED
                 CC|                            |dkorpel@live.nl
         Resolution|---                         |INVALID

--- Comment #2 from Dennis <dkorpel@live.nl> ---
The code compiles now because the check is no longer performed in `@system` code, but the error comes back when you add `@safe:`.

However, the behavior is as intended, and comes down to the infamous 'scope is not transitive' limitation.

> The `return` on parameter `watcher` means that the reference escapes to the `this` parameter.

`scope` on `this` applies to the array (`watchers.ptr`), not any pointers in the array elements. Line 13 is equivalent to:

```D
this.watchers ~= [A(&watcher.func)];
```

And the problem isn't the `this.watchers ~=` part, but the
`[A(&watcher.func)];` part putting it into a GC array literal (the 'allocated
memory' that the error talks about)

--