Friends don't let friends use inout with scope and -dip1000

Atila Neves atila.neves at gmail.com
Mon Aug 20 09:31:09 UTC 2018


On Friday, 17 August 2018 at 13:39:29 UTC, Steven Schveighoffer 
wrote:
> On 8/17/18 3:36 AM, Atila Neves wrote:
>> Here's a struct:
>> 
>> -----------------
>> struct MyStruct {
>>      import core.stdc.stdlib;
>>      int* ints;
>>      this(int size) @trusted { ints = cast(int*) malloc(size); 
>> }
>>      ~this() @trusted { free(ints); }
>>      scope int* ptr() { return ints; }
>> }
>> -----------------
>> 
>> Let's try and be evil with -dip1000:
>> 
>> -----------------
>> @safe:
>> 
>> // struct MyStruct ...
>> 
>> const(int) *gInt;
>> 
>> void main() {
>>      auto s = MyStruct(10);
>>      gInt = s.ptr;
>> }
>> -----------------
>> 
>> % dmd -dip1000 scope_inout.d
>> scope_inout.d(26): Error: scope variable this may not be 
>> returned
>> 
>> 
>> Yay!
>> 
>> What if instead of `auto` I write `const` instead (or 
>> immutable)? This is D we're talking about, so none of this 
>> boilerplate nonsense of writing two (or three) basically 
>> identical functions. So:
>> 
>> -----------------
>> // used to be scope int* ptr() { return ints; }
>> scope inout(int)* ptr() inout { return ints; }
>
> Does scope apply to the return value or the `this` reference?

I assumed the return value. I think I've read DIP1000 about a 
dozen times now and I still get confused. As opposed to `const` 
or `immutable`, `scope(T)` isn't a thing so... I don't know?

> What happens if you remove the return type? (i.e. scope auto)

And write what instead?

>
>> -----------------
>> 
>> % dmd -dip1000 scope_inout.d
>> % echo $?
>> 0
>> # nope, no error here
>> 
>> Wait, what? Turns out now it compiles. After some 
>> under-the-breath mumbling I go hit issues.dlang.org and 
>> realise that the issue already exists:
>> 
>> 
>> https://issues.dlang.org/show_bug.cgi?id=17935
>
> I don't see what this bug report has to do with the given case.

That's because I'm an idiot and I meant this one:

https://issues.dlang.org/show_bug.cgi?id=17927


> This seems like a straight up bug.

I agree, but I also think #17935 is a straight up bug as well...

> This doesn't surprise me. I'm beginning to question whether 
> scope shouldn't have been a type constructor instead of a 
> storage class. It's treated almost like a type constructor in 
> most places, but the language grammar makes it difficult to be 
> specific as to what part it applies.

I'm so confused it's not even funny.




More information about the Digitalmars-d mailing list