[Issue 17730] New: [scope][dip1000] Can escape references to scope classes via moving

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Aug 7 15:45:29 PDT 2017


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

          Issue ID: 17730
           Summary: [scope][dip1000] Can escape references to scope
                    classes via moving
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: moritz at ucworks.org

The following compiles with `dmd -dip1000 escape_scope_class.d`

- escape_scope_class.d -
import core.stdc.stdio : printf;
import std.algorithm : move;

class A
{
    int i;

    this() @safe
    {
        i = 0;
    }
}

void inc(scope A a) @safe
{
    a.i += 1;
}

void print(scope A a) @trusted
{
    printf("A@%x: %d\n", cast(void*) a, a.i);
}

auto makeA() @safe
{
    scope a = new A();
    a.print();
    return move(a);
}

void main() @safe
{
    auto a = makeA();
    foreach (i; 0..3) {
        a.print();
        a.inc();
    }
}
---

and outputs something like

---
A at 198d1568: 0
A at 198d1568: 0
A at 198d1568: 1
A at 198d1568: 2
---

, i.e. a reference to the `makeA.a` object is escaped and assigned to `main.a`
within @safe code.

What needs fixing is that the above code should error out with an appropriate
message about escaping the reference to the scope class.

Additionally, it would be nice if the following code where to compile with
`-dip1000`, but work like moving a std.typecons.scoped:

---
void main() @safe
{
    scope a = makeA();
    foreach (i; 0..3) {
        a.print();
        a.inc();
    }
}
---

would then output something like

---
A at 198d1568: 0 // scope class object makeA.a (on makeA's stack frame)
A at 198d1578: 0 // scope class object main.a (on main's stack frame)
A at 198d1578: 1
A at 198d1578: 2
---

so essentially the scope class object is blitted from makeA to main the same
way it would if it were a scoped!A (and has the same dangers w.r.t.
self-references, of course).

--


More information about the Digitalmars-d-bugs mailing list