Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type of foo ?? throw bar is Null<Foo> instead of Foo #10771

Closed
ALANVF opened this issue Aug 6, 2022 · 3 comments · Fixed by #10791
Closed

Type of foo ?? throw bar is Null<Foo> instead of Foo #10771

ALANVF opened this issue Aug 6, 2022 · 3 comments · Fixed by #10791

Comments

@ALANVF
Copy link
Contributor

ALANVF commented Aug 6, 2022

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.

@nadako
Copy link
Member

nadako commented Aug 7, 2022

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.

@Simn
Copy link
Member

Simn commented Aug 9, 2022

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...

@RblSb
Copy link
Member

RblSb commented Aug 9, 2022

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:
#10428 (comment)
I think this makes it nullable just for check, which is hidden allocation sometimes, it was decided in #10428 (comment))

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: #7736

So you probably cannot generate a ?? b warnings/errors in typer, only in stricter nullsafety filter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants