[Dlang-internal] DIP1000 discussion and testing: borrowing a range
Walter Bright via Dlang-internal
dlang-internal at puremagic.com
Thu Oct 20 22:11:38 PDT 2016
On 10/20/2016 9:44 AM, Dicebot wrote:
> This compiles too:
>
> ```
> int* x;
>
> @safe unittest
> {
> @safe static struct S
> {
> int data;
>
> int* borrow () return scope @trusted {
> return &this.data;
> }
> }
>
> S instance;
> x = instance.borrow();
> }
> ```
>
> Now, _this_ must be wrong, right?
It compiles because you added '@trusted' to the bad boy function, disabling the
check preventing taking the address of a 'ref'. The compiler has no reason to
believe that the return value of instance.borrow() has any relationship to
'instance' - it @trusted the programmer to not do that!
The practical result is that a container that is passed by ref can only control
its uses by returning by ref, not by *.
More information about the Dlang-internal
mailing list