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

Better "always true/false" errors #25251

Merged
merged 6 commits into from
Jun 28, 2018
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
26 changes: 25 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20884,7 +20884,31 @@ namespace ts {
}

function reportOperatorError() {
error(errorNode || operatorToken, Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2, tokenToString(operatorToken.kind), typeToString(leftType), typeToString(rightType));
let err = chainDiagnosticMessages(
/*elaboration*/ undefined,
Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2,
tokenToString(operatorToken.kind),
typeToString(leftType),
typeToString(rightType)
);
err = giveBetterPrimaryError(err);

diagnostics.add(createDiagnosticForNodeFromMessageChain(
errorNode || operatorToken,
err
));
}

function giveBetterPrimaryError(elaboration: DiagnosticMessageChain) {
switch (operatorToken.kind) {
case SyntaxKind.EqualsEqualsEqualsToken:
case SyntaxKind.EqualsEqualsToken:
return chainDiagnosticMessages(elaboration, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0, "false");
case SyntaxKind.ExclamationEqualsEqualsToken:
case SyntaxKind.ExclamationEqualsToken:
return chainDiagnosticMessages(elaboration, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0, "true");
}
return elaboration;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,10 @@
"category": "Error",
"code": 2366
},
"The types of these values indicate that this condition will always be '{0}'.": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If those are statically known primitive types, can we include actual values in error message? Like:

"These values are known to be '{0}' and '{1}', rendering this condition always '{2}'."

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out #25311

"category": "Error",
"code": 2367
},
"Type parameter name cannot be '{0}'.": {
"category": "Error",
"code": 2368
Expand Down
12 changes: 6 additions & 6 deletions src/testRunner/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4980,7 +4980,7 @@ namespace ts.projectSystem {
assert.isTrue(error2Result.length === 0);
});

it("should report semanitc errors for loose JS files with '// @ts-check' and skipLibCheck=true", () => {
it("should report semantic errors for loose JS files with '// @ts-check' and skipLibCheck=true", () => {
const jsFile = {
path: "/a/jsFile.js",
content: `
Expand All @@ -4999,10 +4999,10 @@ namespace ts.projectSystem {
);
const errorResult = <protocol.Diagnostic[]>session.executeCommand(getErrRequest).response;
assert.isTrue(errorResult.length === 1);
assert.equal(errorResult[0].code, Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2.code);
assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code);
});

it("should report semanitc errors for configured js project with '// @ts-check' and skipLibCheck=true", () => {
it("should report semantic errors for configured js project with '// @ts-check' and skipLibCheck=true", () => {
const jsconfigFile = {
path: "/a/jsconfig.json",
content: "{}"
Expand All @@ -5026,10 +5026,10 @@ namespace ts.projectSystem {
);
const errorResult = <protocol.Diagnostic[]>session.executeCommand(getErrRequest).response;
assert.isTrue(errorResult.length === 1);
assert.equal(errorResult[0].code, Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2.code);
assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code);
});

it("should report semanitc errors for configured js project with checkJs=true and skipLibCheck=true", () => {
it("should report semantic errors for configured js project with checkJs=true and skipLibCheck=true", () => {
const jsconfigFile = {
path: "/a/jsconfig.json",
content: JSON.stringify({
Expand All @@ -5055,7 +5055,7 @@ namespace ts.projectSystem {
);
const errorResult = <protocol.Diagnostic[]>session.executeCommand(getErrRequest).response;
assert.isTrue(errorResult.length === 1);
assert.equal(errorResult[0].code, Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2.code);
assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code);
});
});

Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5151,6 +5151,7 @@ declare namespace ts {
The_left_hand_side_of_an_assignment_expression_must_be_a_variable_or_a_property_access: DiagnosticMessage;
Operator_0_cannot_be_applied_to_types_1_and_2: DiagnosticMessage;
Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined: DiagnosticMessage;
The_types_of_these_values_indicate_that_this_condition_will_always_be_0: DiagnosticMessage;
Type_parameter_name_cannot_be_0: DiagnosticMessage;
A_parameter_property_is_only_allowed_in_a_constructor_implementation: DiagnosticMessage;
A_rest_parameter_must_be_of_an_array_type: DiagnosticMessage;
Expand Down
12 changes: 8 additions & 4 deletions tests/baselines/reference/capturedLetConstInLoop5.errors.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
tests/cases/compiler/capturedLetConstInLoop5.ts(174,13): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop5.ts(174,13): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '1'.


==== tests/cases/compiler/capturedLetConstInLoop5.ts (2 errors) ====
Expand Down Expand Up @@ -178,7 +180,8 @@ tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2365: Operator
(() => x + v);
if (x == 1) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
return;
}
}
Expand Down Expand Up @@ -235,7 +238,8 @@ tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2365: Operator
(() => x + y + v);
if (x == 1) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These values are known to be '0' and '1', rendering this condition always 'false'.
^^^^ how much smoother to comprehend and fix!

!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
return;
}
}
Expand Down
12 changes: 8 additions & 4 deletions tests/baselines/reference/capturedLetConstInLoop5_ES6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(174,13): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(174,13): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '1'.


==== tests/cases/compiler/capturedLetConstInLoop5_ES6.ts (2 errors) ====
Expand Down Expand Up @@ -178,7 +180,8 @@ tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2365: Opera
(() => x + v);
if (x == 1) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
return;
}
}
Expand Down Expand Up @@ -235,7 +238,8 @@ tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2365: Opera
(() => x + y + v);
if (x == 1) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
return;
}
}
Expand Down
24 changes: 16 additions & 8 deletions tests/baselines/reference/capturedLetConstInLoop6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
tests/cases/compiler/capturedLetConstInLoop6.ts(147,9): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop6.ts(150,9): error TS2365: Operator '==' cannot be applied to types '0' and '2'.
tests/cases/compiler/capturedLetConstInLoop6.ts(194,9): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2365: Operator '==' cannot be applied to types '0' and '2'.
tests/cases/compiler/capturedLetConstInLoop6.ts(147,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop6.ts(150,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '2'.
tests/cases/compiler/capturedLetConstInLoop6.ts(194,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '2'.


==== tests/cases/compiler/capturedLetConstInLoop6.ts (4 errors) ====
Expand Down Expand Up @@ -153,12 +157,14 @@ tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2365: Operator '
(() => x);
if (x == 1) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
break;
}
if (x == 2) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '2'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '2'.
continue;
}
}
Expand Down Expand Up @@ -204,12 +210,14 @@ tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2365: Operator '
(() => x + y);
if (x == 1) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
break;
}
if (x == 2) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '2'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '2'.
continue;
}
}
Expand Down
24 changes: 16 additions & 8 deletions tests/baselines/reference/capturedLetConstInLoop6_ES6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(147,9): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(150,9): error TS2365: Operator '==' cannot be applied to types '0' and '2'.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(194,9): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2365: Operator '==' cannot be applied to types '0' and '2'.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(147,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(150,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '2'.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(194,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '2'.


==== tests/cases/compiler/capturedLetConstInLoop6_ES6.ts (4 errors) ====
Expand Down Expand Up @@ -153,12 +157,14 @@ tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2365: Operat
(() => x);
if (x == 1) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
break;
}
if (x == 2) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '2'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '2'.
continue;
}
}
Expand Down Expand Up @@ -204,12 +210,14 @@ tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2365: Operat
(() => x + y);
if (x == 1) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
break;
}
if (x == 2) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '2'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '2'.
continue;
}
}
Expand Down
Loading