From 1ae0b461f88e9a68c2b2ad51519611a2cc292fec Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 11 Jan 2018 16:14:19 -0800 Subject: [PATCH 1/4] Use contra-variant inferences when co-variant inferences yield 'never' --- src/compiler/checker.ts | 50 ++++++++++++++++++++++++++--------------- src/compiler/types.ts | 21 +++++++++-------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index fd6a033653a..665b61be66b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11344,6 +11344,7 @@ namespace ts { return { typeParameter, candidates: undefined, + contraCandidates: undefined, inferredType: undefined, priority: undefined, topLevel: true, @@ -11355,6 +11356,7 @@ namespace ts { return { typeParameter: inference.typeParameter, candidates: inference.candidates && inference.candidates.slice(), + contraCandidates: inference.contraCandidates && inference.contraCandidates.slice(), inferredType: inference.inferredType, priority: inference.priority, topLevel: inference.topLevel, @@ -11468,6 +11470,7 @@ namespace ts { function inferTypes(inferences: InferenceInfo[], originalSource: Type, originalTarget: Type, priority: InferencePriority = 0) { let symbolStack: Symbol[]; let visited: Map; + let contravariant = false; inferFromTypes(originalSource, originalTarget); function inferFromTypes(source: Type, target: Type) { @@ -11535,18 +11538,20 @@ namespace ts { const inference = getInferenceInfoForType(target); if (inference) { if (!inference.isFixed) { - // We give lowest priority to inferences of implicitNeverType (which is used as the - // element type for empty array literals). Thus, inferences from empty array literals - // only matter when no other inferences are made. - const p = priority | (source === implicitNeverType ? InferencePriority.NeverType : 0); - if (!inference.candidates || p < inference.priority) { - inference.candidates = [source]; - inference.priority = p; + if (inference.priority === undefined || priority < inference.priority) { + inference.candidates = undefined; + inference.contraCandidates = undefined; + inference.priority = priority; } - else if (p === inference.priority) { - inference.candidates.push(source); + if (priority === inference.priority) { + if (contravariant) { + inference.contraCandidates = append(inference.contraCandidates, source); + } + else { + inference.candidates = append(inference.candidates, source); + } } - if (!(p & InferencePriority.ReturnType) && target.flags & TypeFlags.TypeParameter && !isTypeParameterAtTopLevel(originalTarget, target)) { + if (!(priority & InferencePriority.ReturnType) && target.flags & TypeFlags.TypeParameter && !isTypeParameterAtTopLevel(originalTarget, target)) { inference.topLevel = false; } } @@ -11569,15 +11574,15 @@ namespace ts { } } else if (source.flags & TypeFlags.Index && target.flags & TypeFlags.Index) { - priority ^= InferencePriority.Contravariant; + contravariant = !contravariant; inferFromTypes((source).type, (target).type); - priority ^= InferencePriority.Contravariant; + contravariant = !contravariant; } else if ((isLiteralType(source) || source.flags & TypeFlags.String) && target.flags & TypeFlags.Index) { const empty = createEmptyObjectTypeFromStringLiteral(source); - priority ^= InferencePriority.Contravariant; + contravariant = !contravariant; inferFromTypes(empty, (target as IndexType).type); - priority ^= InferencePriority.Contravariant; + contravariant = !contravariant; } else if (source.flags & TypeFlags.IndexedAccess && target.flags & TypeFlags.IndexedAccess) { inferFromTypes((source).objectType, (target).objectType); @@ -11646,9 +11651,9 @@ namespace ts { function inferFromContravariantTypes(source: Type, target: Type) { if (strictFunctionTypes) { - priority ^= InferencePriority.Contravariant; + contravariant = !contravariant; inferFromTypes(source, target); - priority ^= InferencePriority.Contravariant; + contravariant = !contravariant; } else { inferFromTypes(source, target); @@ -11827,10 +11832,19 @@ namespace ts { // If all inferences were made from contravariant positions, infer a common subtype. Otherwise, if // union types were requested or if all inferences were made from the return type position, infer a // union type. Otherwise, infer a common supertype. - const unwidenedType = inference.priority & InferencePriority.Contravariant ? getCommonSubtype(baseCandidates) : - context.flags & InferenceFlags.InferUnionTypes || inference.priority & InferencePriority.ReturnType ? getUnionType(baseCandidates, UnionReduction.Subtype) : + const unwidenedType = context.flags & InferenceFlags.InferUnionTypes || inference.priority & InferencePriority.ReturnType ? + getUnionType(baseCandidates, UnionReduction.Subtype) : getCommonSupertype(baseCandidates); inferredType = getWidenedType(unwidenedType); + // If we have inferred 'never' but have contravariant candidates. To get a more specific type we + // infer from the contravariant candidates instead. + if (inferredType.flags & TypeFlags.Never && inference.contraCandidates) { + inferredType = getCommonSubtype(inference.contraCandidates); + } + } + else if (inference.contraCandidates) { + // We only have contravariant inferences, infer the best common subtype of those + inferredType = getCommonSubtype(inference.contraCandidates); } else if (context.flags & InferenceFlags.NoDefault) { // We use silentNeverType as the wildcard that signals no inferences. diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 23798635671..7c9a7da69e6 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3772,20 +3772,19 @@ namespace ts { export type TypeMapper = (t: TypeParameter) => Type; export const enum InferencePriority { - Contravariant = 1 << 0, // Inference from contravariant position - NakedTypeVariable = 1 << 1, // Naked type variable in union or intersection type - MappedType = 1 << 2, // Reverse inference for mapped type - ReturnType = 1 << 3, // Inference made from return type of generic function - NeverType = 1 << 4, // Inference made from the never type + NakedTypeVariable = 1 << 0, // Naked type variable in union or intersection type + MappedType = 1 << 1, // Reverse inference for mapped type + ReturnType = 1 << 2, // Inference made from return type of generic function } export interface InferenceInfo { - typeParameter: TypeParameter; - candidates: Type[]; - inferredType: Type; - priority: InferencePriority; - topLevel: boolean; - isFixed: boolean; + typeParameter: TypeParameter; // Type parameter for which inferences are being made + candidates: Type[]; // Candidates in covariant positions (or undefined) + contraCandidates: Type[]; // Candidates in contravariant positions (or undefined) + inferredType: Type; // Cache for resolved inferred type + priority: InferencePriority; // Priority of current inference set + topLevel: boolean; // True if all inferences are to top level occurrences + isFixed: boolean; // True if inferences are fixed } export const enum InferenceFlags { From 6b882c7b39c061bbd2e250508f0b58320c699244 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 11 Jan 2018 16:14:45 -0800 Subject: [PATCH 2/4] Accept new baselines --- tests/baselines/reference/api/tsserverlibrary.d.ts | 9 ++++----- tests/baselines/reference/api/typescript.d.ts | 9 ++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 3eabea4435a..1aac7c893c5 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2149,15 +2149,14 @@ declare namespace ts { declaration?: SignatureDeclaration; } enum InferencePriority { - Contravariant = 1, - NakedTypeVariable = 2, - MappedType = 4, - ReturnType = 8, - NeverType = 16, + NakedTypeVariable = 1, + MappedType = 2, + ReturnType = 4, } interface InferenceInfo { typeParameter: TypeParameter; candidates: Type[]; + contraCandidates: Type[]; inferredType: Type; priority: InferencePriority; topLevel: boolean; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 630b7a08a28..3bf033798f9 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2149,15 +2149,14 @@ declare namespace ts { declaration?: SignatureDeclaration; } enum InferencePriority { - Contravariant = 1, - NakedTypeVariable = 2, - MappedType = 4, - ReturnType = 8, - NeverType = 16, + NakedTypeVariable = 1, + MappedType = 2, + ReturnType = 4, } interface InferenceInfo { typeParameter: TypeParameter; candidates: Type[]; + contraCandidates: Type[]; inferredType: Type; priority: InferencePriority; topLevel: boolean; From 13bf022ef67cb0f5a6a73d2a9a3aa2867305c7f7 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 11 Jan 2018 16:45:44 -0800 Subject: [PATCH 3/4] Add regression tests --- tests/cases/compiler/strictFunctionTypes1.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/cases/compiler/strictFunctionTypes1.ts b/tests/cases/compiler/strictFunctionTypes1.ts index 6c07bc51ffe..ad8836fcf05 100644 --- a/tests/cases/compiler/strictFunctionTypes1.ts +++ b/tests/cases/compiler/strictFunctionTypes1.ts @@ -17,3 +17,13 @@ const x1 = f1(fo, fs); // (x: string) => void const x2 = f2("abc", fo, fs); // "abc" const x3 = f3("abc", fo, fx); // "abc" | "def" const x4 = f4(fo, fs); // Func + +declare const never: never; + +const x10 = f2(never, fo, fs); // string +const x11 = f3(never, fo, fx); // "def" + +// Repro from #21112 + +declare function foo(a: ReadonlyArray): T; +let x = foo([]); // never From b6b936f2df617872e2ef9ee9f7346ef88e8f5792 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 11 Jan 2018 16:45:54 -0800 Subject: [PATCH 4/4] Accept new baselines --- .../reference/strictFunctionTypes1.js | 18 ++++++++++ .../reference/strictFunctionTypes1.symbols | 31 ++++++++++++++++ .../reference/strictFunctionTypes1.types | 35 +++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/tests/baselines/reference/strictFunctionTypes1.js b/tests/baselines/reference/strictFunctionTypes1.js index e802c443561..94ee06f88f8 100644 --- a/tests/baselines/reference/strictFunctionTypes1.js +++ b/tests/baselines/reference/strictFunctionTypes1.js @@ -15,6 +15,16 @@ const x1 = f1(fo, fs); // (x: string) => void const x2 = f2("abc", fo, fs); // "abc" const x3 = f3("abc", fo, fx); // "abc" | "def" const x4 = f4(fo, fs); // Func + +declare const never: never; + +const x10 = f2(never, fo, fs); // string +const x11 = f3(never, fo, fx); // "def" + +// Repro from #21112 + +declare function foo(a: ReadonlyArray): T; +let x = foo([]); // never //// [strictFunctionTypes1.js] @@ -23,6 +33,9 @@ var x1 = f1(fo, fs); // (x: string) => void var x2 = f2("abc", fo, fs); // "abc" var x3 = f3("abc", fo, fx); // "abc" | "def" var x4 = f4(fo, fs); // Func +var x10 = f2(never, fo, fs); // string +var x11 = f3(never, fo, fx); // "def" +var x = foo([]); // never //// [strictFunctionTypes1.d.ts] @@ -40,3 +53,8 @@ declare const x1: (x: string) => void; declare const x2 = "abc"; declare const x3: string; declare const x4: Func; +declare const never: never; +declare const x10: string; +declare const x11: "def"; +declare function foo(a: ReadonlyArray): T; +declare let x: never; diff --git a/tests/baselines/reference/strictFunctionTypes1.symbols b/tests/baselines/reference/strictFunctionTypes1.symbols index 70253411201..715ea287734 100644 --- a/tests/baselines/reference/strictFunctionTypes1.symbols +++ b/tests/baselines/reference/strictFunctionTypes1.symbols @@ -94,3 +94,34 @@ const x4 = f4(fo, fs); // Func >fo : Symbol(fo, Decl(strictFunctionTypes1.ts, 6, 58)) >fs : Symbol(fs, Decl(strictFunctionTypes1.ts, 8, 37)) +declare const never: never; +>never : Symbol(never, Decl(strictFunctionTypes1.ts, 17, 13)) + +const x10 = f2(never, fo, fs); // string +>x10 : Symbol(x10, Decl(strictFunctionTypes1.ts, 19, 5)) +>f2 : Symbol(f2, Decl(strictFunctionTypes1.ts, 0, 79)) +>never : Symbol(never, Decl(strictFunctionTypes1.ts, 17, 13)) +>fo : Symbol(fo, Decl(strictFunctionTypes1.ts, 6, 58)) +>fs : Symbol(fs, Decl(strictFunctionTypes1.ts, 8, 37)) + +const x11 = f3(never, fo, fx); // "def" +>x11 : Symbol(x11, Decl(strictFunctionTypes1.ts, 20, 5)) +>f3 : Symbol(f3, Decl(strictFunctionTypes1.ts, 1, 74)) +>never : Symbol(never, Decl(strictFunctionTypes1.ts, 17, 13)) +>fo : Symbol(fo, Decl(strictFunctionTypes1.ts, 6, 58)) +>fx : Symbol(fx, Decl(strictFunctionTypes1.ts, 9, 37)) + +// Repro from #21112 + +declare function foo(a: ReadonlyArray): T; +>foo : Symbol(foo, Decl(strictFunctionTypes1.ts, 20, 30)) +>T : Symbol(T, Decl(strictFunctionTypes1.ts, 24, 21)) +>a : Symbol(a, Decl(strictFunctionTypes1.ts, 24, 24)) +>ReadonlyArray : Symbol(ReadonlyArray, Decl(lib.d.ts, --, --)) +>T : Symbol(T, Decl(strictFunctionTypes1.ts, 24, 21)) +>T : Symbol(T, Decl(strictFunctionTypes1.ts, 24, 21)) + +let x = foo([]); // never +>x : Symbol(x, Decl(strictFunctionTypes1.ts, 25, 3)) +>foo : Symbol(foo, Decl(strictFunctionTypes1.ts, 20, 30)) + diff --git a/tests/baselines/reference/strictFunctionTypes1.types b/tests/baselines/reference/strictFunctionTypes1.types index 9701d78cff0..b915e0b5f6b 100644 --- a/tests/baselines/reference/strictFunctionTypes1.types +++ b/tests/baselines/reference/strictFunctionTypes1.types @@ -100,3 +100,38 @@ const x4 = f4(fo, fs); // Func >fo : (x: Object) => void >fs : (x: string) => void +declare const never: never; +>never : never + +const x10 = f2(never, fo, fs); // string +>x10 : string +>f2(never, fo, fs) : string +>f2 : (obj: T, f1: (x: T) => void, f2: (x: T) => void) => T +>never : never +>fo : (x: Object) => void +>fs : (x: string) => void + +const x11 = f3(never, fo, fx); // "def" +>x11 : "def" +>f3(never, fo, fx) : "def" +>f3 : (obj: T, f1: (x: T) => void, f2: (f: (x: T) => void) => void) => T +>never : never +>fo : (x: Object) => void +>fx : (f: (x: "def") => void) => void + +// Repro from #21112 + +declare function foo(a: ReadonlyArray): T; +>foo : (a: ReadonlyArray) => T +>T : T +>a : ReadonlyArray +>ReadonlyArray : ReadonlyArray +>T : T +>T : T + +let x = foo([]); // never +>x : never +>foo([]) : never +>foo : (a: ReadonlyArray) => T +>[] : never[] +