memory safety checks and trust

Adam D. Ruppe destructionator at gmail.com
Sat Apr 11 01:21:56 UTC 2020


```
void main() {
         int a;
         b ~= &a;
}

int*[] b;
```

trust.d(3): Error: copying & a into allocated memory escapes a 
reference to local variable a


(Interestingly, `b = [&a]` instead of ~= passes muster. What's 
the difference? Just another bug in this?)


But the inconsistency isn't why I'm posting right now, it is my 
fear that D is losing faith in me. There seems to be no way to 
say "trust me" to the compiler. Note that I'm not even using any 
switches to dmd, and adding @trusted had no effect.

One of the nice things about D is that I very rarely feel like I 
am fighting the language (unless I'm working under constraints 
like -w and @safe pure nothrow @nogc stuff, but that's why I just 
don't use those!). From low-level bit twiddling to high level 
"just make it work", the D language usually works with me, a few 
exceptions excluded - but when those happen, you can cast or 
whatever to tell it to trust me.

But this new thing... can I tell it to trust me? Is that just a 
bug too? Or will I have to trick it with extern(C) or asm or 
something?

(PS I actually changed the code somewhat and used a pointer to a 
static instance which worked for my specific case. But I'm a bit 
concerned about the future.)


More information about the Digitalmars-d mailing list