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

Disallow truthiness/nullishness checks on syntax that never varies on it #59217

Merged
merged 13 commits into from
Jul 22, 2024
121 changes: 117 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ import {
isAutoAccessorPropertyDeclaration,
isAwaitExpression,
isBinaryExpression,
isBinaryLogicalOperator,
isBindableObjectDefinePropertyCall,
isBindableStaticElementAccessExpression,
isBindableStaticNameExpression,
Expand Down Expand Up @@ -898,6 +899,7 @@ import {
NodeWithTypeArguments,
NonNullChain,
NonNullExpression,
NoSubstitutionTemplateLiteral,
not,
noTruncationMaximumTruncationLength,
NumberLiteralType,
Expand All @@ -913,6 +915,7 @@ import {
OptionalTypeNode,
or,
orderedRemoveItemAt,
OuterExpression,
OuterExpressionKinds,
ParameterDeclaration,
parameterIsThisKeyword,
Expand All @@ -928,6 +931,7 @@ import {
PatternAmbientModule,
PlusToken,
PostfixUnaryExpression,
PredicateSemantics,
PrefixUnaryExpression,
PrivateIdentifier,
Program,
Expand Down Expand Up @@ -39463,7 +39467,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return state;
}

checkGrammarNullishCoalesceWithLogicalExpression(node);
checkNullishCoalesceOperands(node);

const operator = node.operatorToken.kind;
if (operator === SyntaxKind.EqualsToken && (node.left.kind === SyntaxKind.ObjectLiteralExpression || node.left.kind === SyntaxKind.ArrayLiteralExpression)) {
Expand Down Expand Up @@ -39496,7 +39500,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (operator === SyntaxKind.AmpersandAmpersandToken || isIfStatement(parent)) {
checkTestingKnownTruthyCallableOrAwaitableOrEnumMemberType(node.left, leftType, isIfStatement(parent) ? parent.thenStatement : undefined);
}
checkTruthinessOfType(leftType, node.left);
if (isBinaryLogicalOperator(operator)) {
checkTruthinessOfType(leftType, node.left);
}
}
}
}
Expand Down Expand Up @@ -39562,7 +39568,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}

