[RFC] Throwing an exception with null pointers

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Wed Apr 16 23:16:55 UTC 2025


On 17/04/2025 11:08 AM, Derek Fawcus wrote:
> On Wednesday, 16 April 2025 at 19:44:09 UTC, Walter Bright wrote:
>> On 4/16/2025 11:43 AM, Derek Fawcus wrote:
>>> However I do have an interest in being able to write code with 
>>> distinct nullable and nonnull pointers.  That such that the compiler 
>>> (or an SA tool) can complain when they're incorrectly confused.
>>
>> That's what templates are for!
> 
> I can't say I've played with them much, having come from C not C++.
> 
> I see there is the ability to overload the unary '*' operator, and so 
> can imagine how one could define a struct providing a non-null form of 
> pointer.
> 
> But just how awkward is that going to be for mixed forms of nullability 
> in function definitions.  Without trying, i suspect it will just get too 
> awkward.
> 
> e.g., how would the equivalent of this args end up in a D rendition:
> 
> ```C
> int foo(char * _Nonnull * _Nullable a, int * _Nullable * _Nonnull b);
> ```

The current C++ analyzer for clang doesn't support that.

```c++
int foo(char ** _Nullable a, int ** _Nonnull b);
```

Would be more accurate.

For D, my design work for type state analysis would have it be:

```d
int foo(/*?initialized*/ char** a, ?nonnull int** b);
```

If you want to dereference twice, you need to perform a load + check.

```d
if (auto c = *b) {
	int d = *c;
}
```

Some syntax sugar can lower to that.



More information about the Digitalmars-d mailing list