safety: null checks

Adam D. Ruppe destructionator at gmail.com
Mon Nov 23 02:51:24 UTC 2020


On Sunday, 22 November 2020 at 11:52:13 UTC, Dibyendu Majumdar 
wrote:
> Above fails because b is null. But why doesn't the compiler say 
> so? It seems like a very basic safety check.

Try this just for laughs:

remove the assert then add the -O switch.

$ dmd -betterC lole -O
lole.d(16): Error: null dereference in function main


(the assert actually disables this because the optimizer sees 
that and then figures the function must not be null.)

It just amuses me that dmd's *optimizer* actually catches this, 
but the debug build doesn't. Walter explained it is because the 
optimizer is analyzing the data flow anyway so it was a free 
check at that point, whereas on a normal or debug build it is a 
bunch of extra work for the implementation. Or something like 
that, don't trust my memory completely.

But yeah it makes me lol.


More information about the Digitalmars-d mailing list