You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now it correctly reasons about exhaustive switch statements.
We used to detect this only syntactically - if you have a switch as the last statement, we would remove undefined from the return type.
But that wasn't sufficient. People kept complaining that it didn't catch specialized cases.
Leveraging the general Control Flow Analysis (CFA) logic is really the best way to do this.
This also means we can do well on exhaustive if statements.
functionf44(x: "a"|"b"){if(someCondition()){switch(x){case"a": return100;case"b": return200;}// now considered unreachable,// return type doesn't include 'undefined' anymore.x;}throw"oops"}
Caveat: by adding this "smarter" logic, we consider code that's meant to be defensive against unchecked JS code to be "unreachable".
functionf46(x: "a"|"b"){switch(x){case"a": return100;case"b": return200;}// now considered unreachable - but// a JS user could've passed in "c"!throw"oops"}
The way to fix this is to add a default to the switch/statement.
function f46(x: "a" | "b") {
switch (x) {
case "a": return 100;
case "b": return 200;
+ default: // empty default
}
// now considered unreachable - but
// a JS user could've passed in "c"!
throw "oops"
}
Does add about 2% overhead on the checker... are committed to optimizing it.
A while back, we added a class of caching for class of code that does thousands and thousands of assignments with arithmetic.
If you need to re-evaluate the types at each assignment site, then this blows up the control flow graph and you have to traverse up the graph each time.
So we added caching.
This change makes it so that we only cache when we hit re-entrancy because it's usually not necessary to begin with.
Does this mean that the lookup of every narrowed type gets invoked at least twice?
[thinks] No?
Does this mean that we only cache every other node?
Maybe. It sounds like that may or may not be the intended effect of the code.
Caching for control flow paths that precede loops.
Also find a way to cache when re-entrancy before a loop.
Control Flow Updates
Assertions in Control Flow
#32695
switch
as the last statement, we would removeundefined
from the return type.if
statements.default
to theswitch
/statement.function f46(x: "a" | "b") { switch (x) { case "a": return 100; case "b": return 200; + default: // empty default } // now considered unreachable - but // a JS user could've passed in "c"! throw "oops" }
Improved Caching in CFA
#33510
Public Class Field Semantics Update
#33509
declare
or in.d.ts
files) to have accessors..d.ts
files.legacyClassFields
to opt in to old behavior.true
by default in 3.7The text was updated successfully, but these errors were encountered: