Issue 23682 - dip1000 problem with return by ref
Summary: dip1000 problem with return by ref
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull, safe
Depends on:
Blocks:
 
Reported: 2023-02-09 02:10 UTC by Walter Bright
Modified: 2023-02-12 23:30 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Walter Bright 2023-02-09 02:10:18 UTC
For:
---
alias VErr = char*;

@safe:

ref char* front_p(return scope char** p) { return *p; }

__gshared char* g;

void main()
{
    char* _errors;
    g = front_p(&_errors); // Error: address of variable `_errors` assigned to `g` with longer lifetime
}
---

The error is incorrect. The return value is indirected before assignment to `g`, so the address of `_errors` isn't being assigned to `g`, what `_errors` is pointing to is assigned to `g`.

This can be seen by commenting out `@safe:`, compiling it, and looking at the assembler.
Comment 1 Dennis 2023-02-09 10:43:55 UTC
Lol, when I posted this, you tried to convince me it's working as intended and closed as invalid. I'm glad we're on the same page now.

*** This issue has been marked as a duplicate of issue 22916 ***
Comment 2 Walter Bright 2023-02-10 08:57:18 UTC
This has a simpler test case, so I'm going to fix it and then look at 22916.
Comment 3 Dlang Bot 2023-02-10 08:59:20 UTC
@WalterBright created dlang/dmd pull request #14869 "fix Issue 23682 - dip1000 problem with return by ref" fixing this issue:

- fix Issue 23682 - dip1000 problem with return by ref

https://github.com/dlang/dmd/pull/14869
Comment 4 Dlang Bot 2023-02-12 23:30:24 UTC
dlang/dmd pull request #14869 "fix Issue 23682 - dip1000 problem with return by ref" was merged into master:

- 2b12d6fe9e704a6807101f17bca5fa7e63bdcb78 by Walter Bright:
  fix Issue 23682 - dip1000 problem with return by ref

https://github.com/dlang/dmd/pull/14869