On 2009-04-28 15:06:32 -0400, "Robert Jacques" <[email protected]> said:

┌───────┬──────────────┬────────────────────┬─────────────┐
│ scope │ Common Super │ Unknown Allocation │ Transitive† │
└───────┴──────────────┴────────────────────┴─────────────┘
Use of the scope keyword for the common ownership-type is based upon Walter’s original escape analysis blog. However, this design is based upon using the type system restrictions as opposed to full escape analysis to prevent object escape. Full escape analysis would alleviate the restrictions in rule 6.
Basic Rules:
1) Refers to scope definitions inside a function body.
2) May only be assigned at declaration
        scope Node!(int) n;
        n.next = new Node!(int)(); // Error: Possible escape
        n = n.next;                // Error: see relaxation of this rule  below

[...]

Relaxation of Rule 2
Technically, only the tail of a scope type must obey rule 2). Therefore, assigning to the head of a scope type is valid. This allows for more imperative style programming and for things like swap to be valid, however, I don’t know how difficult this is to implement.
        n = n.next;
        auto n2 = n;
        swap(n, n2);
        swap(n, n.next); // Error: Cannot take the reference of a scope tail
        Node!(int) m = new Node!(int)();
        swap(n, m); // Error: m is local, not scope

That's basically why I suggested adding scope constrains back then. To implement swap safely, you need to know that the scope of the pointer you are assigning to is always smaller or equal to the scope of the memory block you're feeding them with.

Here's a new syntax for expressing contrains I've been thinking about:

        void swap(scope int* x, scope int* y)
scope(x = y && y = x) // caller enforces that y is assignable to x and x to y
        {
                scope(x = t && t = y) int* t;
                // y assignable to t and t to x; also imply that
                // x is assignable to y, which holds against previous constrains

                t = y;  // valid since scope(t = y)
                y = x;  // valid since scope(y = x)
                x = t;  // valid since scope(x = t)
        }

Perhaps with simple escape analysis, the compiler could infer the scope constrains of local variable t so you don't have to write it everywhere.


--
Michel Fortin
[email protected]
http://michelf.com/

Reply via email to