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

Allow |, &, and ^ operators to have null/undefined inputs #22232

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 18 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19129,8 +19129,9 @@ namespace ts {
return silentNeverType;
}

leftType = checkNonNullType(leftType, left);
rightType = checkNonNullType(rightType, right);
const getType = allowNullInBinaryExpression(operator) ? getNonNullableType : checkNonNullType;
leftType = getType(leftType, left);
rightType = getType(rightType, right);

let suggestedOperator: SyntaxKind;
// if a user tries to apply a bitwise operator to 2 boolean operands
Expand Down Expand Up @@ -19239,6 +19240,21 @@ namespace ts {
return rightType;
}

/** For these binary expressions, `null` and `undefined` are treated like `0`. The result is defined even if the inputs aren't. */
function allowNullInBinaryExpression(operator: SyntaxKind): boolean {
switch (operator) {
case SyntaxKind.AmpersandToken:
case SyntaxKind.BarToken:
case SyntaxKind.CaretToken:
case SyntaxKind.AmpersandEqualsToken:
case SyntaxKind.BarEqualsToken:
case SyntaxKind.CaretEqualsToken:
return true;
default:
return false;
}
}

function isEvalNode(node: Expression) {
return node.kind === SyntaxKind.Identifier && (node as Identifier).escapedText === "eval";
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,9 @@ tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeti
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(78,17): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(79,17): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(80,19): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(83,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(84,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(85,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(86,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(87,15): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(88,15): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(89,15): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(90,17): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(93,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(94,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(95,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(96,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(97,15): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(98,15): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(99,15): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(100,17): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(103,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(104,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(105,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(106,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(107,15): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(108,15): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(109,15): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts(110,17): error TS2531: Object is possibly 'null'.


==== tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts (80 errors) ====
==== tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithNullValueAndValidOperands.ts (56 errors) ====
// If one operand is the null or undefined value, it is treated as having the type of the
// other operand.

Expand Down Expand Up @@ -276,78 +252,30 @@ tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeti

// operator &
var rh1 = null & a;
~~~~
!!! error TS2531: Object is possibly 'null'.
var rh2 = null & b;
~~~~
!!! error TS2531: Object is possibly 'null'.
var rh3 = null & 1;
~~~~
!!! error TS2531: Object is possibly 'null'.
var rh4 = null & E.a;
~~~~
!!! error TS2531: Object is possibly 'null'.
var rh5 = a & null;
~~~~
!!! error TS2531: Object is possibly 'null'.
var rh6 = b & null;
~~~~
!!! error TS2531: Object is possibly 'null'.
var rh7 = 0 & null;
~~~~
!!! error TS2531: Object is possibly 'null'.
var rh8 = E.b & null;
~~~~
!!! error TS2531: Object is possibly 'null'.

// operator ^
var ri1 = null ^ a;
~~~~
!!! error TS2531: Object is possibly 'null'.
var ri2 = null ^ b;
~~~~
!!! error TS2531: Object is possibly 'null'.
var ri3 = null ^ 1;
~~~~
!!! error TS2531: Object is possibly 'null'.
var ri4 = null ^ E.a;
~~~~
!!! error TS2531: Object is possibly 'null'.
var ri5 = a ^ null;
~~~~
!!! error TS2531: Object is possibly 'null'.
var ri6 = b ^ null;
~~~~
!!! error TS2531: Object is possibly 'null'.
var ri7 = 0 ^ null;
~~~~
!!! error TS2531: Object is possibly 'null'.
var ri8 = E.b ^ null;
~~~~
!!! error TS2531: Object is possibly 'null'.

// operator |
var rj1 = null | a;
~~~~
!!! error TS2531: Object is possibly 'null'.
var rj2 = null | b;
~~~~
!!! error TS2531: Object is possibly 'null'.
var rj3 = null | 1;
~~~~
!!! error TS2531: Object is possibly 'null'.
var rj4 = null | E.a;
~~~~
!!! error TS2531: Object is possibly 'null'.
var rj5 = a | null;
~~~~
!!! error TS2531: Object is possibly 'null'.
var rj6 = b | null;
~~~~
!!! error TS2531: Object is possibly 'null'.
var rj7 = 0 | null;
~~~~
!!! error TS2531: Object is possibly 'null'.
var rj8 = E.b | null;
~~~~
!!! error TS2531: Object is possibly 'null'.
var rj8 = E.b | null;
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,9 @@ tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeti
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(40,25): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(41,11): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(41,25): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(44,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(44,18): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(45,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(45,18): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(46,11): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(46,23): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(47,11): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(47,23): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(50,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(50,18): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(51,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(51,18): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(52,11): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(52,23): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(53,11): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(53,23): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(56,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(56,18): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(57,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(57,18): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(58,11): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(58,23): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(59,11): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts(59,23): error TS2532: Object is possibly 'undefined'.


==== tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts (80 errors) ====
==== tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.ts (56 errors) ====
// operator *
var ra1 = null * null;
~~~~
Expand Down Expand Up @@ -237,66 +213,18 @@ tests/cases/conformance/expressions/binaryOperators/arithmeticOperator/arithmeti

// operator &
var rh1 = null & null;
~~~~
!!! error TS2531: Object is possibly 'null'.
~~~~
!!! error TS2531: Object is possibly 'null'.
var rh2 = null & undefined;
~~~~
!!! error TS2531: Object is possibly 'null'.
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
var rh3 = undefined & null;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
~~~~
!!! error TS2531: Object is possibly 'null'.
var rh4 = undefined & undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.

// operator ^
var ri1 = null ^ null;
~~~~
!!! error TS2531: Object is possibly 'null'.
~~~~
!!! error TS2531: Object is possibly 'null'.
var ri2 = null ^ undefined;
~~~~
!!! error TS2531: Object is possibly 'null'.
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
var ri3 = undefined ^ null;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
~~~~
!!! error TS2531: Object is possibly 'null'.
var ri4 = undefined ^ undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.

// operator |
var rj1 = null | null;
~~~~
!!! error TS2531: Object is possibly 'null'.
~~~~
!!! error TS2531: Object is possibly 'null'.
var rj2 = null | undefined;
~~~~
!!! error TS2531: Object is possibly 'null'.
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
var rj3 = undefined | null;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
~~~~
!!! error TS2531: Object is possibly 'null'.
var rj4 = undefined | undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
var rj4 = undefined | undefined;
Loading