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 indirect calls #51989

Merged
merged 2 commits into from
Jan 9, 2023
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
12 changes: 9 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35693,7 +35693,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return getRegularTypeOfObjectLiteral(rightType);
}
case SyntaxKind.CommaToken:
if (!compilerOptions.allowUnreachableCode && isSideEffectFree(left) && !isEvalNode(right)) {
if (!compilerOptions.allowUnreachableCode && isSideEffectFree(left) && !isIndirectCall(left.parent as BinaryExpression)) {
const sf = getSourceFileOfNode(left);
const sourceText = sf.text;
const start = skipTrivia(sourceText, left.pos);
Expand Down Expand Up @@ -35729,8 +35729,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}

function isEvalNode(node: Expression) {
return node.kind === SyntaxKind.Identifier && (node as Identifier).escapedText === "eval";
// Return true for "indirect calls", (i.e. `(0, x.f)(...)` or `(0, eval)(...)`), which prevents passing `this`.
function isIndirectCall(node: BinaryExpression): boolean {
return node.parent.kind === SyntaxKind.ParenthesizedExpression &&
isNumericLiteral(node.left) &&
node.left.text === "0" &&
(isCallExpression(node.parent.parent) && node.parent.parent.expression === node.parent || node.parent.parent.kind === SyntaxKind.TaggedTemplateExpression) &&
// special-case for "eval" because it's the only non-access case where an indirect call actually affects behavior.
(isAccessExpression(node.right) || isIdentifier(node.right) && node.right.escapedText === "eval");
}

// Return true if there was no error, false if there was an error.
Expand Down
10 changes: 8 additions & 2 deletions tests/baselines/reference/commaOperatorLeftSideUnused.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ tests/cases/compiler/commaOperatorLeftSideUnused.ts(38,7): error TS2695: Left si
tests/cases/compiler/commaOperatorLeftSideUnused.ts(39,7): error TS2695: Left side of comma operator is unused and has no side effects.
tests/cases/compiler/commaOperatorLeftSideUnused.ts(40,7): error TS2695: Left side of comma operator is unused and has no side effects.
tests/cases/compiler/commaOperatorLeftSideUnused.ts(41,7): error TS2695: Left side of comma operator is unused and has no side effects.
tests/cases/compiler/commaOperatorLeftSideUnused.ts(42,7): error TS2695: Left side of comma operator is unused and has no side effects.


==== tests/cases/compiler/commaOperatorLeftSideUnused.ts (23 errors) ====
==== tests/cases/compiler/commaOperatorLeftSideUnused.ts (24 errors) ====
var xx: any;
var yy: any;

Expand Down Expand Up @@ -111,6 +112,9 @@ tests/cases/compiler/commaOperatorLeftSideUnused.ts(41,7): error TS2695: Left si
xx = (+xx, 10);
~~~
!!! error TS2695: Left side of comma operator is unused and has no side effects.
xx = (0, xx)();
~
!!! error TS2695: Left side of comma operator is unused and has no side effects.

// OK cases
xx = (xx ? x++ : 4, 10);
Expand All @@ -122,4 +126,6 @@ tests/cases/compiler/commaOperatorLeftSideUnused.ts(41,7): error TS2695: Left si
xx = (Math.pow(3, 2), 4);
xx = (void xx, 10);
xx = (xx as any, 100);

xx = (0, xx.fn)();
xx = (0, xx['fn'])();
xx = (0, xx.fn)``;
13 changes: 12 additions & 1 deletion tests/baselines/reference/commaOperatorLeftSideUnused.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ xx = (!xx, 10);
xx = (~xx, 10);
xx = (-xx, 10);
xx = (+xx, 10);
xx = (0, xx)();

// OK cases
xx = (xx ? x++ : 4, 10);
Expand All @@ -51,9 +52,15 @@ xx = ((xx+= 4), xx);
xx = (Math.pow(3, 2), 4);
xx = (void xx, 10);
xx = (xx as any, 100);

xx = (0, xx.fn)();
xx = (0, xx['fn'])();
xx = (0, xx.fn)``;

//// [commaOperatorLeftSideUnused.js]
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
return cooked;
};
var xx;
var yy;
function fn() {
Expand Down Expand Up @@ -91,6 +98,7 @@ xx = (!xx, 10);
xx = (~xx, 10);
xx = (-xx, 10);
xx = (+xx, 10);
xx = (0, xx)();
// OK cases
xx = (xx ? x++ : 4, 10);
xx = (--xx, 3);
Expand All @@ -101,3 +109,6 @@ xx = ((xx += 4), xx);
xx = (Math.pow(3, 2), 4);
xx = (void xx, 10);
xx = (xx, 100);
xx = (0, xx.fn)();
xx = (0, xx['fn'])();
xx = (0, xx.fn)(__makeTemplateObject([""], [""]));
16 changes: 16 additions & 0 deletions tests/baselines/reference/commaOperatorLeftSideUnused.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ xx = (+xx, 10);
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))

xx = (0, xx)();
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))

// OK cases
xx = (xx ? x++ : 4, 10);
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))
Expand Down Expand Up @@ -151,3 +155,15 @@ xx = (xx as any, 100);
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))

xx = (0, xx.fn)();
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))

xx = (0, xx['fn'])();
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))

xx = (0, xx.fn)``;
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))

43 changes: 43 additions & 0 deletions tests/baselines/reference/commaOperatorLeftSideUnused.types
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ xx = (+xx, 10);
>xx : any
>10 : 10

xx = (0, xx)();
>xx = (0, xx)() : any
>xx : any
>(0, xx)() : any
>(0, xx) : any
>0, xx : any
>0 : 0
>xx : any

// OK cases
xx = (xx ? x++ : 4, 10);
>xx = (xx ? x++ : 4, 10) : 10
Expand Down Expand Up @@ -335,3 +344,37 @@ xx = (xx as any, 100);
>xx : any
>100 : 100

xx = (0, xx.fn)();
>xx = (0, xx.fn)() : any
>xx : any
>(0, xx.fn)() : any
>(0, xx.fn) : any
>0, xx.fn : any
>0 : 0
>xx.fn : any
>xx : any
>fn : any

xx = (0, xx['fn'])();
>xx = (0, xx['fn'])() : any
>xx : any
>(0, xx['fn'])() : any
>(0, xx['fn']) : any
>0, xx['fn'] : any
>0 : 0
>xx['fn'] : any
>xx : any
>'fn' : "fn"

xx = (0, xx.fn)``;
>xx = (0, xx.fn)`` : any
>xx : any
>(0, xx.fn)`` : any
>(0, xx.fn) : any
>0, xx.fn : any
>0 : 0
>xx.fn : any
>xx : any
>fn : any
>`` : ""

4 changes: 4 additions & 0 deletions tests/cases/compiler/commaOperatorLeftSideUnused.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ xx = (!xx, 10);
xx = (~xx, 10);
xx = (-xx, 10);
xx = (+xx, 10);
xx = (0, xx)();

// OK cases
xx = (xx ? x++ : 4, 10);
Expand All @@ -51,3 +52,6 @@ xx = ((xx+= 4), xx);
xx = (Math.pow(3, 2), 4);
xx = (void xx, 10);
xx = (xx as any, 100);
xx = (0, xx.fn)();
xx = (0, xx['fn'])();
xx = (0, xx.fn)``;