-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Infer variables used in nested function are not null if already proven not null in parent function #32339
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
Comments
Duplicate of #9998? |
@jcalz Nah, it's a little different I'm pretty sure - #9998 is a design limitation mostly to do with whether narrowings should be preserved across function calls. In the case here, a narrowed type is being re-widened at a closure boundary, which is AFAIK fully intentional and by design. The function call itself has nothing (directly) to do with it. Either way though, this is almost certainly a wontfix. |
One of the principles of predictability (and performance) is that the checking of a function body doesn't change based on when or how the function is called. This kind of behavior would only be useful and performant for functions which are called exactly once, which are the kind of functions that don't need to exist in the first place. |
Thanks for the explanation...interesting. So, strangely, but I think in accordance with what you're saying, the compiler doesn't actually have a problem with this (however, it fails when run).
I'm having trouble understanding this part. Why does the number of times a function is called matter? And when you say "performant", are you talking about the speed at which the code runs or the speed at which the code is compiled? Here's an example where the nested function is called multiple times. It doesn't compile for the same reason as in my original example.
In what sense are performance and predictability different in this case vs. Edit Interestingly, the arrow function version of this does compile.
Now I'm really confused :) |
Yes, arrow functions are expressions so they are part of the control flow graph. Function declarations on the other hand get hoisted so they are completely outside the CF graph of the containing scope, see discussion about that here: #32300 |
Ah, it does make sense that function expressions (regardless of whether arrow) get evaluated differently than function declarations. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Search Terms
Suggestion
Allow this to compile without error:
Currently, the compiler complains that the
x
inx + 3
could be null, even though that check was performed in the parent function.View in playground
Use Cases
!
) operator unnecessarily.Examples
(see above)
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: