[Issue 21341] [REG2.092] Wrong reference escape error when parameter is stored in this

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue May 9 20:59:36 UTC 2023


https://issues.dlang.org/show_bug.cgi?id=21341

Dennis <dkorpel at live.nl> changed:

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

--- Comment #2 from Dennis <dkorpel at 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)

--


More information about the Digitalmars-d-bugs mailing list