Skip to content

Commit

Permalink
Subsequent variable declarations must have same type: Mention locatio…
Browse files Browse the repository at this point in the history
…n of other declaration (#19356)

* Subsequent variable declarations must have same type: Mention location of other declaration

* Fix naming, remove template literal
  • Loading branch information
Andy authored Oct 20, 2017
1 parent 1ea1254 commit d7be61a
Show file tree
Hide file tree
Showing 57 changed files with 281 additions and 267 deletions.
26 changes: 20 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,10 @@ namespace ts {
return emitResolver;
}

function error(location: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): void {
function error(location: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): void {
const diagnostic = location
? createDiagnosticForNode(location, message, arg0, arg1, arg2)
: createCompilerDiagnostic(message, arg0, arg1, arg2);
? createDiagnosticForNode(location, message, arg0, arg1, arg2, arg3)
: createCompilerDiagnostic(message, arg0, arg1, arg2, arg3);
diagnostics.add(diagnostic);
}

Expand Down Expand Up @@ -4417,8 +4417,7 @@ namespace ts {
jsDocType = declarationType;
}
else if (jsDocType !== unknownType && declarationType !== unknownType && !isTypeIdenticalTo(jsDocType, declarationType)) {
const name = getNameOfDeclaration(declaration);
error(name, Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2, declarationNameToString(name), typeToString(jsDocType), typeToString(declarationType));
errorNextVariableDeclarationMustHaveSameType(symbol.valueDeclaration, jsDocType, declaration, declarationType);
}
}
else if (!jsDocType) {
Expand Down Expand Up @@ -20779,7 +20778,7 @@ namespace ts {
// initializer is consistent with type associated with the node
const declarationType = convertAutoToAny(getWidenedTypeForVariableLikeDeclaration(node));
if (type !== unknownType && declarationType !== unknownType && !isTypeIdenticalTo(type, declarationType)) {
error(node.name, Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2, declarationNameToString(node.name), typeToString(type), typeToString(declarationType));
errorNextVariableDeclarationMustHaveSameType(symbol.valueDeclaration, type, node, declarationType);
}
if (node.initializer) {
checkTypeAssignableTo(checkExpressionCached(node.initializer), declarationType, node, /*headMessage*/ undefined);
Expand All @@ -20803,6 +20802,21 @@ namespace ts {
}
}

function errorNextVariableDeclarationMustHaveSameType(firstDeclaration: Declaration, firstType: Type, nextDeclaration: Declaration, nextType: Type): void {
const firstSourceFile = getSourceFileOfNode(firstDeclaration);
const firstSpan = getErrorSpanForNode(firstSourceFile, getNameOfDeclaration(firstDeclaration) || firstDeclaration);
const firstLocation = getLineAndCharacterOfPosition(firstSourceFile, firstSpan.start);
const firstLocationDescription = firstSourceFile.fileName + " " + firstLocation.line + ":" + firstLocation.character;
const nextDeclarationName = getNameOfDeclaration(nextDeclaration);
error(
nextDeclarationName,
Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_has_type_1_at_2_but_here_has_type_3,
declarationNameToString(nextDeclarationName),
typeToString(firstType),
firstLocationDescription,
typeToString(nextType));
}

function areDeclarationFlagsIdentical(left: Declaration, right: Declaration) {
if ((left.kind === SyntaxKind.Parameter && right.kind === SyntaxKind.VariableDeclaration) ||
(left.kind === SyntaxKind.VariableDeclaration && right.kind === SyntaxKind.Parameter)) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@
"category": "Error",
"code": 2402
},
"Subsequent variable declarations must have the same type. Variable '{0}' must be of type '{1}', but here has type '{2}'.": {
"Subsequent variable declarations must have the same type. Variable '{0}' has type '{1}' at {2}, but here has type '{3}'.": {
"category": "Error",
"code": 2403
},
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,14 +582,14 @@ namespace ts {
}
}

export function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): Diagnostic {
export function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): Diagnostic {
const sourceFile = getSourceFileOfNode(node);
return createDiagnosticForNodeInSourceFile(sourceFile, node, message, arg0, arg1, arg2);
return createDiagnosticForNodeInSourceFile(sourceFile, node, message, arg0, arg1, arg2, arg3);
}

export function createDiagnosticForNodeInSourceFile(sourceFile: SourceFile, node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): Diagnostic {
export function createDiagnosticForNodeInSourceFile(sourceFile: SourceFile, node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): Diagnostic {
const span = getErrorSpanForNode(sourceFile, node);
return createFileDiagnostic(sourceFile, span.start, span.length, message, arg0, arg1, arg2);
return createFileDiagnostic(sourceFile, span.start, span.length, message, arg0, arg1, arg2, arg3);
}

export function createDiagnosticForNodeFromMessageChain(node: Node, messageChain: DiagnosticMessageChain): Diagnostic {
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5For-of7.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/conformance/statements/for-ofStatements/ES5For-of7.ts(6,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'any', but here has type 'any[]'.
tests/cases/conformance/statements/for-ofStatements/ES5For-of7.ts(6,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'x' has type 'any' at tests/cases/conformance/statements/for-ofStatements/ES5For-of7.ts 1:8, but here has type 'any[]'.


==== tests/cases/conformance/statements/for-ofStatements/ES5For-of7.ts (1 errors) ====
Expand All @@ -9,5 +9,5 @@ tests/cases/conformance/statements/for-ofStatements/ES5For-of7.ts(6,9): error TS
for (var v of []) {
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[]'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' has type 'any' at tests/cases/conformance/statements/for-ofStatements/ES5For-of7.ts 1:8, but here has type 'any[]'.
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/conformance/internalModules/DeclarationMerging/module.ts(2,19): error TS2433: A namespace declaration cannot be in a different file from a class or function with which it is merged.
tests/cases/conformance/internalModules/DeclarationMerging/simple.ts(13,5): 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'.
tests/cases/conformance/internalModules/DeclarationMerging/test.ts(2,5): 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'.
tests/cases/conformance/internalModules/DeclarationMerging/simple.ts(13,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'fn' has type '() => { x: number; y: number; }' at tests/cases/conformance/internalModules/DeclarationMerging/test.ts 0:4, but here has type 'typeof Point'.
tests/cases/conformance/internalModules/DeclarationMerging/test.ts(2,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'fn' has type '() => { x: number; y: number; }' at tests/cases/conformance/internalModules/DeclarationMerging/test.ts 0:4, but here has type 'typeof Point'.


==== tests/cases/conformance/internalModules/DeclarationMerging/function.ts (0 errors) ====
Expand All @@ -23,7 +23,7 @@ tests/cases/conformance/internalModules/DeclarationMerging/test.ts(2,5): error T
var fn: () => { x: number; y: number };
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'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'fn' has type '() => { x: number; y: number; }' at tests/cases/conformance/internalModules/DeclarationMerging/test.ts 0:4, but here has type 'typeof Point'.

var cl: { x: number; y: number; }
var cl = A.Point();
Expand All @@ -45,7 +45,7 @@ tests/cases/conformance/internalModules/DeclarationMerging/test.ts(2,5): error T
var fn: () => { x: number; y: number };
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'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'fn' has type '() => { x: number; y: number; }' at tests/cases/conformance/internalModules/DeclarationMerging/test.ts 0:4, but here has type 'typeof Point'.

var cl: { x: number; y: number; }
var cl = B.Point();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,11): error TS2304: Cannot find name 'async'.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,18): error TS2304: Cannot find name 'await'.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,24): error TS1005: ',' expected.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,33): error TS1005: '=' expected.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,40): error TS1109: Expression expected.

Expand All @@ -15,7 +15,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 'void'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
~
!!! error TS1005: '=' expected.
~~
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/asyncArrowFunction5_es5.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,11): error TS2304: Cannot find name 'async'.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,18): error TS2304: Cannot find name 'await'.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,24): error TS1005: ',' expected.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,33): error TS1005: '=' expected.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,40): error TS1109: Expression expected.

