Type of `foo ?? throw bar` is `Null<Foo>` instead of `Foo`
The type of x ?? throw "error" does not equal the non-null type of x.
Example code:
$type("abc" ?? throw "error");
Expected output: String
Actual output: Null<String>
Looking at #10745 this seems to be intentional which is odd. throw does not yield a value (same for return, break, and continue), so I would expect the result to be non-null.
I agree the type should be non-null if possible. It's a bit tricky to handle for various scenarios, including nullable right-hand and abstract casts, but we should look into it.
Did we ever discuss what should happen for something like 0 ?? throw "error" on static targets? Shouldn't that be a compiler error?
The current implementation of ?? just always Null-wraps the lhs, but it's not like that magically makes the run-time value nullable...
I think Null-wrapping is enough to make it work and to workaround annoying On static platforms, null can't be used as basic type error:
https://github.com/HaxeFoundation/haxe/pull/10428#issuecomment-967769956
I think this makes it nullable just for check, which is hidden allocation sometimes, it was decided in https://github.com/HaxeFoundation/haxe/pull/10428#issuecomment-971755513)
On other languages you mostly see The left operand can't be null, so the right operand is never executed warning for such cases, but this behavior in blocked in haxe by this:
var a = null;
a = 1;
$type(a); // Int
See this issue: https://github.com/HaxeFoundation/haxe/issues/7736
So you probably cannot generate a ?? b warnings/errors in typer, only in stricter nullsafety filter.