Skip to content

Even better "always true/false" errors #25311

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 2 commits into from
Jun 29, 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
33 changes: 16 additions & 17 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20904,31 +20904,30 @@ namespace ts {
}

function reportOperatorError() {
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
));
const leftStr = typeToString(leftType);
const rightStr = typeToString(rightType);
const errNode = errorNode || operatorToken;
if (!tryGiveBetterPrimaryError(errNode, leftStr, rightStr)) {
error(
errNode,
Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2,
tokenToString(operatorToken.kind),
leftStr,
rightStr,
);
}
}

function giveBetterPrimaryError(elaboration: DiagnosticMessageChain) {
function tryGiveBetterPrimaryError(errNode: Node, leftStr: string, rightStr: string) {
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");
return error(errNode, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap, "false", leftStr, rightStr);
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 error(errNode, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap, "true", leftStr, rightStr);
}
return elaboration;
return undefined;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@
"category": "Error",
"code": 2366
},
"The types of these values indicate that this condition will always be '{0}'.": {
"This condition will always return '{0}' since the types '{1}' and '{2}' have no overlap.": {
"category": "Error",
"code": 2367
},
Expand Down
6 changes: 3 additions & 3 deletions src/testRunner/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5016,7 +5016,7 @@ namespace ts.projectSystem {
);
const errorResult = <protocol.Diagnostic[]>session.executeCommand(getErrRequest).response;
assert.isTrue(errorResult.length === 1);
assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code);
assert.equal(errorResult[0].code, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap.code);
});

it("should report semantic errors for configured js project with '// @ts-check' and skipLibCheck=true", () => {
Expand All @@ -5043,7 +5043,7 @@ namespace ts.projectSystem {
);
const errorResult = <protocol.Diagnostic[]>session.executeCommand(getErrRequest).response;
assert.isTrue(errorResult.length === 1);
assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code);
assert.equal(errorResult[0].code, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap.code);
});

it("should report semantic errors for configured js project with checkJs=true and skipLibCheck=true", () => {
Expand Down Expand Up @@ -5072,7 +5072,7 @@ namespace ts.projectSystem {
);
const errorResult = <protocol.Diagnostic[]>session.executeCommand(getErrRequest).response;
assert.isTrue(errorResult.length === 1);
assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code);
assert.equal(errorResult[0].code, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap.code);
});
});

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5152,7 +5152,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;
This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap: 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: 4 additions & 8 deletions tests/baselines/reference/capturedLetConstInLoop5.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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(174,13): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.


==== tests/cases/compiler/capturedLetConstInLoop5.ts (2 errors) ====
Expand Down Expand Up @@ -180,8 +178,7 @@ tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2367: The types
(() => x + v);
if (x == 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'.
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
return;
}
}
Expand Down Expand Up @@ -238,8 +235,7 @@ tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2367: The types
(() => x + y + v);
if (x == 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'.
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
return;
}
}
Expand Down
12 changes: 4 additions & 8 deletions tests/baselines/reference/capturedLetConstInLoop5_ES6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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(174,13): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.


==== tests/cases/compiler/capturedLetConstInLoop5_ES6.ts (2 errors) ====
Expand Down Expand Up @@ -180,8 +178,7 @@ tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2367: The t
(() => x + v);
if (x == 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'.
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
return;
}
}
Expand Down Expand Up @@ -238,8 +235,7 @@ tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2367: The t
(() => x + y + v);
if (x == 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'.
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
return;
}
}
Expand Down
24 changes: 8 additions & 16 deletions tests/baselines/reference/capturedLetConstInLoop6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
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(147,9): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6.ts(150,9): error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6.ts(194,9): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.


==== tests/cases/compiler/capturedLetConstInLoop6.ts (4 errors) ====
Expand Down Expand Up @@ -157,14 +153,12 @@ tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2367: The types
(() => x);
if (x == 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'.
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
break;
}
if (x == 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'.
!!! error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
continue;
}
}
Expand Down Expand Up @@ -210,14 +204,12 @@ tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2367: The types
(() => x + y);
if (x == 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'.
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
break;
}
if (x == 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'.
!!! error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
continue;
}
}
Expand Down
24 changes: 8 additions & 16 deletions tests/baselines/reference/capturedLetConstInLoop6_ES6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
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(147,9): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(150,9): error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(194,9): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.


==== tests/cases/compiler/capturedLetConstInLoop6_ES6.ts (4 errors) ====
Expand Down Expand Up @@ -157,14 +153,12 @@ tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2367: The ty
(() => x);
if (x == 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'.
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
break;
}
if (x == 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'.
!!! error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
continue;
}
}
Expand Down Expand Up @@ -210,14 +204,12 @@ tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2367: The ty
(() => x + y);
if (x == 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'.
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
break;
}
if (x == 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'.
!!! error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
continue;
}
}
Expand Down
Loading