Is @safe still a work-in-progress?

Walter Bright newshound2 at digitalmars.com
Wed Aug 29 05:28:30 UTC 2018


On 8/23/2018 6:10 AM, Atila Neves wrote:
> ----------
> struct S {
>      int x;
>      @safe int* foo() { return &x; }
> }
> ----------
> 
> % dmd -o- -dip1000 foo.d
> % echo $?
> 0

struct S {
     int x;
     @safe int* foo() { return &x; }
}

int* bar() {
     S s;
     return s.foo();
}

dmd test -dip1000
test.d(3): Error: returning &this.x escapes a reference to parameter this, 
perhaps annotate with return


> 
> Oops:
> 
> ----------
> int* gPtr;
> void main() {
>      auto s = S(42);
>      gPtr = s.foo;
> }
> ----------

I get:

test.d(3): Error: returning &this.x escapes a reference to parameter this, 
perhaps annotate with return


> ----------
> int[] gSlice;
> void main() {
>      auto s = S([42]);
>      gSlice = s.foo;
> }
> 
> struct S {
>      int[] x;
>      @safe int[] foo() return { return x; }
> }
> ----------

T[] is treated like T* as far as scope, etc., are concerned. You should see 
identical results.

> And in a struct, `this` is a `ref`, yet `scope` on a member function applies to 
> `this`.

Actually, it applies to fields of `this`.


More information about the Digitalmars-d mailing list