Skip to content

Commit de96b41

Browse files
Merge pull request #31521 from microsoft/relatedSpanSubsequentDeclarations
Related error spans on disagreeing declarations
2 parents c71423e + 81d3595 commit de96b41

File tree

70 files changed

+174
-16
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+174
-16
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5352,7 +5352,7 @@ namespace ts {
53525352
return type;
53535353
}
53545354
else if (declaredType !== errorType && type !== errorType && !isTypeIdenticalTo(declaredType, type)) {
5355-
errorNextVariableOrPropertyDeclarationMustHaveSameType(declaredType, declaration, type);
5355+
errorNextVariableOrPropertyDeclarationMustHaveSameType(/*firstDeclaration*/ undefined, declaredType, declaration, type);
53565356
}
53575357
}
53585358
return declaredType;
@@ -26839,7 +26839,7 @@ namespace ts {
2683926839
if (type !== errorType && declarationType !== errorType &&
2684026840
!isTypeIdenticalTo(type, declarationType) &&
2684126841
!(symbol.flags & SymbolFlags.Assignment)) {
26842-
errorNextVariableOrPropertyDeclarationMustHaveSameType(type, node, declarationType);
26842+
errorNextVariableOrPropertyDeclarationMustHaveSameType(symbol.valueDeclaration, type, node, declarationType);
2684326843
}
2684426844
if (node.initializer) {
2684526845
checkTypeAssignableToAndOptionallyElaborate(checkExpressionCached(node.initializer), declarationType, node, node.initializer, /*headMessage*/ undefined);
@@ -26859,17 +26859,24 @@ namespace ts {
2685926859
}
2686026860
}
2686126861

26862-
function errorNextVariableOrPropertyDeclarationMustHaveSameType(firstType: Type, nextDeclaration: Declaration, nextType: Type): void {
26862+
function errorNextVariableOrPropertyDeclarationMustHaveSameType(firstDeclaration: Declaration | undefined, firstType: Type, nextDeclaration: Declaration, nextType: Type): void {
2686326863
const nextDeclarationName = getNameOfDeclaration(nextDeclaration);
2686426864
const message = nextDeclaration.kind === SyntaxKind.PropertyDeclaration || nextDeclaration.kind === SyntaxKind.PropertySignature
2686526865
? Diagnostics.Subsequent_property_declarations_must_have_the_same_type_Property_0_must_be_of_type_1_but_here_has_type_2
2686626866
: Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2;
26867-
error(
26867+
const declName = declarationNameToString(nextDeclarationName);
26868+
const err = error(
2686826869
nextDeclarationName,
2686926870
message,
26870-
declarationNameToString(nextDeclarationName),
26871+
declName,
2687126872
typeToString(firstType),
26872-
typeToString(nextType));
26873+
typeToString(nextType)
26874+
);
26875+
if (firstDeclaration) {
26876+
addRelatedInfo(err,
26877+
createDiagnosticForNode(firstDeclaration, Diagnostics._0_was_also_declared_here, declName)
26878+
);
26879+
}
2687326880
}
2687426881

2687526882
function areDeclarationFlagsIdentical(left: Declaration, right: Declaration) {

tests/baselines/reference/ES5For-of7.errors.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ tests/cases/conformance/statements/for-ofStatements/ES5For-of7.ts(6,9): error TS
1010
var x = [w, v];
1111
~
1212
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'any', but here has type 'any[]'.
13+
!!! related TS6203 tests/cases/conformance/statements/for-ofStatements/ES5For-of7.ts:2:9: 'x' was also declared here.
1314
}

tests/baselines/reference/FunctionAndModuleWithSameNameAndCommonRoot.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ tests/cases/conformance/internalModules/DeclarationMerging/test.ts(2,5): error T
2424
var fn = A.Point;
2525
~~
2626
!!! 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'.
27+
!!! related TS6203 tests/cases/conformance/internalModules/DeclarationMerging/test.ts:1:5: 'fn' was also declared here.
2728

2829
var cl: { x: number; y: number; }
2930
var cl = A.Point();
@@ -46,6 +47,7 @@ tests/cases/conformance/internalModules/DeclarationMerging/test.ts(2,5): error T
4647
var fn = B.Point; // not expected to be an error. bug 840000: [corelang] Function of fundule not assignalbe as expected
4748
~~
4849
!!! 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'.
50+
!!! related TS6203 tests/cases/conformance/internalModules/DeclarationMerging/test.ts:1:5: 'fn' was also declared here.
4951

5052
var cl: { x: number; y: number; }
5153
var cl = B.Point();

tests/baselines/reference/asyncArrowFunction5_es2017.errors.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es20
1616
!!! error TS1005: ',' expected.
1717
~~~~~~~
1818
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
19+
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:152:13: 'Promise' was also declared here.
1920
~
2021
!!! error TS1005: ',' expected.
2122
~~

tests/baselines/reference/asyncArrowFunction5_es5.errors.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(
1616
!!! error TS1005: ',' expected.
1717
~~~~~~~
1818
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
19+
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:152:13: 'Promise' was also declared here.
1920
~
2021
!!! error TS1005: ',' expected.
2122
~~

tests/baselines/reference/asyncArrowFunction5_es6.errors.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(
1616
!!! error TS1005: ',' expected.
1717
~~~~~~~
1818
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
19+
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:152:13: 'Promise' was also declared here.
1920
~
2021
!!! error TS1005: ',' expected.
2122
~~

tests/baselines/reference/asyncArrowFunction9_es2017.errors.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es20
1616
!!! error TS1005: ',' expected.
1717
~~~~~~~
1818
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
19+
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:152:13: 'Promise' was also declared here.
1920
~
2021
!!! error TS1005: ',' expected.
2122
~~

tests/baselines/reference/asyncArrowFunction9_es5.errors.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(
1616
!!! error TS1005: ',' expected.
1717
~~~~~~~
1818
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
19+
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:152:13: 'Promise' was also declared here.
1920
~
2021
!!! error TS1005: ',' expected.
2122
~~

tests/baselines/reference/asyncArrowFunction9_es6.errors.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction9_es6.ts(
1616
!!! error TS1005: ',' expected.
1717
~~~~~~~
1818
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'any'.
19+
!!! related TS6203 /.ts/lib.es2015.promise.d.ts:152:13: 'Promise' was also declared here.
1920
~
2021
!!! error TS1005: ',' expected.
2122
~~

tests/baselines/reference/augmentedTypesVar.errors.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ tests/cases/compiler/augmentedTypesVar.ts(31,8): error TS2300: Duplicate identif
3030
var x3 = () => { } // error
3131
~~
3232
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x3' must be of type 'number', but here has type '() => void'.
33+
!!! related TS6203 tests/cases/compiler/augmentedTypesVar.ts:9:5: 'x3' was also declared here.
3334

3435
// var then class
3536
var x4 = 1; // error

0 commit comments

Comments
 (0)