Skip to content

Improve variance measurement - #36261

Merged
Anders Hejlsberg (ahejlsberg) merged 3 commits into
masterfrom
fix35805
Jan 17, 2020
Merged

Anders Hejlsberg (ahejlsberg) merged 3 commits into
masterfrom
fix35805

Conversation

@ahejlsberg

Copy link
Copy Markdown
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
Copy Markdown
Member Author

@typescript-bot

TypeScript Bot (typescript-bot) commented Jan 17, 2020 •

Copy link
Copy Markdown
Contributor

Heya Anders Hejlsberg (@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

TypeScript Bot (typescript-bot) commented Jan 17, 2020 •

Copy link
Copy Markdown
Contributor

Heya Anders Hejlsberg (@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

TypeScript Bot (typescript-bot) commented Jan 17, 2020 •

Copy link
Copy Markdown
Contributor

Heya Anders Hejlsberg (@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.

@weswigham Wesley Wigham (weswigham) left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
Contributor

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
Copy Markdown
Member Author

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

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

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