Shouldn't invalid references like this fail at compile time?

ag0aep6g anonymous at example.com
Tue Jan 23 01:08:19 UTC 2018


On 01/23/2018 01:20 AM, Mike Franklin wrote:
> I would be surprised if 
> the compiler allowed you to do something like this in `@safe` code.

You might get surprised then, if you expect the compiler to reject code 
like that statically.

If you add `@safe`, the compiler rejects this line:

     ls ~= &foo;

But that line would only be problematic if the pointer would leave the 
scope of the function. It doesn't, so this is actually safe. But the 
compiler isn't smart enough to see this.

The real question is about this line:

     p2 = ls[0];

That's an out-of-bounds access, and the compiler does not catch this 
statically. Instead, it inserts bounds-checking code that crashes the 
program safely with an `Error`.


More information about the Digitalmars-d mailing list