Escape Analysis on reddit

Steven Schveighoffer schveiguy at yahoo.com
Fri Oct 31 13:17:41 PDT 2008


"Walter Bright" wrote
> http://www.reddit.com/r/programming/comments/7akzd/escape_analysis/

The question is, in the example:

int* bar(int* p) { return p; }
int* foo()
{
    int x = 3;
    return bar(&x);
}

where is the problem?  Is the problem in bar, or is the problem in foo?

The real problem is in foo, because there's technically nothing wrong with 
bar.

So what ends up happening in your scheme is this:

int *bar(int *p) { return p;}

void foo(scope int *p)
{
   int *x = bar(p); // no escape, but compile error
}

So yes, you are protected, but at the cost of not being able to write code 
that is provably safe.  The end result is you end up removing scope where it 
really should be, and then virally, it makes you remove scope decorations 
everywhere else.  I predict that if this is implemented, nobody will be able 
to use it.

In order for this to be useful, we need to have a way to express total 
relationships between parameters and the return value.  And that would 
probably be so convoluted that it's not worth the syntax.

I'll reiterate my challenge from another thread:

void f(ref int *a, int *b, int *c) { if(*b < *c) a = b;  else a = c;}

struct S
{
   int *v;
}

int *f2(S* s) { return s.v;}

void f3(ref int *a, ref int *b, ref int *c)
{
   int *tmp = a;
   a = b; b = c; c = tmp;
}

void f4()
{
   int a=1, b=2, c=3;
   auto ap = &a;
   auto bp = &b;
   auto cp = &c;
   S s(cp);

   f(ap, bp, cp);
   b = *f2(&s);
   f3(ap, bp, cp);
}

Please tell me how I should mark f, S, f2, and f3 such that f4 compiles. 
Note that this code all works in D1 and provably has no problems with 
escaping values, since f4 returns void, and no globals are accessed.

-Steve 





More information about the Digitalmars-d mailing list