D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 18644 - [dip1000] escape of outer local not detected
Summary: [dip1000] escape of outer local not detected
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: safe
Depends on:
Blocks:
 
Reported: 2018-03-21 07:30 UTC by Walter Bright
Modified: 2018-03-22 00:08 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 2018-03-21 07:30:21 UTC
Consider:

  @safe void test() {
    int i;

    int*[] a = [&i];  // error detected correctly

    int* foo() { return &i; }
    int*[] b = [foo()]; // should detect error
  }
Comment 1 Jonathan M Davis 2018-03-21 08:23:10 UTC
Shouldn't the error be that

    int* foo() { return &i; }

is trying to convert scope int* to int* in @safe code? Unless foo isn't inferred as @safe in spite of it being declared in an @safe function, but if that's the case, then calling foo should be an error, since otherwise, an @safe function would be calling an @system function without @trusted being involved. Certainly, given the fact that foo() returns int*, I don't see how

    int*[] b = [foo()]; // should detect error

is invalid. It's only dealing with int*. So, there's no escaping as far is that bit of code is concerned.
Comment 2 Walter Bright 2018-03-21 08:25:51 UTC
https://github.com/dlang/dmd/pull/8062
Comment 3 Walter Bright 2018-03-21 08:29:35 UTC
(In reply to Jonathan M Davis from comment #1)
>     int i;
>     int* foo() { return &i; }

should behave like:

    int* foo(int* p) { return p; }

and then the error is detected:

    int*[] b = [foo(&i)];

The [ ] puts things on the heap, where they escape.
Comment 4 github-bugzilla 2018-03-22 00:08:43 UTC
Commits pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/51565f362fd318ceb0cf94519819c3232b1adb22
fix Issue 18644 - [dip1000] escape of outer local not detected

https://github.com/dlang/dmd/commit/0e0df72c1d3b70cb2f1a7fb2f6b26ac7bb8dcc39
Merge pull request #8062 from WalterBright/fix18644

fix Issue 18644 - [dip1000] escape of outer local not detected