From 8c13c91f274c140ec7b233d4e567b630469f248a Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Tue, 31 Jan 2023 13:39:36 -0800 Subject: [PATCH] [valueblocks] ternary tests showing assignment bug A ternary that reassigns the same variable in both branches causes some weirdness with LeaveSSA, this PR just adds the failing case. --- .../ternary-assignment-expression.expect.md | 13 +++++ .../hir/ternary-assignment-expression.js | 6 +++ .../fixtures/hir/ternary-expression.expect.md | 53 +++++++++++++++++++ .../fixtures/hir/ternary-expression.js | 5 ++ 4 files changed, 77 insertions(+) create mode 100644 compiler/forget/src/__tests__/fixtures/hir/ternary-assignment-expression.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/hir/ternary-assignment-expression.js create mode 100644 compiler/forget/src/__tests__/fixtures/hir/ternary-expression.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/hir/ternary-expression.js diff --git a/compiler/forget/src/__tests__/fixtures/hir/ternary-assignment-expression.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ternary-assignment-expression.expect.md new file mode 100644 index 0000000000..3db71487a1 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/ternary-assignment-expression.expect.md @@ -0,0 +1,13 @@ + +## Input + +```javascript + +``` + +## Code + +```javascript + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/ternary-assignment-expression.js b/compiler/forget/src/__tests__/fixtures/hir/ternary-assignment-expression.js new file mode 100644 index 0000000000..654fa341fa --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/ternary-assignment-expression.js @@ -0,0 +1,6 @@ +// @only +function ternary(props) { + let x = 0; + const y = props.a ? (x = 1) : (x = 2); + return x + y; +} diff --git a/compiler/forget/src/__tests__/fixtures/hir/ternary-expression.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ternary-expression.expect.md new file mode 100644 index 0000000000..c10d292ccc --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/ternary-expression.expect.md @@ -0,0 +1,53 @@ + +## Input + +```javascript +function ternary(props) { + const a = props.a && props.b ? props.c || props.d : props.e ?? props.f; + const b = props.a ? (props.b && props.c ? props.d : props.e) : props.f; + return a ? b : null; +} + +``` + +## Code + +```javascript +function ternary(props) { + const $ = React.useMemoCache(); + const c_0 = $[0] !== props; + let t1; + if (c_0) { + t1 = props.a && props.b ? props.c || props.d : props.e ?? props.f; + $[0] = props; + $[1] = t1; + } else { + t1 = $[1]; + } + const a = t1; + const c_2 = $[2] !== props; + let t3; + if (c_2) { + t3 = props.a ? (props.b && props.c ? props.d : props.e) : props.f; + $[2] = props; + $[3] = t3; + } else { + t3 = $[3]; + } + const b = t3; + const c_4 = $[4] !== a; + const c_5 = $[5] !== b; + let t6; + if (c_4 || c_5) { + t6 = a ? b : null; + $[4] = a; + $[5] = b; + $[6] = t6; + } else { + t6 = $[6]; + } + return t6; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/ternary-expression.js b/compiler/forget/src/__tests__/fixtures/hir/ternary-expression.js new file mode 100644 index 0000000000..9752555303 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/ternary-expression.js @@ -0,0 +1,5 @@ +function ternary(props) { + const a = props.a && props.b ? props.c || props.d : props.e ?? props.f; + const b = props.a ? (props.b && props.c ? props.d : props.e) : props.f; + return a ? b : null; +}