Skip to content
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

Remove empty intersection types in union types #18438

Merged
merged 6 commits into from
Sep 14, 2017
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
24 changes: 22 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7289,6 +7289,22 @@ namespace ts {
return binarySearchTypes(types, type) >= 0;
}

// Return true if the given intersection type contains (a) more than one unit type or (b) an object
// type and a nullable type (null or undefined).
function isEmptyIntersectionType(type: IntersectionType) {
let combined: TypeFlags = 0;
for (const t of type.types) {
if (t.flags & TypeFlags.Unit && combined & TypeFlags.Unit) {
return true;
}
combined |= t.flags;
if (combined & TypeFlags.Nullable && combined & (TypeFlags.Object | TypeFlags.NonPrimitive)) {
return true;
}
}
return false;
}

function addTypeToUnion(typeSet: TypeSet, type: Type) {
const flags = type.flags;
if (flags & TypeFlags.Union) {
Expand All @@ -7302,7 +7318,11 @@ namespace ts {
if (flags & TypeFlags.Null) typeSet.containsNull = true;
if (!(flags & TypeFlags.ContainsWideningType)) typeSet.containsNonWideningType = true;
}
else if (!(flags & TypeFlags.Never)) {
else if (!(flags & TypeFlags.Never || flags & TypeFlags.Intersection && isEmptyIntersectionType(<IntersectionType>type))) {
// We ignore 'never' types in unions. Likewise, we ignore intersections of unit types as they are
// another form of 'never' (in that they have an empty value domain). We could in theory turn
// intersections of unit types into 'never' upon construction, but deferring the reduction makes it
// easier to reason about their origin.
if (flags & TypeFlags.String) typeSet.containsString = true;
if (flags & TypeFlags.Number) typeSet.containsNumber = true;
if (flags & TypeFlags.StringOrNumberLiteral) typeSet.containsStringOrNumberLiteral = true;
Expand Down Expand Up @@ -10037,7 +10057,7 @@ namespace ts {
}

function isUnitType(type: Type): boolean {
return (type.flags & (TypeFlags.Literal | TypeFlags.Undefined | TypeFlags.Null)) !== 0;
return !!(type.flags & TypeFlags.Unit);
}

function isLiteralType(type: Type): boolean {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3217,6 +3217,7 @@ namespace ts {
/* @internal */
Nullable = Undefined | Null,
Literal = StringLiteral | NumberLiteral | BooleanLiteral,
Unit = Literal | Nullable,
StringOrNumberLiteral = StringLiteral | NumberLiteral,
/* @internal */
DefinitelyFalsy = StringLiteral | NumberLiteral | BooleanLiteral | Void | Undefined | Null,
Expand Down
47 changes: 47 additions & 0 deletions tests/baselines/reference/intersectionOfUnionOfUnitTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//// [intersectionOfUnionOfUnitTypes.ts]
// @strict

const enum E { A, B, C, D, E, F }

let x0: ('a' | 'b' | 'c') & ('a' | 'b' | 'c'); // 'a' | 'b' | 'c'
let x1: ('a' | 'b' | 'c') & ('b' | 'c' | 'd'); // 'b' | 'c'
let x2: ('a' | 'b' | 'c') & ('c' | 'd' | 'e'); // 'c'
let x3: ('a' | 'b' | 'c') & ('d' | 'e' | 'f'); // never
let x4: ('a' | 'b' | 'c') & ('b' | 'c' | 'd') & ('c' | 'd' | 'e'); // 'c'
let x5: ('a' | 'b' | 'c') & ('b' | 'c' | 'd') & ('c' | 'd' | 'e') & ('d' | 'e' | 'f'); // never

let y0: (0 | 1 | 2) & (0 | 1 | 2); // 0 | 1 | 2
let y1: (0 | 1 | 2) & (1 | 2 | 3); // 1 | 2
let y2: (0 | 1 | 2) & (2 | 3 | 4); // 2
let y3: (0 | 1 | 2) & (3 | 4 | 5); // never
let y4: (0 | 1 | 2) & (1 | 2 | 3) & (2 | 3 | 4); // 2
let y5: (0 | 1 | 2) & (1 | 2 | 3) & (2 | 3 | 4) & (3 | 4 | 5); // never

let z0: (E.A | E.B | E.C) & (E.A | E.B | E.C); // E.A | E.B | E.C
let z1: (E.A | E.B | E.C) & (E.B | E.C | E.D); // E.B | E.C
let z2: (E.A | E.B | E.C) & (E.C | E.D | E.E); // E.C
let z3: (E.A | E.B | E.C) & (E.D | E.E | E.F); // never
let z4: (E.A | E.B | E.C) & (E.B | E.C | E.D) & (E.C | E.D | E.E); // E.C
let z5: (E.A | E.B | E.C) & (E.B | E.C | E.D) & (E.C | E.D | E.E) & (E.D | E.E | E.F); // never


//// [intersectionOfUnionOfUnitTypes.js]
// @strict
var x0; // 'a' | 'b' | 'c'
var x1; // 'b' | 'c'
var x2; // 'c'
var x3; // never
var x4; // 'c'
var x5; // never
var y0; // 0 | 1 | 2
var y1; // 1 | 2
var y2; // 2
var y3; // never
var y4; // 2
var y5; // never
var z0; // E.A | E.B | E.C
var z1; // E.B | E.C
var z2; // E.C
var z3; // never
var z4; // E.C
var z5; // never
156 changes: 156 additions & 0 deletions tests/baselines/reference/intersectionOfUnionOfUnitTypes.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
=== tests/cases/conformance/types/intersection/intersectionOfUnionOfUnitTypes.ts ===
// @strict

const enum E { A, B, C, D, E, F }
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14))
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26))
>F : Symbol(E.F, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 29))

let x0: ('a' | 'b' | 'c') & ('a' | 'b' | 'c'); // 'a' | 'b' | 'c'
>x0 : Symbol(x0, Decl(intersectionOfUnionOfUnitTypes.ts, 4, 3))

let x1: ('a' | 'b' | 'c') & ('b' | 'c' | 'd'); // 'b' | 'c'
>x1 : Symbol(x1, Decl(intersectionOfUnionOfUnitTypes.ts, 5, 3))

let x2: ('a' | 'b' | 'c') & ('c' | 'd' | 'e'); // 'c'
>x2 : Symbol(x2, Decl(intersectionOfUnionOfUnitTypes.ts, 6, 3))

let x3: ('a' | 'b' | 'c') & ('d' | 'e' | 'f'); // never
>x3 : Symbol(x3, Decl(intersectionOfUnionOfUnitTypes.ts, 7, 3))

let x4: ('a' | 'b' | 'c') & ('b' | 'c' | 'd') & ('c' | 'd' | 'e'); // 'c'
>x4 : Symbol(x4, Decl(intersectionOfUnionOfUnitTypes.ts, 8, 3))

let x5: ('a' | 'b' | 'c') & ('b' | 'c' | 'd') & ('c' | 'd' | 'e') & ('d' | 'e' | 'f'); // never
>x5 : Symbol(x5, Decl(intersectionOfUnionOfUnitTypes.ts, 9, 3))

let y0: (0 | 1 | 2) & (0 | 1 | 2); // 0 | 1 | 2
>y0 : Symbol(y0, Decl(intersectionOfUnionOfUnitTypes.ts, 11, 3))

let y1: (0 | 1 | 2) & (1 | 2 | 3); // 1 | 2
>y1 : Symbol(y1, Decl(intersectionOfUnionOfUnitTypes.ts, 12, 3))

let y2: (0 | 1 | 2) & (2 | 3 | 4); // 2
>y2 : Symbol(y2, Decl(intersectionOfUnionOfUnitTypes.ts, 13, 3))

let y3: (0 | 1 | 2) & (3 | 4 | 5); // never
>y3 : Symbol(y3, Decl(intersectionOfUnionOfUnitTypes.ts, 14, 3))

let y4: (0 | 1 | 2) & (1 | 2 | 3) & (2 | 3 | 4); // 2
>y4 : Symbol(y4, Decl(intersectionOfUnionOfUnitTypes.ts, 15, 3))

let y5: (0 | 1 | 2) & (1 | 2 | 3) & (2 | 3 | 4) & (3 | 4 | 5); // never
>y5 : Symbol(y5, Decl(intersectionOfUnionOfUnitTypes.ts, 16, 3))

let z0: (E.A | E.B | E.C) & (E.A | E.B | E.C); // E.A | E.B | E.C
>z0 : Symbol(z0, Decl(intersectionOfUnionOfUnitTypes.ts, 18, 3))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))