Expand All @@ -15,7 +15,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 'void'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
~
!!! error TS1005: '=' expected.
~~
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/asyncArrowFunction5_es6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(1,11): error TS2304: Cannot find name 'async'.
tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(1,18): error TS2304: Cannot find name 'await'.
tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(1,24): error TS1005: ',' expected.
tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(1,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'.
tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(1,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(1,33): error TS1005: '=' expected.
tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(1,40): error TS1109: Expression expected.

Expand All @@ -15,7 +15,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 'void'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
~
!!! error TS1005: '=' expected.
~~
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,11): error TS2304: Cannot find name 'async'.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,18): error TS2304: Cannot find name 'a'.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,37): error TS1005: ',' expected.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,39): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,39): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,46): error TS1005: '=' expected.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,53): error TS1109: Expression expected.

Expand All @@ -15,7 +15,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 'void'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
~
!!! error TS1005: '=' expected.
~~
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/asyncArrowFunction9_es5.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(1,11): error TS2304: Cannot find name 'async'.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(1,18): error TS2304: Cannot find name 'a'.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(1,37): error TS1005: ',' expected.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(1,39): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(1,39): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(1,46): error TS1005: '=' expected.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(1,53): error TS1109: Expression expected.

Expand All @@ -15,7 +15,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 'void'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
~
!!! error TS1005: '=' expected.
~~
Expand Down
Loading

0 comments on commit d7be61a

Please sign in to comment.