[Dlang-internal] DIP1000 discussion and testing: borrowing a range

Walter Bright via Dlang-internal dlang-internal at puremagic.com
Sun Oct 16 12:51:52 PDT 2016


On 10/16/2016 7:02 AM, Dicebot wrote:
> 	// [1] Error: function has 'return' but does not return any
>  	// indirections
>         auto borrow () return scope
>         {
>             struct Range
>             {
>                 int* data;
>             }
>
>             return Range(&this.data); // [2] Error: cannot take address of
>                                       // parameter this in @safe
> function borrow
>         }
>
> First error [1] simply to be simply a bug, because it contradicts "struct is
> a composition of its fields" statement from DIP1000.

I'd ignore that error because it is a cascaded error.

(It is helpful to label errors so it's easier to see which you're referring to. 
I added such annotations to the quoted part.)

As for error [2], 'ref' is meant to be a restricted pointer. Converting a 
restricted pointer to a regular pointer, which is what &this does, undermines 
the whole point of having 'ref', and so is disallowed in @safe code.

Consider:

   @safe int* foo(return ref int r) {
     return &r; // Error: cannot take address of parameter r in @safe function foo
   }

Making it @trusted works. But even if @trusted, escape checking is still done, 
even in @system code:

   @trusted int* foo(return ref int r) {
     return &r; // no error, 'cuz it's trusted
   }

   int* bar() {
     int r;
     return foo(r); // Error: escaping reference to local variable r
   }



More information about the Dlang-internal mailing list