[Issue 17784] New: [scope][DIP1000] Confusing error message for escaping local via new-expression

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Aug 26 23:34:15 PDT 2017


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

          Issue ID: 17784
           Summary: [scope][DIP1000] Confusing error message for escaping
                    local via new-expression
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: petar.p.kirov at gmail.com

With PR https://github.com/dlang/dmd/pull/7101, given the following code:
```
#line 1300

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

void test1300() @safe
{
    int i;
    auto p = new S1300(&i).oops; // line 1306
}

struct S1300
{
    int* oops;
//    this(int* p) @safe { oops = p; }
}
```

The compiler produces the following error message:
scope_test.d(1306): Error: returning `& i` escapes a reference to local
variable `i`

Which is confusing to the user, because there are no return statements in the
whole file. I suggest the following error message:

scope_test.d(1306): Error: reference to local variable `i` assigned to
heap-allocated `S1300.oops` with possibly longer lifetime

--


More information about the Digitalmars-d-bugs mailing list