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
{{ message }}
Repository navigation
Refinement with instanceof and generic unions is unsound #6741
If x: T | MyClass<T>, then Flow infers from x instanceof MyClass
that x: MyClass<T>. This is unsound: T could be MyClass<U> for
some other U, so that in fact x: MyClass<MyClass<U>>.
// @flowclassBox<T>{+field: T;constructor(x){this.field=x;}}functionasBox<T>(x: T | Box<T>): Box<T>{if(xinstanceofBox){// Here, `x` is refined to be `Box<T>`. This is unsound: `T` could// well be `Box<U>` for some other `U`. Example below.returnx;}else{// whateverreturnnewBox(x);}}conststringStringBox: Box<Box<string>>=asBox(newBox("wat"));// unsound!stringStringBox.field.field.substr(0);// runtime error
changed the title [-]Inference with `instanceof` and generic unions is unsound[/-][+]Refinement with `instanceof` and generic unions is unsound[/+]on Aug 15, 2018
Worth experimenting with using instantiate_poly instead, which creates an implicit type argument (i.e., some fresh tvar instead of any). But I haven't thought about this very hard and I wonder about perf.
Worth experimenting with using instantiate_poly instead, which creates an implicit type argument (i.e., some fresh tvar instead of any).
Makes sense; this sounds like the right semantics.
It would be nice for string | Box<T> to be refined to Box<T> instead of Box<?> under an instanceof check; it’s not clear to me whether instantiate_poly would admit that. But if it’s a tradeoff between that incompleteness and this unsoundness, I’d happily take the incompleteness.
Activity
wchargin commentedon Aug 14, 2018
Needless to say, this yields
coerce:[-]Inference with `instanceof` and generic unions is unsound[/-][+]Refinement with `instanceof` and generic unions is unsound[/+]jbrown215 commentedon Aug 20, 2018
cc @samwgoldman
jbrown215 commentedon Aug 20, 2018
After speaking to @samwgoldman , we think it's possible that we are refining x to
Box<any>. I'll take a look at this!Thank you for reporting, @wchargin!
samwgoldman commentedon Aug 20, 2018
Yeah, see https://github.com/facebook/flow/blob/7057f045695764d512a483ba2d5102bdaa06390a/src/typing/flow_js.ml#L3995
Worth experimenting with using
instantiate_polyinstead, which creates an implicit type argument (i.e., some fresh tvar instead ofany). But I haven't thought about this very hard and I wonder about perf.wchargin commentedon Aug 21, 2018
Makes sense; this sounds like the right semantics.
It would be nice for
string | Box<T>to be refined toBox<T>instead ofBox<?>under aninstanceofcheck; it’s not clear to me whetherinstantiate_polywould admit that. But if it’s a tradeoff between that incompleteness and this unsoundness, I’d happily take the incompleteness.