safety: null checks

Max Haughton maxhaton at
Mon Nov 23 00:52:59 UTC 2020

On Sunday, 22 November 2020 at 11:52:13 UTC, Dibyendu Majumdar 
> import core.stdc.stdio : printf;
> extern (C++) abstract class A {
>     void sayHello();
> }
> extern (C++) class B : A {
>     override void sayHello() {
>         printf("hello\n");
>     }
> }
> extern (C) void main() {
>     //scope b = new B;
>     B b;
>     assert(b);
>     b.sayHello();
> }
> Above fails because b is null. But why doesn't the compiler say 
> so? It seems like a very basic safety check.

Keep in mind that to find a null pointer, you must first invent 
the universe. This isn't a point about the halting problem, but 
rather than doing static analysis properly is complicated and dmd 
is already not particularly well structured.

You can use constant folding to find simple bugs like this, 
however, to do it in a clean manner requires a fair amount of 
thought to get right. Ideally you'd want to use some kind of 
complete design like abstract interpretation, but in reality 
you'll end up with @safe (minus the ownership aspects) where 
certain special cases are allowed.

More information about the Digitalmars-d mailing list