Skip to content

Make contravariant inferences only from pure contravariant positions #27357

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 4 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13460,6 +13460,7 @@ namespace ts {
let symbolStack: Symbol[];
let visited: Map<boolean>;
let contravariant = false;
let bivariant = false;
let propagationType: Type;
let allowComplexConstraintInference = true;
inferFromTypes(originalSource, originalTarget);
Expand Down Expand Up @@ -13546,11 +13547,13 @@ namespace ts {
}
if (priority === inference.priority) {
const candidate = propagationType || source;
if (contravariant) {
inference.contraCandidates = append(inference.contraCandidates, candidate);
// We make contravariant inferences only if we are in a pure contravariant position,
// i.e. only if we have not descended into a bivariant position.
if (contravariant && !bivariant) {
inference.contraCandidates = appendIfUnique(inference.contraCandidates, candidate);
}
else {
inference.candidates = append(inference.candidates, candidate);
inference.candidates = appendIfUnique(inference.candidates, candidate);
}
}
if (!(priority & InferencePriority.ReturnType) && target.flags & TypeFlags.TypeParameter && !isTypeParameterAtTopLevel(originalTarget, <TypeParameter>target)) {
Expand Down Expand Up @@ -13798,7 +13801,12 @@ namespace ts {

function inferFromSignature(source: Signature, target: Signature, skipParameters: boolean) {
if (!skipParameters) {
const saveBivariant = bivariant;
const kind = target.declaration ? target.declaration.kind : SyntaxKind.Unknown;
// Once we descend into a bivariant signature we remain bivariant for all nested inferences
bivariant = bivariant || kind === SyntaxKind.MethodDeclaration || kind === SyntaxKind.MethodSignature || kind === SyntaxKind.Constructor;
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this only need to be set on the specifically contravariant comparison within forEachMatchingParameterType? Also, could we not use a single variance variable for both bivariant and contravariant context tracking (seeing as we have a variance enum already)?

Copy link
Member Author

Choose a reason for hiding this comment

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

The contravariant flag continues to track whether the position is co- or contra-variant, regardless of whether we've descended into a bivariant position. Doing it this way, using two separate boolean variables, makes the logic that flips the flag easy. I looked at keeping it in a single variable but didn't like the complexity that came with it.

forEachMatchingParameterType(source, target, inferFromContravariantTypes);
bivariant = saveBivariant;
}
const sourceTypePredicate = getTypePredicateOfSignature(source);
const targetTypePredicate = getTypePredicateOfSignature(target);
Expand Down
17 changes: 17 additions & 0 deletions tests/baselines/reference/bivariantInferences.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [bivariantInferences.ts]
// Repro from #27337

interface Array<T> {
equalsShallow<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>): boolean;
}

declare const a: (string | number)[] | null[] | undefined[] | {}[];
declare const b: (string | number)[] | null[] | undefined[] | {}[];

let x = a.equalsShallow(b);


//// [bivariantInferences.js]
"use strict";
// Repro from #27337
var x = a.equalsShallow(b);
31 changes: 31 additions & 0 deletions tests/baselines/reference/bivariantInferences.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
=== tests/cases/conformance/types/typeRelationships/typeInference/bivariantInferences.ts ===
// Repro from #27337

interface Array<T> {
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(bivariantInferences.ts, 0, 0))
>T : Symbol(T, Decl(lib.es5.d.ts, --, --), Decl(bivariantInferences.ts, 2, 16))

equalsShallow<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>): boolean;
>equalsShallow : Symbol(Array.equalsShallow, Decl(bivariantInferences.ts, 2, 20))
>T : Symbol(T, Decl(bivariantInferences.ts, 3, 18))
>this : Symbol(this, Decl(bivariantInferences.ts, 3, 21))
>ReadonlyArray : Symbol(ReadonlyArray, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(bivariantInferences.ts, 3, 18))
>other : Symbol(other, Decl(bivariantInferences.ts, 3, 44))
>ReadonlyArray : Symbol(ReadonlyArray, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(bivariantInferences.ts, 3, 18))
}

declare const a: (string | number)[] | null[] | undefined[] | {}[];
>a : Symbol(a, Decl(bivariantInferences.ts, 6, 13))

declare const b: (string | number)[] | null[] | undefined[] | {}[];
>b : Symbol(b, Decl(bivariantInferences.ts, 7, 13))

let x = a.equalsShallow(b);
>x : Symbol(x, Decl(bivariantInferences.ts, 9, 3))
>a.equalsShallow : Symbol(equalsShallow, Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20))
>a : Symbol(a, Decl(bivariantInferences.ts, 6, 13))
>equalsShallow : Symbol(equalsShallow, Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20))
>b : Symbol(b, Decl(bivariantInferences.ts, 7, 13))

26 changes: 26 additions & 0 deletions tests/baselines/reference/bivariantInferences.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
=== tests/cases/conformance/types/typeRelationships/typeInference/bivariantInferences.ts ===
// Repro from #27337

interface Array<T> {
equalsShallow<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>): boolean;
>equalsShallow : <T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean
>this : ReadonlyArray<T>
>other : ReadonlyArray<T>
}

declare const a: (string | number)[] | null[] | undefined[] | {}[];
>a : (string | number)[] | null[] | undefined[] | {}[]
>null : null

declare const b: (string | number)[] | null[] | undefined[] | {}[];
>b : (string | number)[] | null[] | undefined[] | {}[]
>null : null

let x = a.equalsShallow(b);
>x : boolean
>a.equalsShallow(b) : boolean
>a.equalsShallow : (<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean) | (<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean) | (<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean) | (<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean)
>a : (string | number)[] | null[] | undefined[] | {}[]
>equalsShallow : (<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean) | (<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean) | (<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean) | (<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean)
>b : (string | number)[] | null[] | undefined[] | {}[]

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @strict: true

// Repro from #27337

interface Array<T> {
equalsShallow<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>): boolean;
}

declare const a: (string | number)[] | null[] | undefined[] | {}[];
declare const b: (string | number)[] | null[] | undefined[] | {}[];

let x = a.equalsShallow(b);