Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #31521 from microsoft/relatedSpanSubsequentDeclara…
…tions

Related error spans on disagreeing declarations
  • Loading branch information
DanielRosenwasser committed May 21, 2019
2 parents c71423e + 81d3595 commit de96b41
Show file tree
Hide file tree
Showing 70 changed files with 174 additions and 16 deletions.
19 changes: 13 additions & 6 deletions src/compiler/checker.ts
Expand Up @@ -5352,7 +5352,7 @@ namespace ts {
return type;
}
else if (declaredType !== errorType && type !== errorType && !isTypeIdenticalTo(declaredType, type)) {
errorNextVariableOrPropertyDeclarationMustHaveSameType(declaredType, declaration, type);
errorNextVariableOrPropertyDeclarationMustHaveSameType(/*firstDeclaration*/ undefined, declaredType, declaration, type);
}
}
return declaredType;
Expand Down Expand Up @@ -26839,7 +26839,7 @@ namespace ts {
if (type !== errorType && declarationType !== errorType &&
!isTypeIdenticalTo(type, declarationType) &&
!(symbol.flags & SymbolFlags.Assignment)) {
errorNextVariableOrPropertyDeclarationMustHaveSameType(type, node, declarationType);
errorNextVariableOrPropertyDeclarationMustHaveSameType(symbol.valueDeclaration, type, node, declarationType);
}
if (node.initializer) {
checkTypeAssignableToAndOptionallyElaborate(checkExpressionCached(node.initializer), declarationType, node, node.initializer, /*headMessage*/ undefined);
Expand All @@ -26859,17 +26859,24 @@ namespace ts {
}
}

function errorNextVariableOrPropertyDeclarationMustHaveSameType(firstType: Type, nextDeclaration: Declaration, nextType: Type): void {
function errorNextVariableOrPropertyDeclarationMustHaveSameType(firstDeclaration: Declaration | undefined, firstType: Type, nextDeclaration: Declaration, nextType: Type): void {
const nextDeclarationName = getNameOfDeclaration(nextDeclaration);
const message = nextDeclaration.kind === SyntaxKind.PropertyDeclaration || nextDeclaration.kind === SyntaxKind.PropertySignature
? Diagnostics.Subsequent_property_declarations_must_have_the_same_type_Property_0_must_be_of_type_1_but_here_has_type_2
: Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2;
error(
const declName = declarationNameToString(nextDeclarationName);
const err = error(
nextDeclarationName,
message,
declarationNameToString(nextDeclarationName),
declName,
typeToString(firstType),
typeToString(nextType));
typeToString(nextType)
);
if (firstDeclaration) {
addRelatedInfo(err,
createDiagnosticForNode(firstDeclaration, Diagnostics._0_was_also_declared_here, declName)
);
}
}

function areDeclarationFlagsIdentical(left: Declaration, right: Declaration) {
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/ES5For-of7.errors.txt
Expand Up @@ -10,4 +10,5 @@ tests/cases/conformance/statements/for-ofStatements/ES5For-of7.ts(6,9): error TS
var x = [w, v];
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'any', but here has type 'any[]'.
!!! related TS6203 tests/cases/conformance/statements/for-ofStatements/ES5For-of7.ts:2:9: 'x' was also declared here.
}
Expand Up @@ -24,6 +24,7 @@ tests/cases/conformance/internalModules/DeclarationMerging/test.ts(2,5): error T
var fn = A.Point;
~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'fn' must be of type '() => { x: number; y: number; }', but here has type 'typeof Point'.
!!! related TS6203 tests/cases/conformance/internalModules/DeclarationMerging/test.ts:1:5: 'fn' was also declared here.

var cl: { x: number; y: number; }
var cl = A.Point();
Expand All @@ -46,6 +47,7 @@ tests/cases/conformance/internalModules/DeclarationMerging/test.ts(2,5): error T
var fn = B.Point; // not expected to be an error. bug 840000: [corelang] Function of fundule not assignalbe as expected
~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'fn' must be of type '() => { x: number; y: number; }', but here has type 'typeof Point'.
!!! related TS6203 tests/cases/conformance/internalModules/DeclarationMerging/test.ts:1:5: 'fn' was also declared here.

var cl: { x: number; y: number; }
var cl = B.Point();
Expand Down
Expand Up @@ -16,6 +16,7 @@ tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es20
!!! error TS1005: ',' expected.
~~~~~~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:152:13: 'Promise' was also declared here.
~
!!! error TS1005: ',' expected.
~~
Expand Down
Expand Up @@ -16,6 +16,7 @@ tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(
!!! error TS1005: ',' expected.
~~~~~~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:152:13: 'Promise' was also declared here.
~
!!! error TS1005: ',' expected.
~~
Expand Down
Expand Up @@ -16,6 +16,7 @@ tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(
!!! error TS1005: ',' expected.
~~~~~~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:152:13: 'Promise' was also declared here.
~
!!! error TS1005: ',' expected.
~~
Expand Down
Expand Up @@ -16,6 +16,7 @@ tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es20
!!! error TS1005: ',' expected.
~~~~~~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:152:13: 'Promise' was also declared here.
~
!!! error TS1005: ',' expected.
~~
Expand Down
Expand Up @@ -16,6 +16,7 @@ tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(
!!! error TS1005: ',' expected.
~~~~~~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:152:13: 'Promise' was also declared here.
~
!!! error TS1005: ',' expected.
~~
Expand Down
Expand Up @@ -16,6 +16,7 @@ tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction9_es6.ts(
!!! error TS1005: ',' expected.
~~~~~~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:152:13: 'Promise' was also declared here.
~
!!! error TS1005: ',' expected.
~~
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/augmentedTypesVar.errors.txt
Expand Up @@ -30,6 +30,7 @@ tests/cases/compiler/augmentedTypesVar.ts(31,8): error TS2300: Duplicate identif
var x3 = () => { } // error
~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x3' must be of type 'number', but here has type '() => void'.
!!! related TS6203 tests/cases/compiler/augmentedTypesVar.ts:9:5: 'x3' was also declared here.

// var then class
var x4 = 1; // error
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/castingTuple.errors.txt
Expand Up @@ -71,6 +71,7 @@ tests/cases/conformance/types/tuple/castingTuple.ts(33,1): error TS2304: Cannot
var array1 = <number[]>numStrTuple;
~~~~~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'array1' must be of type '{}[]', but here has type 'number[]'.
!!! related TS6203 tests/cases/conformance/types/tuple/castingTuple.ts:23:5: 'array1' was also declared here.
t4[2] = 10;
~~
!!! error TS2304: Cannot find name 't4'.
Expand Down
Expand Up @@ -15,6 +15,7 @@ tests/cases/compiler/global.d.ts(6,16): error TS2403: Subsequent variable declar
export const THREE: typeof _three;
~~~~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'THREE' must be of type 'typeof import("tests/cases/compiler/global")', but here has type 'typeof import("tests/cases/compiler/three")'.
!!! related TS6203 tests/cases/compiler/global.d.ts:1:1: 'THREE' was also declared here.
}

==== tests/cases/compiler/test.ts (0 errors) ====
Expand Down
Expand Up @@ -14,6 +14,7 @@ tests/cases/compiler/classWithDuplicateIdentifier.ts(11,5): error TS2717: Subseq
!!! error TS2300: Duplicate identifier 'a'.
~
!!! error TS2717: Subsequent property declarations must have the same type. Property 'a' must be of type '() => number', but here has type 'number'.
!!! related TS6203 tests/cases/compiler/classWithDuplicateIdentifier.ts:2:5: 'a' was also declared here.
}
class K {
b: number; // error: duplicate identifier
Expand All @@ -30,5 +31,6 @@ tests/cases/compiler/classWithDuplicateIdentifier.ts(11,5): error TS2717: Subseq
!!! error TS2300: Duplicate identifier 'c'.
~
!!! error TS2717: Subsequent property declarations must have the same type. Property 'c' must be of type 'number', but here has type 'string'.
!!! related TS6203 tests/cases/compiler/classWithDuplicateIdentifier.ts:10:5: 'c' was also declared here.
}

1 change: 1 addition & 0 deletions tests/baselines/reference/conditionalTypes1.errors.txt
Expand Up @@ -391,6 +391,7 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(288,43): error TS
var z: T2; // Error, T2 is distributive, T1 isn't
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'z' must be of type 'T1', but here has type 'Foo<T & U>'.
!!! related TS6203 tests/cases/conformance/types/conditional/conditionalTypes1.ts:262:9: 'z' was also declared here.
}

function f33<T, U>() {
Expand Down
Expand Up @@ -97,6 +97,7 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9):
var y: string;
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'y' must be of type 'string | number', but here has type 'string'.
!!! related TS6203 tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts:56:17: 'y' was also declared here.
}

function f8() {
Expand Down
Expand Up @@ -97,6 +97,7 @@ tests/cases/compiler/duplicateClassElements.ts(41,12): error TS2300: Duplicate i
!!! error TS2300: Duplicate identifier 'x2'.
~~
!!! error TS2717: Subsequent property declarations must have the same type. Property 'x2' must be of type 'number', but here has type 'any'.
!!! related TS6203 tests/cases/compiler/duplicateClassElements.ts:29:9: 'x2' was also declared here.

get z2() {
~~
Expand Down
Expand Up @@ -38,4 +38,5 @@ tests/cases/compiler/duplicateIdentifierInCatchBlock.ts(16,9): error TS2403: Sub
var p: number; // error
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'p' must be of type 'string', but here has type 'number'.
!!! related TS6203 tests/cases/compiler/duplicateIdentifierInCatchBlock.ts:15:9: 'p' was also declared here.
}
Expand Up @@ -201,6 +201,7 @@ tests/cases/compiler/duplicateLocalVariable1.ts(186,37): error TS2356: An arithm
for (var i = 0; i < 14; i++) {
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'i' must be of type 'string', but here has type 'number'.
!!! related TS6203 tests/cases/compiler/duplicateLocalVariable1.ts:181:22: 'i' was also declared here.
~~~~~~
!!! error TS2365: Operator '<' cannot be applied to types 'string' and 'number'.
~
Expand Down
Expand Up @@ -33,6 +33,7 @@ tests/cases/compiler/duplicateLocalVariable2.ts(27,37): error TS2356: An arithme
for (var i = 0; i < 14; i++) {
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'i' must be of type 'string', but here has type 'number'.
!!! related TS6203 tests/cases/compiler/duplicateLocalVariable2.ts:22:22: 'i' was also declared here.
~~~~~~
!!! error TS2365: Operator '<' cannot be applied to types 'string' and 'number'.
~
Expand Down
Expand Up @@ -15,4 +15,5 @@ tests/cases/compiler/duplicateLocalVariable3.ts(11,9): error TS2403: Subsequent
var z = "";
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'z' must be of type 'number', but here has type 'string'.
!!! related TS6203 tests/cases/compiler/duplicateLocalVariable3.ts:10:9: 'z' was also declared here.
}
3 changes: 2 additions & 1 deletion tests/baselines/reference/duplicateLocalVariable4.errors.txt
Expand Up @@ -9,4 +9,5 @@ tests/cases/compiler/duplicateLocalVariable4.ts(6,5): error TS2403: Subsequent v
var x = E;
var x = E.a;
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'typeof E', but here has type 'E'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'typeof E', but here has type 'E'.
!!! related TS6203 tests/cases/compiler/duplicateLocalVariable4.ts:5:5: 'x' was also declared here.
Expand Up @@ -10,22 +10,26 @@ tests/cases/compiler/duplicateVariablesWithAny.ts(13,9): error TS2403: Subsequen
var x = 2; //error
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'any', but here has type 'number'.
!!! related TS6203 tests/cases/compiler/duplicateVariablesWithAny.ts:2:5: 'x' was also declared here.

var y = "";
var y; //error
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'y' must be of type 'string', but here has type 'any'.
!!! related TS6203 tests/cases/compiler/duplicateVariablesWithAny.ts:5:5: 'y' was also declared here.

module N {
var x: any;
var x = 2; //error
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'any', but here has type 'number'.
!!! related TS6203 tests/cases/compiler/duplicateVariablesWithAny.ts:9:9: 'x' was also declared here.

var y = "";
var y; //error
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'y' must be of type 'string', but here has type 'any'.
!!! related TS6203 tests/cases/compiler/duplicateVariablesWithAny.ts:12:9: 'y' was also declared here.
}

var z: any;
Expand Down
Expand Up @@ -12,18 +12,22 @@ tests/cases/compiler/duplicateVarsAcrossFileBoundaries_2.ts(3,5): error TS2403:
var x = true;
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'number', but here has type 'boolean'.
!!! related TS6203 tests/cases/compiler/duplicateVarsAcrossFileBoundaries_0.ts:1:5: 'x' was also declared here.
var z = 3;

==== tests/cases/compiler/duplicateVarsAcrossFileBoundaries_2.ts (3 errors) ====
var x = "";
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'number', but here has type 'string'.
!!! related TS6203 tests/cases/compiler/duplicateVarsAcrossFileBoundaries_0.ts:1:5: 'x' was also declared here.
var y = 3;
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'y' must be of type 'string', but here has type 'number'.
!!! related TS6203 tests/cases/compiler/duplicateVarsAcrossFileBoundaries_0.ts:2:5: 'y' was also declared here.
var z = false;
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'z' must be of type 'number', but here has type 'boolean'.
!!! related TS6203 tests/cases/compiler/duplicateVarsAcrossFileBoundaries_1.ts:2:5: 'z' was also declared here.

==== tests/cases/compiler/duplicateVarsAcrossFileBoundaries_3.ts (0 errors) ====
var x = 0;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/dynamicNamesErrors.errors.txt
Expand Up @@ -35,6 +35,7 @@ tests/cases/compiler/dynamicNamesErrors.ts(25,1): error TS2322: Type 'T1' is not
[c1]: string;
~~~~
!!! error TS2717: Subsequent property declarations must have the same type. Property '[c1]' must be of type 'number', but here has type 'string'.
!!! related TS6203 tests/cases/compiler/dynamicNamesErrors.ts:18:5: '[c1]' was also declared here.
}

let t1: T1;
Expand Down
Expand Up @@ -109,10 +109,12 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/enumAssi
var r4 = foo16(E.A);
~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'r4' must be of type 'E', but here has type 'Object'.
!!! related TS6203 tests/cases/conformance/types/typeRelationships/assignmentCompatibility/enumAssignabilityInInheritance.ts:22:5: 'r4' was also declared here.

declare function foo17(x: {}): {};
declare function foo17(x: E): E;

var r4 = foo16(E.A);
~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'r4' must be of type 'E', but here has type 'Object'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'r4' must be of type 'E', but here has type 'Object'.
!!! related TS6203 tests/cases/conformance/types/typeRelationships/assignmentCompatibility/enumAssignabilityInInheritance.ts:22:5: 'r4' was also declared here.
2 changes: 2 additions & 0 deletions tests/baselines/reference/for-inStatements.errors.txt
Expand Up @@ -39,6 +39,7 @@ tests/cases/conformance/statements/for-inStatements/for-inStatements.ts(79,15):
for (var x in this) { }
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'string', but here has type 'Extract<keyof this, string>'.
!!! related TS6203 tests/cases/conformance/statements/for-inStatements/for-inStatements.ts:31:18: 'x' was also declared here.
return null;
}

Expand All @@ -58,6 +59,7 @@ tests/cases/conformance/statements/for-inStatements/for-inStatements.ts(79,15):
for (var x in this) { }
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'string', but here has type 'Extract<keyof this, string>'.
!!! related TS6203 tests/cases/conformance/statements/for-inStatements/for-inStatements.ts:48:18: 'x' was also declared here.

for (var x in super.biz) { }
for (var x in super.biz()) { }
Expand Down

0 comments on commit de96b41

Please sign in to comment.