[hir] Use types to infer constant declarations
Sathya Gunasekaran committed
Jan 16, 2023 at 15:34 UTC
39eb89f65e10aabac442c04072f9583756e8f769
2 files changed
+11
-35
compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts
+6
-28
@@ -10,6 +10,7 @@ import {
10
InstructionId,
11
InstructionKind,
12
InstructionValue,
13
+ isPrimitiveType,
14
LValue,
15
makeInstructionId,
16
Place,
@@ -287,7 +288,9 @@ function visitInstruction(context: Context, instr: ReactiveInstruction): void {
288
const range = lvalue.place.identifier.mutableRange;
289
// TODO: only assign Const if the value is never reassigned
290
const kind =
290
- range.end === range.start + 1 ? valueKind(instr.value) : DeclKind.Dynamic;
291
+ range.end === range.start + 1
292
+ ? valueKind(lvalue.place.identifier)
293
+ : DeclKind.Dynamic;
294
context.declare(lvalue.place.identifier, {
295
kind,
296
id: lvalue.place.identifier.mutableRange.start,
@@ -295,31 +298,6 @@ function visitInstruction(context: Context, instr: ReactiveInstruction): void {
298
}
299
}
300
298
-function valueKind(value: InstructionValue): DeclKind {
299
- switch (value.kind) {
300
- case "BinaryExpression":
301
- case "JSXText":
302
- case "Primitive": {
303
- return DeclKind.Const;
304
- }
305
- case "ComputedLoad":
306
- case "ComputedStore":
307
- case "PropertyStore":
308
- case "PropertyLoad":
309
- case "Identifier":
310
- case "ArrayExpression":
311
- case "CallExpression":
312
- case "JsxExpression":
313
- case "JsxFragment":
314
- case "NewExpression":
315
- case "ObjectExpression":
316
- case "FunctionExpression":
317
- case "OtherStatement":
318
- case "UnaryExpression": {
319
- return DeclKind.Dynamic;
320
- }
321
- default: {
322
- assertExhaustive(value, `Unexpected value kind '${(value as any).kind}'`);
323
- }
324
- }
301
+function valueKind(id: Identifier): DeclKind {
302
+ return isPrimitiveType(id) ? DeclKind.Const : DeclKind.Dynamic;
303
}
compiler/forget/src/__tests__/fixtures/hir/type-test-polymorphic.expect.md
+5
-7
@@ -41,20 +41,18 @@ function component() {
41
o = $[1];
42
}
43
44
- const c_2 = $[2] !== p;
45
- const c_3 = $[3] !== o;
44
+ const c_2 = $[2] !== o;
45
let x;
46
48
- if (c_2 || c_3) {
47
+ if (c_2) {
48
x = {};
49
x.t = p;
50
const z = x.t;
51
x.t = o;
53
- $[2] = p;
54
- $[3] = o;
55
- $[4] = x;
52
+ $[2] = o;
53
+ $[3] = x;
54
} else {
57
- x = $[4];
55
+ x = $[3];
56
}
57
58
const y = x.t;