function checkGrammarNullishCoalesceWithLogicalExpression(node: BinaryExpression) {
function checkNullishCoalesceOperands(node: BinaryExpression) {
const { left, operatorToken, right } = node;
if (operatorToken.kind === SyntaxKind.QuestionQuestionToken) {
if (isBinaryExpression(left) && (left.operatorToken.kind === SyntaxKind.BarBarToken || left.operatorToken.kind === SyntaxKind.AmpersandAmpersandToken)) {
Expand All @@ -39571,7 +39577,63 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (isBinaryExpression(right) && (right.operatorToken.kind === SyntaxKind.BarBarToken || right.operatorToken.kind === SyntaxKind.AmpersandAmpersandToken)) {
grammarErrorOnNode(right, Diagnostics._0_and_1_operations_cannot_be_mixed_without_parentheses, tokenToString(right.operatorToken.kind), tokenToString(operatorToken.kind));
}

const leftTarget = skipOuterExpressions(left, OuterExpressionKinds.All);
const nullishSemantics = getSyntacticNullishnessSemantics(leftTarget);
if (nullishSemantics !== PredicateSemantics.Sometimes) {
if (node.parent.kind === SyntaxKind.BinaryExpression) {
error(leftTarget, Diagnostics.This_binary_expression_is_never_nullish_Are_you_missing_parentheses);
}
else {
if (nullishSemantics === PredicateSemantics.Always) {
error(leftTarget, Diagnostics.This_expression_is_always_nullish);
}
else {
error(leftTarget, Diagnostics.Right_operand_of_is_unreachable_because_the_left_operand_is_never_nullish);
}
}
}
}
}

function getSyntacticNullishnessSemantics(node: Node): PredicateSemantics {
switch (node.kind) {
case SyntaxKind.AwaitExpression:
case SyntaxKind.CallExpression:
case SyntaxKind.ElementAccessExpression:
case SyntaxKind.NewExpression:
case SyntaxKind.PropertyAccessExpression:
case SyntaxKind.YieldExpression:
return PredicateSemantics.Sometimes;
case SyntaxKind.BinaryExpression:
// List of operators that can produce null/undefined:
// = ??= ?? || ||= && &&=
switch ((node as BinaryExpression).operatorToken.kind) {
case SyntaxKind.EqualsToken:
case SyntaxKind.QuestionQuestionToken:
case SyntaxKind.QuestionQuestionEqualsToken:
case SyntaxKind.BarBarToken:
case SyntaxKind.BarBarEqualsToken:
case SyntaxKind.AmpersandAmpersandToken:
case SyntaxKind.AmpersandAmpersandEqualsToken:
return PredicateSemantics.Sometimes;
}
return PredicateSemantics.Never;
case SyntaxKind.TypeAssertionExpression:
case SyntaxKind.AsExpression:
case SyntaxKind.ParenthesizedExpression:
return getSyntacticNullishnessSemantics((node as OuterExpression).expression);
RyanCavanaugh marked this conversation as resolved.
Show resolved Hide resolved
case SyntaxKind.ConditionalExpression:
return getSyntacticNullishnessSemantics((node as ConditionalExpression).whenTrue) | getSyntacticNullishnessSemantics((node as ConditionalExpression).whenFalse);
case SyntaxKind.NullKeyword:
return PredicateSemantics.Always;
case SyntaxKind.Identifier:
if (getResolvedSymbol(node as Identifier) === undefinedSymbol) {
return PredicateSemantics.Always;
RyanCavanaugh marked this conversation as resolved.
Show resolved Hide resolved
}
return PredicateSemantics.Sometimes;
}
return PredicateSemantics.Never;
RyanCavanaugh marked this conversation as resolved.
Show resolved Hide resolved
}

// Note that this and `checkBinaryExpression` above should behave mostly the same, except this elides some
Expand All @@ -39582,7 +39644,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return checkDestructuringAssignment(left, checkExpression(right, checkMode), checkMode, right.kind === SyntaxKind.ThisKeyword);
}
let leftType: Type;
if (isLogicalOrCoalescingBinaryOperator(operator)) {
if (isBinaryLogicalOperator(operator)) {
leftType = checkTruthinessExpression(left, checkMode);
}
else {
Expand Down Expand Up @@ -44206,9 +44268,60 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (type.flags & TypeFlags.Void) {
error(node, Diagnostics.An_expression_of_type_void_cannot_be_tested_for_truthiness);
}
else {
const semantics = getSyntacticTruthySemantics(node);
if (semantics !== PredicateSemantics.Sometimes) {
error(
node,
semantics === PredicateSemantics.Always ?
Diagnostics.This_kind_of_expression_is_always_truthy :
Diagnostics.This_kind_of_expression_is_always_falsy,
);
}
}

return type;
}

function getSyntacticTruthySemantics(node: Node): PredicateSemantics {
switch (node.kind) {
case SyntaxKind.NumericLiteral:
// Allow `while(0)` or `while(1)`
if ((node as NumericLiteral).text === "0" || (node as NumericLiteral).text === "1") {
return PredicateSemantics.Sometimes;
}
return PredicateSemantics.Always;
case SyntaxKind.ArrayLiteralExpression:
case SyntaxKind.ArrowFunction:
case SyntaxKind.BigIntLiteral:
case SyntaxKind.ClassExpression:
case SyntaxKind.FunctionExpression:
case SyntaxKind.JsxElement:
case SyntaxKind.JsxSelfClosingElement:
case SyntaxKind.ObjectLiteralExpression:
case SyntaxKind.RegularExpressionLiteral:
return PredicateSemantics.Always;
case SyntaxKind.VoidExpression:
case SyntaxKind.NullKeyword:
return PredicateSemantics.Never;
case SyntaxKind.NoSubstitutionTemplateLiteral:
case SyntaxKind.StringLiteral:
return !!(node as StringLiteral | NoSubstitutionTemplateLiteral).text ? PredicateSemantics.Always : PredicateSemantics.Never;
case SyntaxKind.TypeAssertionExpression:
case SyntaxKind.AsExpression:
case SyntaxKind.ParenthesizedExpression:
return getSyntacticTruthySemantics((node as OuterExpression).expression);
RyanCavanaugh marked this conversation as resolved.
Show resolved Hide resolved
case SyntaxKind.ConditionalExpression:
return getSyntacticTruthySemantics((node as ConditionalExpression).whenTrue) | getSyntacticTruthySemantics((node as ConditionalExpression).whenFalse);
case SyntaxKind.Identifier:
if (getResolvedSymbol(node as Identifier) === undefinedSymbol) {
return PredicateSemantics.Never;
}
return PredicateSemantics.Sometimes;
}
return PredicateSemantics.Sometimes;
}

function checkTruthinessExpression(node: Expression, checkMode?: CheckMode) {
return checkTruthinessOfType(checkExpression(node, checkMode), node);
}
Expand Down
21 changes: 20 additions & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3911,7 +3911,26 @@
"category": "Error",
"code": 2868
},

"Right operand of ?? is unreachable because the left operand is never nullish.": {
"category": "Error",
"code": 2869
},
"This binary expression is never nullish. Are you missing parentheses?": {
"category": "Error",
"code": 2870
},
"This expression is always nullish.": {
"category": "Error",
"code": 2871
},
"This kind of expression is always truthy.": {
"category": "Error",
"code": 2872
},
"This kind of expression is always falsy.": {
"category": "Error",
"code": 2873
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
"code": 4000
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,14 @@ export const enum RelationComparisonResult {
Overflow = ComplexityOverflow | StackDepthOverflow,
}

/** @internal */
export const enum PredicateSemantics {
Copy link
Member

Choose a reason for hiding this comment

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

I think Ternary would actually cover this, if you didn't want to come up with something new.

None = 0,
Always = 1 << 0,
Never = 1 << 1,
Sometimes = Always | Never,
}

/** @internal */
export type NodeId = number;

Expand Down
3 changes: 2 additions & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7187,7 +7187,8 @@ export function modifierToFlag(token: SyntaxKind): ModifierFlags {
return ModifierFlags.None;
}

function isBinaryLogicalOperator(token: SyntaxKind): boolean {
/** @internal */
export function isBinaryLogicalOperator(token: SyntaxKind): boolean {
return token === SyntaxKind.BarBarToken || token === SyntaxKind.AmpersandAmpersandToken;
}

Expand Down
28 changes: 28 additions & 0 deletions tests/baselines/reference/checkJsdocReturnTag1.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
returns.js(20,12): error TS2872: This kind of expression is always truthy.


==== returns.js (1 errors) ====
// @ts-check
/**
* @returns {string} This comment is not currently exposed
*/
function f() {
return "hello";
}

/**
* @returns {string=} This comment is not currently exposed
*/
function f1() {
return "hello world";
}

/**
* @returns {string|number} This comment is not currently exposed
*/
function f2() {
return 5 || "hello";
~
!!! error TS2872: This kind of expression is always truthy.
}

5 changes: 4 additions & 1 deletion tests/baselines/reference/checkJsdocReturnTag2.errors.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
returns.js(6,5): error TS2322: Type 'number' is not assignable to type 'string'.
returns.js(13,5): error TS2322: Type 'number | boolean' is not assignable to type 'string | number'.
Type 'boolean' is not assignable to type 'string | number'.
returns.js(13,12): error TS2872: This kind of expression is always truthy.


==== returns.js (2 errors) ====
==== returns.js (3 errors) ====
// @ts-check
/**
* @returns {string} This comment is not currently exposed
Expand All @@ -22,5 +23,7 @@ returns.js(13,5): error TS2322: Type 'number | boolean' is not assignable to typ
~~~~~~
!!! error TS2322: Type 'number | boolean' is not assignable to type 'string | number'.
!!! error TS2322: Type 'boolean' is not assignable to type 'string | number'.
~
!!! error TS2872: This kind of expression is always truthy.
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
computedPropertyNames46_ES5.ts(2,6): error TS2873: This kind of expression is always falsy.


==== computedPropertyNames46_ES5.ts (1 errors) ====
var o = {
["" || 0]: 0
~~
!!! error TS2873: This kind of expression is always falsy.
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
computedPropertyNames46_ES6.ts(2,6): error TS2873: This kind of expression is always falsy.


==== computedPropertyNames46_ES6.ts (1 errors) ====
var o = {
["" || 0]: 0
~~
!!! error TS2873: This kind of expression is always falsy.
};
23 changes: 23 additions & 0 deletions tests/baselines/reference/computedPropertyNames48_ES5.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
computedPropertyNames48_ES5.ts(16,6): error TS2873: This kind of expression is always falsy.


==== computedPropertyNames48_ES5.ts (1 errors) ====
declare function extractIndexer<T>(p: { [n: number]: T }): T;

enum E { x }

var a: any;

extractIndexer({
[a]: ""
}); // Should return string

extractIndexer({
[E.x]: ""
}); // Should return string

extractIndexer({
["" || 0]: ""
~~
!!! error TS2873: This kind of expression is always falsy.
}); // Should return any (widened form of undefined)
2 changes: 2 additions & 0 deletions tests/baselines/reference/computedPropertyNames48_ES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum E { x }

var a: any;
>a : any
> : ^^^
Copy link
Member

Choose a reason for hiding this comment

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

What happened that updated the types baselines like this?

Copy link
Member

Choose a reason for hiding this comment

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

This is a goofy quirk of the types baselines; in typeWriter.ts:

// Distinguish `errorType`s from `any`s; but only if the file has no errors.

So this types file changed because computedPropertyNames48_ES5.errors.txt was emitted.

Copy link
Member

Choose a reason for hiding this comment

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

(Honestly, I wonder if it's worth the special case; I noticed it separately while working on something else)


extractIndexer({
>extractIndexer({ [a]: ""}) : string
Expand All @@ -30,6 +31,7 @@ extractIndexer({
>[a] : string
> : ^^^^^^
>a : any
> : ^^^
>"" : ""
> : ^^

Expand Down
23 changes: 23 additions & 0 deletions tests/baselines/reference/computedPropertyNames48_ES6.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
computedPropertyNames48_ES6.ts(16,6): error TS2873: This kind of expression is always falsy.


==== computedPropertyNames48_ES6.ts (1 errors) ====
declare function extractIndexer<T>(p: { [n: number]: T }): T;

enum E { x }

var a: any;

extractIndexer({
[a]: ""
}); // Should return string

extractIndexer({
[E.x]: ""
}); // Should return string

extractIndexer({
["" || 0]: ""
~~
!!! error TS2873: This kind of expression is always falsy.
}); // Should return any (widened form of undefined)
Loading
Loading