[Issue 19183] DIP1000 defeated if auto used instead of scope in variable declaration with template this member function

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Aug 21 20:22:35 UTC 2018


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

--- Comment #6 from Atila Neves <atila.neves at gmail.com> ---
I typed the code from memory and made a mistake. Yes, it's supposed to be
`.ptr`. There's no way I can call core.stdc.stdlib.free without @trusted
_somewhere_, since `free` isn't @safe - free(42) is obviously going to crash.

What's @trusted is that I'm calling `free` on the same pointer I allocated in
the constructor, and the other thing needing checking is the postblit.

This all is besides the point, given the code below has no @trusted anywhere,
compiles, and shouldn't:

-------------------------------
@safe:

const(int)* gInts;

void main() {
    auto s = MyStruct();
    gInts = s.ptr;
}

struct MyStruct {
    int* _ints;
    auto ptr(this This)() { return _ints; }
}
-------------------------------


As in the original report:


* Adding a no-op destructor does nothing (i.e. the code still compiles):

~this() {} // ok but shouldn't be


* Adding a no-op destructor annotate with scope causes a compile-time error:

~this() scope { }

baz.d(7): Error: scope variable s assigned to gInts with longer lifetime


* With no destructor at all but replacing `auto` with `scope` also fails to
compile:

scope s = MyStruct();

baz.d(7): Error: scope variable s assigned to gInts with longer lifetime




Weirdly enough, changing `gInts` to a static array and returning &_ints[0] in
ptr also fails to compile;

--


More information about the Digitalmars-d-bugs mailing list