[RFC] Throwing an exception with null pointers

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Wed Apr 16 21:30:58 UTC 2025


On 17/04/2025 9:12 AM, Walter Bright wrote:
> On 4/16/2025 12:57 PM, Dave P. wrote:
>> You put a `#pragma clang assume_nonnull begin` at the top of your C/C+ 
>> +/Objective-C code and you have to annotate only the nullable 
>> pointers. Most pointers in a program should be non-null and the 
>> nullable ones should be the exception that you have to annotate.
> 
> Annotation means more than one pointer type.

Annotating the type, and annotating the variable/expression are two 
different things.

In the DFA literature they have distinct properties and are applied 
differently.

I read Principles of Program Analysis today, it was very interesting and 
did have some details on the subject (but not much). It also confirmed 
some things that I had already come up with independently which was nice!

 From what I've seen, application VM languages annotate the type, 
whereas C++ annotates the variable. As of DIP1000, we annotate the 
variable i.e. scope.

 From a link made previously in this thread, the state of the art 
annotation of nullability in C++: 
https://clang.llvm.org/docs/analyzer/developer-docs/nullability.html

Very similar to what I'm wanting.

> Back in the old MSDOS days, there were 5 pointer types - near, far, 
> stack, code and huge. Dealing with that is a gigantic mess - which 
> pointer type does strlen() take? Or worse, strcpy()?
> 
> Microsoft's Managed C++ has two pointer types with different syntax, a 
> GC pointer and a non-GC pointer. The same problem - what pointer type 
> does strcpy() accept?

I genuinely would prefer throwing a full fledged CFG DFA at this kind of 
thing, and only annotate the variable, not the type.

Its a shame not everyone would accept that as a solution.
It is forcing me to verify that there are no alternative solutions for 
these people.

I know you and I Walter would be happy with a full CFG DFA as the 
resolution, but alas.

I remain heavily concerned at the idea of boxing types in D, in any 
scenario. It seems to spell an absolute mess in any attempts I have 
modeled mentally.

> It's an ugly mess, and why I've avoided any such thing in D.
> 
> I'm curious - how does one traverse a binary tree with non-null 
> pointers? How does one create a circular data structure with non-null 
> pointers?

Sentinels.

They are used pretty heavily in data structures, such as head and foot 
nodes.

My recommendation for data structure/algorithm book: 
https://www.amazon.com/Algorithms-Parts-1-4-Fundamentals-Structures/dp/0201314525



More information about the Digitalmars-d mailing list