On Mon, 23 Aug 2010 16:30:15 -0400, Adam B <cru...@gmail.com> wrote:

I was pondering bearophile's idea of having compile-time null
checking.  Since adding new language syntax is often a hard thing to
sell to the language authors, perhaps we could get by with just a
compiler warning?  Some static code analysis.  For example:

--------------
char[] foo()
{
       if (some condition)
            return "yada";
       else
return null; //compiler makes note that foo() might return null
}

void bar()
{
     char[] s = foo();
     printf(s);   //compiler issues warning because 's' has not been
null-checked
}
--------------

Or, if that's too heavy for the compiler, it could be done in a
separate program.  (Is there a Lint like program for D?)

This kind of analysis is not possible by the compiler because of the compilation model. Essentially, it's possible to force the compiler to compile bar without having access to the source code of foo. Without access to the source code, it cannot tell whether the result will be null or not.

The object file format does not provide places to store such metadata (whether a function returns null or not), so it's not possible to resolve this. There are other compilation models which allow storage of metadata, and I'm in favor of having such a system, but it would be a drastic change from the current model. But essentially such a model allows complex analysis of the code (including full escape analysis), and also allows you to avoid sync problems such as compiling against newer sources but linking with stale objects.

-Steve

Reply via email to