Introducing Nullable Reference Types in C#. Is there hope for D, too?

Walter Bright newshound2 at digitalmars.com
Sat Nov 18 04:15:35 UTC 2017


On 11/16/2017 5:47 PM, Michael V. Franklin wrote:
> the lack of any warning or error for this trivial case surprised me.

Consider the following code:

   void test() {
     int* p;
     *p = 3;
   }

Compiling it with -O gives:

   Error: null dereference in function _D5test54testFNaZv

The -O is necessary because the optimizer uses data flow analysis, which makes 
the error easy to pick up.

Note that if the code were written:

   void test(int i) {
     int* p;
     if (i) p = &i;
     *p = 3;
   }

no error is diagnosed. Data flow analysis determines all paths, and some of 
those paths may (legitimately) never happen, and so giving an error would be 
spurious.


More information about the Digitalmars-d mailing list