Skip to content

Refinement with instanceof and generic unions is unsound #6741

Open

Description

@wchargin

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

Here is an example of how this can go wrong:

// @flow
class Box<T> {
  +field: T;
  constructor(x) {
    this.field = x;
  }
}

function asBox<T>(x: T | Box<T>): Box<T> {
  if (x instanceof Box) {
    // Here, `x` is refined to be `Box<T>`. This is unsound: `T` could
    // well be `Box<U>` for some other `U`. Example below.
    return x;
  } else {
    // whatever
    return new Box(x);
  }
}

const stringStringBox: Box<Box<string>> = asBox(new Box("wat"));  // unsound!
stringStringBox.field.field.substr(0);  // runtime error

Activity

  1. wchargin commented on Aug 14, 2018

    @wchargin
    ContributorAuthor

    Needless to say, this yields coerce:

    // @flow
    function coerce<T, U>(t: T): U {
      class B<X> {
        x: X;
        constructor(x) {
          this.x = x;
        }
      }
      function b<X>(x: X | B<X>): B<X> {
        if (x instanceof B) return x;
        else throw new Error("Unreachable.");
      }
      const bb: B<B<{t: T}>> = b(new B({t}));
      if (!(bb.x instanceof B)) {
        return bb.x.t;
      }
      throw new Error("Unreachable.");
    }
    const twelve: number = coerce("twelve");
  2. changed the title [-]Inference with `instanceof` and generic unions is unsound[/-] [+]Refinement with `instanceof` and generic unions is unsound[/+] on Aug 15, 2018
  3. jbrown215 commented on Aug 20, 2018

    @jbrown215
    Contributor
  4. self-assigned this
    on Aug 20, 2018
  5. jbrown215 commented on Aug 20, 2018

    @jbrown215
    Contributor

    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!

  6. samwgoldman commented on Aug 20, 2018

    @samwgoldman
    Member

    Yeah, see https://github.com/facebook/flow/blob/7057f045695764d512a483ba2d5102bdaa06390a/src/typing/flow_js.ml#L3995

    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.

  7. wchargin commented on Aug 21, 2018

    @wchargin
    ContributorAuthor

    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.

  8. added
    Typing: soundnessNo false negatives (type checker claims that there is no error in the incorrect program)
    and removed
    Typing: soundnessNo false negatives (type checker claims that there is no error in the incorrect program)
    on Feb 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Typing: soundnessNo false negatives (type checker claims that there is no error in the incorrect program)bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions