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

Suppress error caused by intermediate types in getTypeOfExpression #54380

Merged
merged 3 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 13 additions & 10 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,7 @@ export const enum CheckMode {
RestBindingElement = 1 << 6, // Checking a type that is going to be used to determine the type of a rest binding element
// e.g. in `const { a, ...rest } = foo`, when checking the type of `foo` to determine the type of `rest`,
// we need to preserve generic types instead of substituting them for constraints
TypeOnly = 1 << 7, // Called from getTypeOfExpression, diagnostics may be omitted
Copy link
Member

Choose a reason for hiding this comment

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

“Type-only” is an overloaded term that sounds like it refers to type-only imports. Would SkipDiagnostics be more clear?

Also, there are a number of mechanisms like this already in the checker, and it’s confusing which one to use. getSignatureApplicabilityError, compareSignaturesRelated, and checkApplicableSignatureForJsxOpeningLikeElement all take both a CheckMode and a reportErrors parameter. It now seems possible for those two to disagree. And there are surely lots of other functions that take a CheckMode but haven’t been updated to suppress errors for TypeOnly.

It seems like it might be better to add use a reportErrors parameter to checkBinaryLikeExpressionWorker, unless we’re prepared to handle this new CheckMode everywhere CheckModes are passed.

Copy link
Member Author

Choose a reason for hiding this comment

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

The new flag indicates that the originating call came from getTypeOfExpression, meaning that diagnostics can optionally be omitted with no ill effects. But unless we're dealing with an expression that could possibly cause new errors when operands are given narrower types than their declared types, there is no requirement that diagnostics must be omitted. It's bit of a mouthful, not sure what a good short name for that concept is.

I don't think there's an issue with disagreement between checkMode and reportErrors since the flag can be safely ignored in practically all scenarios--in fact, beyond the error for non-comparable operands, nothing needs to change as of now. But if we later discover other issues similar to this, we now have a mechanism to address them.

The only way to properly propagate the information is through a CheckMode flag.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I kinda agree with Andrew and from time to time I find myself wishing we had a better way to control when to report errors and when to not to based on whether we're actually checking things, or just getting types. But since we're nowhere close to doing this, I'm ok with the CheckMode trick for now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, in an ideal world we'd have more separation between the process of resolving types and that of checking the types. But given that both of those processes have a lot of common code paths (i.e. the entire top-down recursive decomposition is pretty much the same), we've chosen to conflate the them. This does occasionally muddle things, but it definitely also saves a lot of code.

}

/** @internal */
Expand Down Expand Up @@ -36753,7 +36754,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const rightType = getLastResult(state);
Debug.assertIsDefined(rightType);

result = checkBinaryLikeExpressionWorker(node.left, node.operatorToken, node.right, leftType, rightType, node);
result = checkBinaryLikeExpressionWorker(node.left, node.operatorToken, node.right, leftType, rightType, state.checkMode, node);
}

state.skip = false;
Expand Down Expand Up @@ -36824,7 +36825,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

const rightType = checkExpression(right, checkMode);
return checkBinaryLikeExpressionWorker(left, operatorToken, right, leftType, rightType, errorNode);
return checkBinaryLikeExpressionWorker(left, operatorToken, right, leftType, rightType, checkMode, errorNode);
}

function checkBinaryLikeExpressionWorker(
Expand All @@ -36833,6 +36834,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
right: Expression,
leftType: Type,
rightType: Type,
checkMode?: CheckMode,
errorNode?: Node
): Type {
const operator = operatorToken.kind;
Expand Down Expand Up @@ -36986,14 +36988,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
case SyntaxKind.ExclamationEqualsToken:
case SyntaxKind.EqualsEqualsEqualsToken:
case SyntaxKind.ExclamationEqualsEqualsToken:
if (isLiteralExpressionOfObject(left) || isLiteralExpressionOfObject(right)) {
const eqType = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.EqualsEqualsEqualsToken;
error(errorNode, Diagnostics.This_condition_will_always_return_0_since_JavaScript_compares_objects_by_reference_not_value, eqType ? "false" : "true");
if (!(checkMode && checkMode & CheckMode.TypeOnly)) {
if (isLiteralExpressionOfObject(left) || isLiteralExpressionOfObject(right)) {
const eqType = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.EqualsEqualsEqualsToken;
error(errorNode, Diagnostics.This_condition_will_always_return_0_since_JavaScript_compares_objects_by_reference_not_value, eqType ? "false" : "true");
}
checkNaNEquality(errorNode, operator, left, right);
reportOperatorErrorUnless((left, right) => isTypeEqualityComparableTo(left, right) || isTypeEqualityComparableTo(right, left));
}
checkNaNEquality(errorNode, operator, left, right);
reportOperatorErrorUnless((left, right) => isTypeEqualityComparableTo(left, right) || isTypeEqualityComparableTo(right, left));
return booleanType;

case SyntaxKind.InstanceOfKeyword:
return checkInstanceOfExpression(left, right, leftType, rightType);
case SyntaxKind.InKeyword:
Expand Down Expand Up @@ -37348,7 +37351,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function checkConditionalExpression(node: ConditionalExpression, checkMode?: CheckMode): Type {
const type = checkTruthinessExpression(node.condition);
const type = checkTruthinessExpression(node.condition, checkMode);
checkTestingKnownTruthyCallableOrAwaitableType(node.condition, type, node.whenTrue);
const type1 = checkExpression(node.whenTrue, checkMode);
const type2 = checkExpression(node.whenFalse, checkMode);
Expand Down Expand Up @@ -37729,7 +37732,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
const startInvocationCount = flowInvocationCount;
const type = checkExpression(node);
const type = checkExpression(node, CheckMode.TypeOnly);
// If control flow analysis was required to determine the type, it is worth caching.
if (flowInvocationCount !== startInvocationCount) {
const cache = flowTypeCache || (flowTypeCache = []);
Expand Down
43 changes: 43 additions & 0 deletions tests/baselines/reference/controlFlowNoIntermediateErrors.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
=== tests/cases/conformance/controlFlow/controlFlowNoIntermediateErrors.ts ===
// Repros from #46475

function f1() {
>f1 : Symbol(f1, Decl(controlFlowNoIntermediateErrors.ts, 0, 0))

let code: 0 | 1 | 2 = 0;
>code : Symbol(code, Decl(controlFlowNoIntermediateErrors.ts, 3, 7))

const otherCodes: (0 | 1 | 2)[] = [2, 0, 1, 0, 2, 2, 2, 0, 1, 0, 2, 1, 1, 0, 2, 1];
>otherCodes : Symbol(otherCodes, Decl(controlFlowNoIntermediateErrors.ts, 4, 9))

for (const code2 of otherCodes) {
>code2 : Symbol(code2, Decl(controlFlowNoIntermediateErrors.ts, 5, 14))
>otherCodes : Symbol(otherCodes, Decl(controlFlowNoIntermediateErrors.ts, 4, 9))

if (code2 === 0) {
>code2 : Symbol(code2, Decl(controlFlowNoIntermediateErrors.ts, 5, 14))

code = code === 2 ? 1 : 0;
>code : Symbol(code, Decl(controlFlowNoIntermediateErrors.ts, 3, 7))
>code : Symbol(code, Decl(controlFlowNoIntermediateErrors.ts, 3, 7))
}
else {
code = 2;
>code : Symbol(code, Decl(controlFlowNoIntermediateErrors.ts, 3, 7))
}
}
}

function f2() {
>f2 : Symbol(f2, Decl(controlFlowNoIntermediateErrors.ts, 13, 1))

let code: 0 | 1 = 0;
>code : Symbol(code, Decl(controlFlowNoIntermediateErrors.ts, 16, 7))

while (true) {
code = code === 1 ? 0 : 1;
>code : Symbol(code, Decl(controlFlowNoIntermediateErrors.ts, 16, 7))
>code : Symbol(code, Decl(controlFlowNoIntermediateErrors.ts, 16, 7))
}
}

80 changes: 80 additions & 0 deletions tests/baselines/reference/controlFlowNoIntermediateErrors.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
=== tests/cases/conformance/controlFlow/controlFlowNoIntermediateErrors.ts ===
// Repros from #46475

function f1() {
>f1 : () => void

let code: 0 | 1 | 2 = 0;
>code : 0 | 1 | 2
>0 : 0

const otherCodes: (0 | 1 | 2)[] = [2, 0, 1, 0, 2, 2, 2, 0, 1, 0, 2, 1, 1, 0, 2, 1];
>otherCodes : (0 | 1 | 2)[]
>[2, 0, 1, 0, 2, 2, 2, 0, 1, 0, 2, 1, 1, 0, 2, 1] : (0 | 1 | 2)[]
>2 : 2
>0 : 0
>1 : 1
>0 : 0
>2 : 2
>2 : 2
>2 : 2
>0 : 0
>1 : 1
>0 : 0
>2 : 2
>1 : 1
>1 : 1
>0 : 0
>2 : 2
>1 : 1

for (const code2 of otherCodes) {
>code2 : 0 | 1 | 2
>otherCodes : (0 | 1 | 2)[]

if (code2 === 0) {
>code2 === 0 : boolean
>code2 : 0 | 1 | 2
>0 : 0

code = code === 2 ? 1 : 0;
>code = code === 2 ? 1 : 0 : 0 | 1
>code : 0 | 1 | 2
>code === 2 ? 1 : 0 : 0 | 1
>code === 2 : boolean
>code : 0 | 1 | 2
>2 : 2
>1 : 1
>0 : 0
}
else {
code = 2;
>code = 2 : 2
>code : 0 | 1 | 2
>2 : 2
}
}
}

function f2() {
>f2 : () => void

let code: 0 | 1 = 0;
>code : 0 | 1
>0 : 0

while (true) {
>true : true

code = code === 1 ? 0 : 1;
>code = code === 1 ? 0 : 1 : 0 | 1
>code : 0 | 1
>code === 1 ? 0 : 1 : 0 | 1
>code === 1 : boolean
>code : 0 | 1
>1 : 1
>0 : 0
>1 : 1
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// @strict: true
// @noEmit: true

// Repros from #46475

function f1() {
let code: 0 | 1 | 2 = 0;
const otherCodes: (0 | 1 | 2)[] = [2, 0, 1, 0, 2, 2, 2, 0, 1, 0, 2, 1, 1, 0, 2, 1];
for (const code2 of otherCodes) {
if (code2 === 0) {
code = code === 2 ? 1 : 0;
}
else {
code = 2;
}
}
}

function f2() {
let code: 0 | 1 = 0;
while (true) {
code = code === 1 ? 0 : 1;
}
}