[alias] Don't alias primitives
Sathya Gunasekaran committed
Jan 16, 2023 at 15:34 UTC
6ea0b6a42e0609dbb0ba80ca1ce638ca6cb506c4
4 files changed
+69
compiler/forget/src/HIR/HIR.ts
+4
@@ -607,3 +607,7 @@ function phiTypeEquals(tA: Type, tB: Type): boolean {
607
export function isObjectType(id: Identifier): boolean {
608
return id.type.kind === "Object";
609
}
610
+
611
+export function isPrimitiveType(id: Identifier): boolean {
612
+ return id.type.kind === "Primitive";
613
+}
compiler/forget/src/Inference/InferAlias.ts
+4
@@ -3,6 +3,7 @@ import {
3
HIRFunction,
4
Identifier,
5
Instruction,
6
+ isPrimitiveType,
7
LValue,
8
Place,
9
} from "../HIR/HIR";
@@ -33,6 +34,9 @@ function inferInstr(instr: Instruction, state: AliasAnalyser) {
34
let alias: Place | null = null;
35
switch (instrValue.kind) {
36
case "Identifier": {
37
+ if (isPrimitiveType(instrValue.identifier)) {
38
+ return;
39
+ }
40
alias = instrValue;
41
break;
42
}
compiler/forget/src/__tests__/fixtures/hir/primitive-alias-mutate.expect.md
new
+50
@@ -0,0 +1,50 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function component(a) {
6
+ let x = "foo";
7
+ if (a) {
8
+ x = "bar";
9
+ } else {
10
+ x = "baz";
11
+ }
12
+ let y = x;
13
+ mutate(y);
14
+ return y;
15
+}
16
+
17
+```
18
+
19
+## Code
20
+
21
+```javascript
22
+function component(a) {
23
+ const $ = React.useMemoCache();
24
+ const x = "foo";
25
+ const c_0 = $[0] !== a;
26
+ let x$0;
27
+ if (c_0) {
28
+ x$0 = undefined;
29
+
30
+ if (a) {
31
+ const x$1 = "bar";
32
+ x$0 = x$1;
33
+ } else {
34
+ const x$2 = "baz";
35
+ x$0 = x$2;
36
+ }
37
+
38
+ $[0] = a;
39
+ $[1] = x$0;
40
+ } else {
41
+ x$0 = $[1];
42
+ }
43
+
44
+ const y = x$0;
45
+ mutate(y);
46
+ return y;
47
+}
48
+
49
+```
50
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/primitive-alias-mutate.js
new
+11
@@ -0,0 +1,11 @@
1
+function component(a) {
2
+ let x = "foo";
3
+ if (a) {
4
+ x = "bar";
5
+ } else {
6
+ x = "baz";
7
+ }
8
+ let y = x;
9
+ mutate(y);
10
+ return y;
11
+}