Skip to content

Change assignability to account for type parameters extending unions #2778

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
Apr 15, 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
38 changes: 24 additions & 14 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4016,6 +4016,7 @@ module ts {
if (source === numberType && target.flags & TypeFlags.Enum) return Ternary.True;
}
}
let saveErrorInfo = errorInfo;
if (source.flags & TypeFlags.Union || target.flags & TypeFlags.Union) {
if (relation === identityRelation) {
if (source.flags & TypeFlags.Union && target.flags & TypeFlags.Union) {
Expand Down Expand Up @@ -4054,25 +4055,34 @@ module ts {
return result;
}
}
else {
let saveErrorInfo = errorInfo;
if (source.flags & TypeFlags.Reference && target.flags & TypeFlags.Reference && (<TypeReference>source).target === (<TypeReference>target).target) {
// We have type references to same target type, see if relationship holds for all type arguments
if (result = typesRelatedTo((<TypeReference>source).typeArguments, (<TypeReference>target).typeArguments, reportErrors)) {
return result;
}
else if (source.flags & TypeFlags.Reference && target.flags & TypeFlags.Reference && (<TypeReference>source).target === (<TypeReference>target).target) {
// We have type references to same target type, see if relationship holds for all type arguments
if (result = typesRelatedTo((<TypeReference>source).typeArguments, (<TypeReference>target).typeArguments, reportErrors)) {
return result;
}
// Even if relationship doesn't hold for type arguments, it may hold in a structural comparison
// Report structural errors only if we haven't reported any errors yet
let reportStructuralErrors = reportErrors && errorInfo === saveErrorInfo;
// identity relation does not use apparent type
let sourceOrApparentType = relation === identityRelation ? source : getApparentType(source);
if (sourceOrApparentType.flags & TypeFlags.ObjectType && target.flags & TypeFlags.ObjectType &&
(result = objectTypeRelatedTo(sourceOrApparentType, <ObjectType>target, reportStructuralErrors))) {
}

// Even if relationship doesn't hold for unions, type parameters, or generic type references,
// it may hold in a structural comparison.
// Report structural errors only if we haven't reported any errors yet
let reportStructuralErrors = reportErrors && errorInfo === saveErrorInfo;
// identity relation does not use apparent type
let sourceOrApparentType = relation === identityRelation ? source : getApparentType(source);
if (sourceOrApparentType.flags & TypeFlags.ObjectType && target.flags & TypeFlags.ObjectType) {
if (result = objectTypeRelatedTo(sourceOrApparentType, <ObjectType>target, reportStructuralErrors)) {
errorInfo = saveErrorInfo;
return result;
}
}
else if (source.flags & TypeFlags.TypeParameter && sourceOrApparentType.flags & TypeFlags.Union) {
// We clear the errors first because the following check often gives a better error than
// the union comparison above if it is applicable.
errorInfo = saveErrorInfo;
if (result = isRelatedTo(sourceOrApparentType, target, reportErrors)) {
return result;
}
}

if (reportErrors) {
headMessage = headMessage || Diagnostics.Type_0_is_not_assignable_to_type_1;
let sourceType = typeToString(source);
Expand Down
28 changes: 28 additions & 0 deletions tests/baselines/reference/typeParameterDiamond1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [typeParameterDiamond1.ts]
function diamondTop<Top>() {
function diamondMiddle<T extends Top, U extends Top>() {
function diamondBottom<Bottom extends T | U>() {
var top: Top;
var middle: T | U;
var bottom: Bottom;

top = middle;
middle = bottom;
top = bottom;
}
}
}

//// [typeParameterDiamond1.js]
function diamondTop() {
function diamondMiddle() {
function diamondBottom() {
var top;
var middle;
var bottom;
top = middle;
middle = bottom;
top = bottom;
}
}
}
48 changes: 48 additions & 0 deletions tests/baselines/reference/typeParameterDiamond1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
=== tests/cases/compiler/typeParameterDiamond1.ts ===
function diamondTop<Top>() {
>diamondTop : <Top>() => void, Symbol(diamondTop, Decl(typeParameterDiamond1.ts, 0, 0))
>Top : Top, Symbol(Top, Decl(typeParameterDiamond1.ts, 0, 20))

function diamondMiddle<T extends Top, U extends Top>() {
>diamondMiddle : <T extends Top, U extends Top>() => void, Symbol(diamondMiddle, Decl(typeParameterDiamond1.ts, 0, 28))
>T : T, Symbol(T, Decl(typeParameterDiamond1.ts, 1, 27))
>Top : Top, Symbol(Top, Decl(typeParameterDiamond1.ts, 0, 20))
>U : U, Symbol(U, Decl(typeParameterDiamond1.ts, 1, 41))
>Top : Top, Symbol(Top, Decl(typeParameterDiamond1.ts, 0, 20))

function diamondBottom<Bottom extends T | U>() {
>diamondBottom : <Bottom extends T | U>() => void, Symbol(diamondBottom, Decl(typeParameterDiamond1.ts, 1, 60))
>Bottom : Bottom, Symbol(Bottom, Decl(typeParameterDiamond1.ts, 2, 31))
>T : T, Symbol(T, Decl(typeParameterDiamond1.ts, 1, 27))
>U : U, Symbol(U, Decl(typeParameterDiamond1.ts, 1, 41))

var top: Top;
>top : Top, Symbol(top, Decl(typeParameterDiamond1.ts, 3, 15))
>Top : Top, Symbol(Top, Decl(typeParameterDiamond1.ts, 0, 20))

var middle: T | U;
>middle : T | U, Symbol(middle, Decl(typeParameterDiamond1.ts, 4, 15))
>T : T, Symbol(T, Decl(typeParameterDiamond1.ts, 1, 27))
>U : U, Symbol(U, Decl(typeParameterDiamond1.ts, 1, 41))

var bottom: Bottom;
>bottom : Bottom, Symbol(bottom, Decl(typeParameterDiamond1.ts, 5, 15))
>Bottom : Bottom, Symbol(Bottom, Decl(typeParameterDiamond1.ts, 2, 31))

top = middle;
>top = middle : T | U
>top : Top, Symbol(top, Decl(typeParameterDiamond1.ts, 3, 15))
>middle : T | U, Symbol(middle, Decl(typeParameterDiamond1.ts, 4, 15))

middle = bottom;
>middle = bottom : Bottom
>middle : T | U, Symbol(middle, Decl(typeParameterDiamond1.ts, 4, 15))
>bottom : Bottom, Symbol(bottom, Decl(typeParameterDiamond1.ts, 5, 15))

top = bottom;
>top = bottom : Bottom
>top : Top, Symbol(top, Decl(typeParameterDiamond1.ts, 3, 15))
>bottom : Bottom, Symbol(bottom, Decl(typeParameterDiamond1.ts, 5, 15))
}
}
}
28 changes: 28 additions & 0 deletions tests/baselines/reference/typeParameterDiamond2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
tests/cases/compiler/typeParameterDiamond2.ts(8,13): error TS2322: Type 'T | U' is not assignable to type 'Top'.
Type 'U' is not assignable to type 'Top'.
tests/cases/compiler/typeParameterDiamond2.ts(10,13): error TS2322: Type 'Bottom' is not assignable to type 'Top'.
Type 'T | U' is not assignable to type 'Top'.
Type 'U' is not assignable to type 'Top'.


==== tests/cases/compiler/typeParameterDiamond2.ts (2 errors) ====
function diamondTop<Top>() {
function diamondMiddle<T extends Top, U>() {
function diamondBottom<Bottom extends T | U>() {
var top: Top;
var middle: T | U;
var bottom: Bottom;

top = middle;
~~~
!!! error TS2322: Type 'T | U' is not assignable to type 'Top'.
!!! error TS2322: Type 'U' is not assignable to type 'Top'.
middle = bottom;
top = bottom;
~~~
!!! error TS2322: Type 'Bottom' is not assignable to type 'Top'.
!!! error TS2322: Type 'T | U' is not assignable to type 'Top'.
!!! error TS2322: Type 'U' is not assignable to type 'Top'.
}
}
}
28 changes: 28 additions & 0 deletions tests/baselines/reference/typeParameterDiamond2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [typeParameterDiamond2.ts]
function diamondTop<Top>() {
function diamondMiddle<T extends Top, U>() {
function diamondBottom<Bottom extends T | U>() {
var top: Top;
var middle: T | U;
var bottom: Bottom;

top = middle;
middle = bottom;
top = bottom;
}
}
}

//// [typeParameterDiamond2.js]
function diamondTop() {
function diamondMiddle() {
function diamondBottom() {
var top;
var middle;
var bottom;
top = middle;
middle = bottom;
top = bottom;
}
}
}
37 changes: 37 additions & 0 deletions tests/baselines/reference/typeParameterDiamond3.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
tests/cases/compiler/typeParameterDiamond3.ts(8,13): error TS2322: Type 'T | U' is not assignable to type 'Top'.
Type 'T' is not assignable to type 'Top'.
tests/cases/compiler/typeParameterDiamond3.ts(9,13): error TS2322: Type 'Bottom' is not assignable to type 'T | U'.
Type 'Top | T | U' is not assignable to type 'T | U'.
Type 'Top' is not assignable to type 'T | U'.
Type 'Top' is not assignable to type 'U'.
tests/cases/compiler/typeParameterDiamond3.ts(10,13): error TS2322: Type 'Bottom' is not assignable to type 'Top'.
Type 'Top | T | U' is not assignable to type 'Top'.
Type 'T' is not assignable to type 'Top'.


==== tests/cases/compiler/typeParameterDiamond3.ts (3 errors) ====
function diamondTop<Top>() {
function diamondMiddle<T, U>() {
function diamondBottom<Bottom extends Top | T | U>() {
var top: Top;
var middle: T | U;
var bottom: Bottom;

top = middle;
~~~
!!! error TS2322: Type 'T | U' is not assignable to type 'Top'.
!!! error TS2322: Type 'T' is not assignable to type 'Top'.
middle = bottom;
~~~~~~
!!! error TS2322: Type 'Bottom' is not assignable to type 'T | U'.
!!! error TS2322: Type 'Top | T | U' is not assignable to type 'T | U'.
!!! error TS2322: Type 'Top' is not assignable to type 'T | U'.
!!! error TS2322: Type 'Top' is not assignable to type 'U'.
top = bottom;
~~~
!!! error TS2322: Type 'Bottom' is not assignable to type 'Top'.
!!! error TS2322: Type 'Top | T | U' is not assignable to type 'Top'.
!!! error TS2322: Type 'T' is not assignable to type 'Top'.
}
}
}
28 changes: 28 additions & 0 deletions tests/baselines/reference/typeParameterDiamond3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [typeParameterDiamond3.ts]
function diamondTop<Top>() {
function diamondMiddle<T, U>() {
function diamondBottom<Bottom extends Top | T | U>() {
var top: Top;
var middle: T | U;
var bottom: Bottom;

top = middle;
middle = bottom;
top = bottom;
}
}
}

//// [typeParameterDiamond3.js]
function diamondTop() {
function diamondMiddle() {
function diamondBottom() {
var top;
var middle;
var bottom;
top = middle;
middle = bottom;
top = bottom;
}
}
}
28 changes: 28 additions & 0 deletions tests/baselines/reference/typeParameterDiamond4.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
tests/cases/compiler/typeParameterDiamond4.ts(8,13): error TS2322: Type 'Top | T | U' is not assignable to type 'Top'.
Type 'T' is not assignable to type 'Top'.
tests/cases/compiler/typeParameterDiamond4.ts(10,13): error TS2322: Type 'Bottom' is not assignable to type 'Top'.
Type 'Top | T | U' is not assignable to type 'Top'.
Type 'T' is not assignable to type 'Top'.


==== tests/cases/compiler/typeParameterDiamond4.ts (2 errors) ====
function diamondTop<Top>() {
function diamondMiddle<T, U>() {
function diamondBottom<Bottom extends Top | T | U>() {
var top: Top;
var middle: Top | T | U;
var bottom: Bottom;

top = middle;
~~~
!!! error TS2322: Type 'Top | T | U' is not assignable to type 'Top'.
!!! error TS2322: Type 'T' is not assignable to type 'Top'.
middle = bottom;
top = bottom;
~~~
!!! error TS2322: Type 'Bottom' is not assignable to type 'Top'.
!!! error TS2322: Type 'Top | T | U' is not assignable to type 'Top'.
!!! error TS2322: Type 'T' is not assignable to type 'Top'.
}
}
}
28 changes: 28 additions & 0 deletions tests/baselines/reference/typeParameterDiamond4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [typeParameterDiamond4.ts]
function diamondTop<Top>() {
function diamondMiddle<T, U>() {
function diamondBottom<Bottom extends Top | T | U>() {
var top: Top;
var middle: Top | T | U;
var bottom: Bottom;

top = middle;
middle = bottom;
top = bottom;
}
}
}

//// [typeParameterDiamond4.js]
function diamondTop() {
function diamondMiddle() {
function diamondBottom() {
var top;
var middle;
var bottom;
top = middle;
middle = bottom;
top = bottom;
}
}
}
48 changes: 48 additions & 0 deletions tests/baselines/reference/typeParameterExtendingUnion1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//// [typeParameterExtendingUnion1.ts]
class Animal { run() { } }
class Cat extends Animal { meow }
class Dog extends Animal { woof }

function run(a: Animal) {
a.run();
}

function f<T extends Cat | Dog>(a: T) {
a.run();
run(a);
}

//// [typeParameterExtendingUnion1.js]
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var Animal = (function () {
function Animal() {
}
Animal.prototype.run = function () { };
return Animal;
})();
var Cat = (function (_super) {
__extends(Cat, _super);
function Cat() {
_super.apply(this, arguments);
}
return Cat;
})(Animal);
var Dog = (function (_super) {
__extends(Dog, _super);
function Dog() {
_super.apply(this, arguments);
}
return Dog;
})(Animal);
function run(a) {
a.run();
}
function f(a) {
a.run();
run(a);
}
Loading