Escape Analysis on reddit
Walter Bright
newshound1 at digitalmars.com
Fri Oct 31 20:50:39 PDT 2008
Steven Schveighoffer wrote:
> So how does foo know what scope return means? Consider a min function:
>
> scope T min(T)(scope T v1, scope T v2);
>
> Now, let's try it out:
>
> scope int *foo(scope int *p)
> {
> int x;
> return min(&x, p);
> }
>
> Shouldn't this not compile? If you say 'well, the compiler can take the
> most conservative approach, and assume scope means the most inner scope.'
Assume the compiler keeps track of, for every expression, a list of
references that it is composed of. For:
min(&x, p)
the compiler knows that min() returns some combination of v1 and v2, so
the expression min(&x, p) is tagged with the list [&x,p]. Now it
examines the return, and sees that [&x,p] is escaping the scope of the
function. Although p is a scope parameter, the scope on the foo() return
value says that's ok, so that passes. The &x, however, fails because
it's a reference to a local going out of scope.
So, I think we've got this case covered properly.
But I don't like the syntax. For one thing, if the return value might
refer to parameter p1 but not p2, we need to express that. So, something
like:
int* foo(return scope int* p1, scope int* p2, return scope int* p3);
Now, the caller tags a call to foo() with [arg1,arg3] for its escape
analysis, omitting arg2.
The fly in this is I'd like to combine it somehow with the const
propagation issue of argument type to return type.
More information about the Digitalmars-d
mailing list