[alias] Don't alias primitives

This commit is contained in:
Sathya Gunasekaran
2023-01-16 15:34:21 +00:00
parent ab6a1dbbd4
commit 6ea0b6a42e
4 changed files with 69 additions and 0 deletions
+4
View File
@@ -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";
}
@@ -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;
}
@@ -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;
}
```
@@ -0,0 +1,11 @@
function component(a) {
let x = "foo";
if (a) {
x = "bar";
} else {
x = "baz";
}
let y = x;
mutate(y);
return y;
}