[Issue 24208] New: [DIP1000] Nested function can pass scope pointer to non-scope parameter

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Oct 29 21:21:06 UTC 2023


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

          Issue ID: 24208
           Summary: [DIP1000] Nested function can pass scope pointer to
                    non-scope parameter
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: snarwin+bugzilla at gmail.com

As of DMD 2.105.2, the following invalid program compiles with -preview=dip1000
and runs without errors:

---
void main() @safe
{
    int* escaped;

    void escape(int* p) @safe
    {
        escaped = p;
    }
    void borrow(scope int* param) @safe
    {
        escape(param);
    }

    int n;
    borrow(&n);
    assert(escaped == &n);
}
---

This program is invalid because, in @safe code, it assigns the address of the
variable `n` to the variable `escaped`, which has a longer lifetime than `n`.

The expression `escape(param)` should cause a compile-time error, because it
assigns the value of the scope parameter `param` to the non-scope parameter
`p`.

--


More information about the Digitalmars-d-bugs mailing list