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

Unsoundness when mutable objects are used (`false` has type `true`) #6893

Open
aij opened this Issue Sep 14, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@aij

aij commented Sep 14, 2018

I think this is due to a problem with unification: (Tested on v0.80.0)

const a = {x: true};         // A mutable object.
const b:{x: true} = a;       // Works
const c:{x: boolean} = a;    // Also works...
c.x = false;                 // Uh oh. This can't be good...
const derp:true = b.x;       // Egads! false:true

Try it

I was trying to explain how in a Hindley-Milner style type inference system, subsequent type annotations will influence the types inferred earlier through unification, but it wasn't working as I expected. Ordering shouldn't matter of course, but changing the order of b and c above does produce a type error as would be expected of a sound type system.

#5895 may or may not be more than tangentially related...

@wchargin

This comment has been minimized.

Show comment
Hide comment
@wchargin

wchargin Sep 14, 2018

Contributor

Just to be clear, this yields coerce:

// @flow
function coerce<T, U>(t: T): U {
  class Unique {}
  type Box<E> = {x: E};
  const box = {x: new Unique()};
  const xob = (box: Box<U | Unique>);
  (box: Box<mixed>);
  box.x = t;
  if (xob.x instanceof Unique) throw new Error("Unreachable.");
  return xob.x;
}

(Thanks for posting.)

Contributor

wchargin commented Sep 14, 2018

Just to be clear, this yields coerce:

// @flow
function coerce<T, U>(t: T): U {
  class Unique {}
  type Box<E> = {x: E};
  const box = {x: new Unique()};
  const xob = (box: Box<U | Unique>);
  (box: Box<mixed>);
  box.x = t;
  if (xob.x instanceof Unique) throw new Error("Unreachable.");
  return xob.x;
}

(Thanks for posting.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment