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

Mike Franklin slavo5150 at yahoo.com
Tue Jan 23 02:25:57 UTC 2018


On Tuesday, 23 January 2018 at 01:08:19 UTC, ag0aep6g wrote:

>
> 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`.

Due to the aforementioned bugs in my prior posts, I couldn't even 
make an example to demonstrate in @safe code, so I modified the 
example slightly in an effort to reproduce the same problem.

import std.stdio;

void main() @safe
{
     string foo = "foo";
     string* ls0;
     string* p1, p2;

     ls0 = &foo;
     p1 = ls0;
     ls0.destroy();
     p2 = ls0;
     writeln(p2.length);
}

Error: program killed by signal 11

https://run.dlang.io/is/ecYAKZ

Yeah, that's pretty poopy.

Not sure how to precisely define the problem here.  Should 
`destroy` be `@system` so it can't be called in `@safe` code, or 
should the compiler be smart enough to figure out the flow 
control and throw an error?

Mike




More information about the Digitalmars-d mailing list