Bug 22977 – [dip1000] can escape scope pointer returned by nested function

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-04-03T10:05:39Z
Last change time
2024-07-22T10:31:42Z
Keywords
pull
Assigned to
No Owner
Creator
Dennis
See also
https://issues.dlang.org/show_bug.cgi?id=23438

Comments

Comment #0 by dkorpel — 2022-04-03T10:05:39Z
Nested function `fn` does not get `return` inferred here: ``` int* gPtr; void main() @safe { scope int* sPtr; int* fn() { return sPtr; } gPtr = fn(); } ```
Comment #1 by dlang-bot — 2022-06-21T21:21:03Z
@dkorpel created dlang/dmd pull request #14236 "Fix 22977 - can escape scope pointer returned by nested function" fixing this issue: - Fix 22977 - can escape scope pointer returned by nested function https://github.com/dlang/dmd/pull/14236
Comment #2 by bugzilla — 2022-08-15T15:36:27Z
Probably the most sensible way to deal with this is to allow non-static nested functions to have `return` and `scope` attributes, and then treat uplevel variable references as if they were parameters marked with those attributes.
Comment #3 by bugzilla — 2022-08-27T21:24:58Z
Interestingly, if `scope return` is added to `fn()`: int* gPtr; void main() @safe { scope int* sPtr; int* fn() scope return { return sPtr; } gPtr = fn(); } The correct error is generated: Error: reference to stack allocated value returned by `fn()` assigned to non-scope `gPtr`
Comment #4 by bugzilla — 2022-08-28T04:37:59Z
It appears that the problem is the nested function is not inferring `return scope` when returning a scope variable from its closure.
Comment #5 by dkorpel — 2022-08-28T10:51:13Z
(In reply to Walter Bright from comment #4) > It appears that the problem is the nested function is not inferring `return > scope` when returning a scope variable from its closure. Indeed, the linked PR (https://github.com/dlang/dmd/pull/14236) tries to fix this, but it is currently blocked by buildkite projects.
Comment #6 by dlang-bot — 2022-08-29T01:06:56Z
@WalterBright created dlang/dmd pull request #14391 "fix Issue 22977 - [dip1000] can escape scope pointer returned by nest…" fixing this issue: - fix Issue 22977 - [dip1000] can escape scope pointer returned by nested function https://github.com/dlang/dmd/pull/14391
Comment #7 by bugzilla — 2022-08-29T01:07:35Z
And now we have two fixes! Let's compare and make the best of both.
Comment #8 by bugzilla — 2022-08-29T02:46:08Z
Dennis, your's is better.
Comment #9 by dkorpel — 2022-10-27T13:00:52Z
*** Issue 23438 has been marked as a duplicate of this issue. ***
Comment #10 by dkorpel — 2024-07-22T10:31:42Z