[Issue 4595] Accessing non-static member of a null reference compiles

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Aug 7 20:37:34 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4595


Jonathan M Davis <jmdavisProg at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg at gmail.com


--- Comment #5 from Jonathan M Davis <jmdavisProg at gmail.com> 2010-08-07 20:37:32 PDT ---
No offense, but relying on a segfault seems rather silly. And if you really
need one, I'm sure that you could easily produce one which the compiler can't
catch (probably all it would take would be a function which returned null being
used to initialize the variable rather than just declaring the variable).

I think that it's perfectly reasonable for dmd to catch simple and obvious
cases where a null reference or pointer is going to be dereferenced. In other
languages trying to be safe (such as Java or C#), they do enough code analysis
to scream at you if do something like that and force you to initialize the
variable. D goes the route of default initialization to the closest thing to an
error value rather than forcing you to initialize variables before they're
used, but I think that it's perfectly reasonable for the compiler to catch
simple and obvious cases and scream at you about them. I believe that the main
reason that dmd doesn't do it in the general case is because it's too hard for
the compiler to accurately catch more complex cases without making the compiler
much more complex in its code flow analysis (and Walter doesn't want that kind
of complexity).

There's no question that you cannot rely on the compiler to catch all of the
references and pointers that you forgot to initialize, but I see nothing wrong
with it catching some of them.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list