-
Notifications
You must be signed in to change notification settings - Fork 12.8k
CFA Assertions Not Active on Implicitly-Typed Function Calls #33580
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
I think this is working as intended, and the issue is that you are missing explicit type annotations.
This makes the error go away: const bar1: Bar = new Bar(); I will just put this here to annoy people. |
Ah, thanks for the link to the comment. That explains what is going on. Was it a specific decision to require explicit type annotations to "activate" assertions, or is it a technical restriction? Regardless, it would be nice to not have to add otherwise-unnecessary type annotations to get this to work. |
It's a technical restriction, otherwise control flow would have to do an unbounded number of passes to determine if a given function ever returned |
Alright, thanks for the quick and informative response @jack-williams and @RyanCavanaugh. While I do agree with Jack's comment on the PR that this will catch a lot of people, feel free to handle this issue however you see fit. |
I'm going to mark this working as intended, although we should probably discuss whether to issue errors when assertion functions and non-returning functions are used in a manner that doesn't get picked up by control flow analysis. See #32695 (comment). |
Yeah, an error with a corresponding annotation quick-fix would hopefully mitigate this? Or something to stem the expected tide of duplicates of this issue being filed and the analogous SO questions. |
My concern with an error would be libraries upgrading to use assertions and clients breaking because they didn't use a type annotation on previously OK code. |
Ok, with two issues reported in short order I'm convinced this merits an error as suggested by @jack-williams. I'm changing this back to a bug. |
I'm not concerned with issuing errors on non-CFA-recognized assertion function calls since assertions are new feature and no existing code uses them. The fact that we now report reachability errors following calls to never-returning functions is already a breaking change, so also issuing errors when such functions are called in a manner that CFA doesn't recognized doesn't materially change the situation. |
Makes sense - I overlooked the general changes you made to reachability analysis beyond |
TypeScript Version: 3.7.0-insiders.20190923 (this tgz from this comment)
Search Terms:
asserts
cfa
control flow analysis
Code
The
Bar.add
assertion works when called inside another method ofBar
but fails when called externally frommain
. I added a static function assertion to show that assertion still works otherwise inmain
.Expected behavior: In
main
afterbar1.add(foo1)
,foo1
should be of typeFoo & BarChild
.Actual behavior: In
main
afterbar1.add(foo1)
,foo1
is still typeFoo
.Playground Link: I've duplicated the sample code above in this playground link, but at the time of posting the nightly version does not have these changes.
Related Issues: #32695 This feature was merged to master yesterday, so instead of commenting on a closed PR I opened a new issue.
The text was updated successfully, but these errors were encountered: