Skip to content

Improve variance measurement #36261

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

Merged
merged 3 commits into from
Jan 17, 2020
Merged

Improve variance measurement #36261

merged 3 commits into from
Jan 17, 2020

Conversation

ahejlsberg
Copy link
Member

This PR improves variance measurement for generic types. Previously we'd default to covariance for recursive references to the same type during variance measurement. Now we instead assume recursive references to be "possibly related" (signaled using Ternary.Maybe), similarly to what happens when we hit the recursion depth limiter. This effectively means we measure variance only from type parameter occurrences that aren't nested in recursive instantiations of the generic type.

Previously, a type like the following:

interface Foo<T> {
  f: (x: T) => void;
  g: Foo<Array<T>>;
}

would be measured as invariant in T: We would correctly measure the reference to T in the f property as contravariant, but then the recursive reference to Foo in the g property would default to covariance and we'd end up with a combined invariant result. We now correctly measure the type as contravariant in T:

declare const f1: Foo<string>;
const f2: Foo<'a'> = f1;  // Ok
const f3: Foo<unknown> = f1;  // Error

We furthermore now correctly measure unwitnessed type parameters in recursive references as independent:

interface Bar<T> {
  g: Bar<Array<T>>;
}

declare const b1: Bar<string>;
const b2: Bar<number> = b1;  // Ok

We could potentially further improve variance measurement by structurally exploring recursive references to some depth other than zero, but we already know that the limit of 5 imposed by the recursion depth limiter is too high to prevent runaway recursion in some of our test suites.

Fixes #35805.

@ahejlsberg
Copy link
Member Author

@typescript-bot test this
@typescript-bot user test this
@typescript-bot run dt

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 17, 2020

Heya @ahejlsberg, I've started to run the extended test suite on this PR at b453606. You can monitor the build here. It should now contribute to this PR's status checks.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 17, 2020

Heya @ahejlsberg, I've started to run the parallelized community code test suite on this PR at b453606. You can monitor the build here. It should now contribute to this PR's status checks.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 17, 2020

Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at b453606. You can monitor the build here. It should now contribute to this PR's status checks.

Copy link
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems straightforward to me.

@typescript-bot
Copy link
Collaborator

The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master.

@ahejlsberg
Copy link
Member Author

Community test failures appear to be preexisting conditions. Other tests are clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Function-like recursive generic interface invariant in its parameter type
3 participants