Skip to content

Port union reduction to 1.7 #5919

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
Dec 4, 2015
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"browserify": "latest",
"istanbul": "latest",
"mocha-fivemat-progress-reporter": "latest",
"tslint": "latest",
"tslint": "~2",
"tsd": "latest"
},
"scripts": {
Expand Down
92 changes: 57 additions & 35 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ namespace ts {
let emptyArray: any[] = [];
let emptySymbols: SymbolTable = {};

let jsxElementClassType: Type = undefined;

let compilerOptions = host.getCompilerOptions();
let languageVersion = compilerOptions.target || ScriptTarget.ES3;
let modulekind = compilerOptions.module ? compilerOptions.module : languageVersion === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.None;
Expand Down Expand Up @@ -4938,9 +4940,6 @@ namespace ts {
}
return objectTypeRelatedTo(<ObjectType>source, <ObjectType>target, /*reportErrors*/ false);
}
if (source.flags & TypeFlags.TypeParameter && target.flags & TypeFlags.TypeParameter) {
return typeParameterIdenticalTo(<TypeParameter>source, <TypeParameter>target);
}
if (source.flags & TypeFlags.Union && target.flags & TypeFlags.Union ||
source.flags & TypeFlags.Intersection && target.flags & TypeFlags.Intersection) {
if (result = eachTypeRelatedToSomeType(<UnionOrIntersectionType>source, <UnionOrIntersectionType>target)) {
Expand Down Expand Up @@ -5071,20 +5070,6 @@ namespace ts {
return result;
}

function typeParameterIdenticalTo(source: TypeParameter, target: TypeParameter): Ternary {
if (source.symbol.name !== target.symbol.name) {
return Ternary.False;
}
// covers case when both type parameters does not have constraint (both equal to noConstraintType)
if (source.constraint === target.constraint) {
return Ternary.True;
}
if (source.constraint === noConstraintType || target.constraint === noConstraintType) {
return Ternary.False;
}
return isIdenticalTo(source.constraint, target.constraint);
}

// Determine if two object types are related by structure. First, check if the result is already available in the global cache.
// Second, check if we have already started a comparison of the given two types in which case we assume the result to be true.
// Third, check if both types are part of deeply nested chains of generic type instantiations and if so assume the types are
Expand Down Expand Up @@ -5607,27 +5592,20 @@ namespace ts {
return Ternary.False;
}
}
let result = Ternary.True;
if (source.typeParameters && target.typeParameters) {
if (source.typeParameters.length !== target.typeParameters.length) {
return Ternary.False;
}
for (let i = 0, len = source.typeParameters.length; i < len; ++i) {
let related = compareTypes(source.typeParameters[i], target.typeParameters[i]);
if (!related) {
return Ternary.False;
}
result &= related;
}
}
else if (source.typeParameters || target.typeParameters) {
// Check that the two signatures have the same number of type parameters. We might consider
// also checking that any type parameter constraints match, but that would require instantiating
// the constraints with a common set of type arguments to get relatable entities in places where
// type parameters occur in the constraints. The complexity of doing that doesn't seem worthwhile,
// particularly as we're comparing erased versions of the signatures below.
if ((source.typeParameters ? source.typeParameters.length : 0) !== (target.typeParameters ? target.typeParameters.length : 0)) {
return Ternary.False;
}
// Spec 1.0 Section 3.8.3 & 3.8.4:
// M and N (the signatures) are instantiated using type Any as the type argument for all type parameters declared by M and N
source = getErasedSignature(source);
target = getErasedSignature(target);
let targetLen = target.parameters.length;
let result = Ternary.True;
const targetLen = target.parameters.length;
for (let i = 0; i < targetLen; i++) {
let s = isRestParameterIndex(source, i) ? getRestTypeOfSignature(source) : getTypeOfSymbol(source.parameters[i]);
let t = isRestParameterIndex(target, i) ? getRestTypeOfSignature(target) : getTypeOfSymbol(target.parameters[i]);
Expand Down Expand Up @@ -5928,6 +5906,17 @@ namespace ts {
}

function inferFromTypes(source: Type, target: Type) {
if (source.flags & TypeFlags.Union && target.flags & TypeFlags.Union ||
source.flags & TypeFlags.Intersection && target.flags & TypeFlags.Intersection) {
// Source and target are both unions or both intersections. To improve the quality of
// inferences we first reduce the types by removing constituents that are identically
// matched by a constituent in the other type. For example, when inferring from
// 'string | string[]' to 'string | T', we reduce the types to 'string[]' and 'T'.
const reducedSource = reduceUnionOrIntersectionType(<UnionOrIntersectionType>source, <UnionOrIntersectionType>target);
const reducedTarget = reduceUnionOrIntersectionType(<UnionOrIntersectionType>target, <UnionOrIntersectionType>source);
source = reducedSource;
target = reducedTarget;
}
if (target.flags & TypeFlags.TypeParameter) {
// If target is a type parameter, make an inference, unless the source type contains
// the anyFunctionType (the wildcard type that's used to avoid contextually typing functions).
Expand All @@ -5938,8 +5927,7 @@ namespace ts {
if (source.flags & TypeFlags.ContainsAnyFunctionType) {
return;
}

let typeParameters = context.typeParameters;
const typeParameters = context.typeParameters;
for (let i = 0; i < typeParameters.length; i++) {
if (target === typeParameters[i]) {
let inferences = context.inferences[i];
Expand Down Expand Up @@ -6086,6 +6074,41 @@ namespace ts {
}
}

function typeIdenticalToSomeType(source: Type, target: UnionOrIntersectionType): boolean {
for (const t of target.types) {
if (isTypeIdenticalTo(source, t)) {
return true;
}
}
return false;
}

/**
* Return the reduced form of the source type. This type is computed by by removing all source
* constituents that have an identical match in the target type.
*/
function reduceUnionOrIntersectionType(source: UnionOrIntersectionType, target: UnionOrIntersectionType) {
let sourceTypes = source.types;
let sourceIndex = 0;
let modified = false;
while (sourceIndex < sourceTypes.length) {
if (typeIdenticalToSomeType(sourceTypes[sourceIndex], target)) {
if (!modified) {
sourceTypes = sourceTypes.slice(0);
modified = true;
}
sourceTypes.splice(sourceIndex, 1);
}
else {
sourceIndex++;
}
}
if (modified) {
return source.flags & TypeFlags.Union ? getUnionType(sourceTypes, /*noSubtypeReduction*/ true) : getIntersectionType(sourceTypes);
}
return source;
}

function getInferenceCandidates(context: InferenceContext, index: number): Type[] {
let inferences = context.inferences[index];
return inferences.primary || inferences.secondary || emptyArray;
Expand Down Expand Up @@ -7859,7 +7882,6 @@ namespace ts {
return prop || unknownSymbol;
}

let jsxElementClassType: Type = undefined;
function getJsxGlobalElementClassType(): Type {
if (!jsxElementClassType) {
jsxElementClassType = getExportedTypeFromNamespace(JsxNames.JSX, JsxNames.ElementClass);
Expand Down
32 changes: 32 additions & 0 deletions tests/baselines/reference/genericSignatureIdentity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [genericSignatureIdentity.ts]
// This test is here to remind us of our current limits of type identity checking.
// Ideally all of the below declarations would be considered different (and thus errors)
// but they aren't because we erase type parameters to type any and don't check that
// constraints are identical.

var x: {
<T extends Date>(x: T): T;
};

var x: {
<T extends number>(x: T): T;
};

var x: {
<T>(x: T): T;
};

var x: {
<T>(x: any): any;
};


//// [genericSignatureIdentity.js]
// This test is here to remind us of our current limits of type identity checking.
// Ideally all of the below declarations would be considered different (and thus errors)
// but they aren't because we erase type parameters to type any and don't check that
// constraints are identical.
var x;
var x;
var x;
var x;
49 changes: 49 additions & 0 deletions tests/baselines/reference/genericSignatureIdentity.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
=== tests/cases/compiler/genericSignatureIdentity.ts ===
// This test is here to remind us of our current limits of type identity checking.
// Ideally all of the below declarations would be considered different (and thus errors)
// but they aren't because we erase type parameters to type any and don't check that
// constraints are identical.

var x: {
>x : Symbol(x, Decl(genericSignatureIdentity.ts, 5, 3), Decl(genericSignatureIdentity.ts, 9, 3), Decl(genericSignatureIdentity.ts, 13, 3), Decl(genericSignatureIdentity.ts, 17, 3))

<T extends Date>(x: T): T;
>T : Symbol(T, Decl(genericSignatureIdentity.ts, 6, 5))
>Date : Symbol(Date, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(genericSignatureIdentity.ts, 6, 21))
>T : Symbol(T, Decl(genericSignatureIdentity.ts, 6, 5))
>T : Symbol(T, Decl(genericSignatureIdentity.ts, 6, 5))

};

var x: {
>x : Symbol(x, Decl(genericSignatureIdentity.ts, 5, 3), Decl(genericSignatureIdentity.ts, 9, 3), Decl(genericSignatureIdentity.ts, 13, 3), Decl(genericSignatureIdentity.ts, 17, 3))

<T extends number>(x: T): T;
>T : Symbol(T, Decl(genericSignatureIdentity.ts, 10, 5))
>x : Symbol(x, Decl(genericSignatureIdentity.ts, 10, 23))
>T : Symbol(T, Decl(genericSignatureIdentity.ts, 10, 5))
>T : Symbol(T, Decl(genericSignatureIdentity.ts, 10, 5))

};

var x: {
>x : Symbol(x, Decl(genericSignatureIdentity.ts, 5, 3), Decl(genericSignatureIdentity.ts, 9, 3), Decl(genericSignatureIdentity.ts, 13, 3), Decl(genericSignatureIdentity.ts, 17, 3))

<T>(x: T): T;
>T : Symbol(T, Decl(genericSignatureIdentity.ts, 14, 5))
>x : Symbol(x, Decl(genericSignatureIdentity.ts, 14, 8))
>T : Symbol(T, Decl(genericSignatureIdentity.ts, 14, 5))
>T : Symbol(T, Decl(genericSignatureIdentity.ts, 14, 5))

};

var x: {
>x : Symbol(x, Decl(genericSignatureIdentity.ts, 5, 3), Decl(genericSignatureIdentity.ts, 9, 3), Decl(genericSignatureIdentity.ts, 13, 3), Decl(genericSignatureIdentity.ts, 17, 3))

<T>(x: any): any;
>T : Symbol(T, Decl(genericSignatureIdentity.ts, 18, 5))
>x : Symbol(x, Decl(genericSignatureIdentity.ts, 18, 8))

};

49 changes: 49 additions & 0 deletions tests/baselines/reference/genericSignatureIdentity.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
=== tests/cases/compiler/genericSignatureIdentity.ts ===
// This test is here to remind us of our current limits of type identity checking.
// Ideally all of the below declarations would be considered different (and thus errors)
// but they aren't because we erase type parameters to type any and don't check that
// constraints are identical.

var x: {
>x : <T extends Date>(x: T) => T

<T extends Date>(x: T): T;
>T : T
>Date : Date
>x : T
>T : T
>T : T

};

var x: {
>x : <T extends Date>(x: T) => T

<T extends number>(x: T): T;
>T : T
>x : T
>T : T
>T : T

};

var x: {
>x : <T extends Date>(x: T) => T

<T>(x: T): T;
>T : T
>x : T
>T : T
>T : T

};

var x: {
>x : <T extends Date>(x: T) => T

<T>(x: any): any;
>T : T
>x : any

};

Loading