Skip to content

Fix top level logical assignment leak #40536

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 3 commits into from
Mar 14, 2022
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
1 change: 0 additions & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,6 @@ namespace ts {
node = node.parent;
}
return !isStatementCondition(node) &&
!isLogicalAssignmentExpression(node.parent) &&
Copy link
Member

@sandersn sandersn Mar 31, 2021

Choose a reason for hiding this comment

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

This function is called when node was a LogicalAssignmentOperator. But from the tests, I guess that the failing case was when node was a LogicalOperator and node.parent was a LogicalAssignmentOperator. I don't understand why the control flow doesn't get created when the parent LogicalAssignmentOperator is node.

Also, are there now more control flow nodes created for other nodes besides LogicalAssignmentOperators? I'll run the perf tests.

Copy link
Member

Choose a reason for hiding this comment

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

Much later: I was wrong. It is called when node is a QuestionQuestionToken and node.parent is a LogicalAssignmentExpression. From the new test, when node is x in d ??= x ?? "string", and node.parent is ??=.

I guess the deleted line was added because ??= is a logical expression...but it's also an assignment expression.

!isLogicalExpression(node.parent) &&
!(isOptionalChain(node.parent) && node.parent.expression === node);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/baselines/reference/logicalAssignment11(target=es2015).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [logicalAssignment11.ts]
let x: string | undefined;

let d: string | undefined;
d ?? (d = x ?? "x")
d.length;

let e: string | undefined;
e ??= x ?? "x"
e.length

//// [logicalAssignment11.js]
"use strict";
let x;
let d;
d !== null && d !== void 0 ? d : (d = x !== null && x !== void 0 ? x : "x");
d.length;
let e;
e !== null && e !== void 0 ? e : (e = x !== null && x !== void 0 ? x : "x");
e.length;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
let x: string | undefined;
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))

let d: string | undefined;
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))

d ?? (d = x ?? "x")
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))

d.length;
>d.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))

let e: string | undefined;
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))

e ??= x ?? "x"
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))

e.length
>e.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))

37 changes: 37 additions & 0 deletions tests/baselines/reference/logicalAssignment11(target=es2015).types
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
let x: string | undefined;
>x : string | undefined

let d: string | undefined;
>d : string | undefined

d ?? (d = x ?? "x")
>d ?? (d = x ?? "x") : string
>d : string | undefined
>(d = x ?? "x") : string
>d = x ?? "x" : string
>d : string | undefined
>x ?? "x" : string
>x : string | undefined
>"x" : "x"

d.length;
>d.length : number
>d : string
>length : number

let e: string | undefined;
>e : string | undefined

e ??= x ?? "x"
>e ??= x ?? "x" : string
>e : string | undefined
>x ?? "x" : string
>x : string | undefined
>"x" : "x"

e.length
>e.length : number
>e : string
>length : number

20 changes: 20 additions & 0 deletions tests/baselines/reference/logicalAssignment11(target=es2020).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [logicalAssignment11.ts]
let x: string | undefined;

let d: string | undefined;
d ?? (d = x ?? "x")
d.length;

let e: string | undefined;
e ??= x ?? "x"
e.length

//// [logicalAssignment11.js]
"use strict";
let x;
let d;
d ?? (d = x ?? "x");
d.length;
let e;
e ?? (e = x ?? "x");
e.length;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
let x: string | undefined;
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))

let d: string | undefined;
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))

d ?? (d = x ?? "x")
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))

d.length;
>d.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))

let e: string | undefined;
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))

e ??= x ?? "x"
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))

e.length
>e.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))

37 changes: 37 additions & 0 deletions tests/baselines/reference/logicalAssignment11(target=es2020).types
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
let x: string | undefined;
>x : string | undefined

let d: string | undefined;
>d : string | undefined

d ?? (d = x ?? "x")
>d ?? (d = x ?? "x") : string
>d : string | undefined
>(d = x ?? "x") : string
>d = x ?? "x" : string
>d : string | undefined
>x ?? "x" : string
>x : string | undefined
>"x" : "x"

d.length;
>d.length : number
>d : string
>length : number

let e: string | undefined;
>e : string | undefined

e ??= x ?? "x"
>e ??= x ?? "x" : string
>e : string | undefined
>x ?? "x" : string
>x : string | undefined
>"x" : "x"

e.length
>e.length : number
>e : string
>length : number

20 changes: 20 additions & 0 deletions tests/baselines/reference/logicalAssignment11(target=esnext).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [logicalAssignment11.ts]
let x: string | undefined;

let d: string | undefined;
d ?? (d = x ?? "x")
d.length;

let e: string | undefined;
e ??= x ?? "x"
e.length

//// [logicalAssignment11.js]
"use strict";
let x;
let d;
d ?? (d = x ?? "x");
d.length;
let e;
e ??= x ?? "x";
e.length;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
let x: string | undefined;
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))

let d: string | undefined;
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))

d ?? (d = x ?? "x")
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))

d.length;
>d.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))

let e: string | undefined;
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))

e ??= x ?? "x"
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))

e.length
>e.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))

37 changes: 37 additions & 0 deletions tests/baselines/reference/logicalAssignment11(target=esnext).types
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
let x: string | undefined;
>x : string | undefined

let d: string | undefined;
>d : string | undefined

d ?? (d = x ?? "x")
>d ?? (d = x ?? "x") : string
>d : string | undefined
>(d = x ?? "x") : string
>d = x ?? "x" : string
>d : string | undefined
>x ?? "x" : string
>x : string | undefined
>"x" : "x"

d.length;
>d.length : number
>d : string
>length : number

let e: string | undefined;
>e : string | undefined

e ??= x ?? "x"
>e ??= x ?? "x" : string
>e : string | undefined
>x ?? "x" : string
>x : string | undefined
>"x" : "x"

e.length
>e.length : number
>e : string
>length : number

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @strict: true
// @target: esnext, es2020, es2015

let x: string | undefined;

let d: string | undefined;
d ?? (d = x ?? "x")
d.length;

let e: string | undefined;
e ??= x ?? "x"
e.length