let z1: (E.A | E.B | E.C) & (E.B | E.C | E.D); // E.B | E.C
>z1 : Symbol(z1, Decl(intersectionOfUnionOfUnitTypes.ts, 19, 3))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))

let z2: (E.A | E.B | E.C) & (E.C | E.D | E.E); // E.C
>z2 : Symbol(z2, Decl(intersectionOfUnionOfUnitTypes.ts, 20, 3))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26))

let z3: (E.A | E.B | E.C) & (E.D | E.E | E.F); // never
>z3 : Symbol(z3, Decl(intersectionOfUnionOfUnitTypes.ts, 21, 3))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>F : Symbol(E.F, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 29))

let z4: (E.A | E.B | E.C) & (E.B | E.C | E.D) & (E.C | E.D | E.E); // E.C
>z4 : Symbol(z4, Decl(intersectionOfUnionOfUnitTypes.ts, 22, 3))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26))

let z5: (E.A | E.B | E.C) & (E.B | E.C | E.D) & (E.C | E.D | E.E) & (E.D | E.E | E.F); // never
>z5 : Symbol(z5, Decl(intersectionOfUnionOfUnitTypes.ts, 23, 3))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>A : Symbol(E.A, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 14))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>B : Symbol(E.B, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 17))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>C : Symbol(E.C, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 20))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>D : Symbol(E.D, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 23))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>E : Symbol(E.E, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 26))
>E : Symbol(E, Decl(intersectionOfUnionOfUnitTypes.ts, 0, 0))
>F : Symbol(E.F, Decl(intersectionOfUnionOfUnitTypes.ts, 2, 29))

Loading