From 6ea0b6a42e0609dbb0ba80ca1ce638ca6cb506c4 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Mon, 16 Jan 2023 15:34:21 +0000 Subject: [PATCH] [alias] Don't alias primitives --- compiler/forget/src/HIR/HIR.ts | 4 ++ compiler/forget/src/Inference/InferAlias.ts | 4 ++ .../hir/primitive-alias-mutate.expect.md | 50 +++++++++++++++++++ .../fixtures/hir/primitive-alias-mutate.js | 11 ++++ 4 files changed, 69 insertions(+) create mode 100644 compiler/forget/src/__tests__/fixtures/hir/primitive-alias-mutate.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/hir/primitive-alias-mutate.js diff --git a/compiler/forget/src/HIR/HIR.ts b/compiler/forget/src/HIR/HIR.ts index 99b4297bdb..e1fbeebb19 100644 --- a/compiler/forget/src/HIR/HIR.ts +++ b/compiler/forget/src/HIR/HIR.ts @@ -607,3 +607,7 @@ function phiTypeEquals(tA: Type, tB: Type): boolean { export function isObjectType(id: Identifier): boolean { return id.type.kind === "Object"; } + +export function isPrimitiveType(id: Identifier): boolean { + return id.type.kind === "Primitive"; +} diff --git a/compiler/forget/src/Inference/InferAlias.ts b/compiler/forget/src/Inference/InferAlias.ts index 970e3fe2f6..6a1127369d 100644 --- a/compiler/forget/src/Inference/InferAlias.ts +++ b/compiler/forget/src/Inference/InferAlias.ts @@ -3,6 +3,7 @@ import { HIRFunction, Identifier, Instruction, + isPrimitiveType, LValue, Place, } from "../HIR/HIR"; @@ -33,6 +34,9 @@ function inferInstr(instr: Instruction, state: AliasAnalyser) { let alias: Place | null = null; switch (instrValue.kind) { case "Identifier": { + if (isPrimitiveType(instrValue.identifier)) { + return; + } alias = instrValue; break; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/primitive-alias-mutate.expect.md b/compiler/forget/src/__tests__/fixtures/hir/primitive-alias-mutate.expect.md new file mode 100644 index 0000000000..47a727bf64 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/primitive-alias-mutate.expect.md @@ -0,0 +1,50 @@ + +## Input + +```javascript +function component(a) { + let x = "foo"; + if (a) { + x = "bar"; + } else { + x = "baz"; + } + let y = x; + mutate(y); + return y; +} + +``` + +## Code + +```javascript +function component(a) { + const $ = React.useMemoCache(); + const x = "foo"; + const c_0 = $[0] !== a; + let x$0; + if (c_0) { + x$0 = undefined; + + if (a) { + const x$1 = "bar"; + x$0 = x$1; + } else { + const x$2 = "baz"; + x$0 = x$2; + } + + $[0] = a; + $[1] = x$0; + } else { + x$0 = $[1]; + } + + const y = x$0; + mutate(y); + return y; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/primitive-alias-mutate.js b/compiler/forget/src/__tests__/fixtures/hir/primitive-alias-mutate.js new file mode 100644 index 0000000000..f77284dcd9 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/primitive-alias-mutate.js @@ -0,0 +1,11 @@ +function component(a) { + let x = "foo"; + if (a) { + x = "bar"; + } else { + x = "baz"; + } + let y = x; + mutate(y); + return y; +}