diff --git a/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts b/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts index def6f9b899..4c29c6d41c 100644 --- a/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts +++ b/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts @@ -10,6 +10,7 @@ import { InstructionId, InstructionKind, InstructionValue, + isPrimitiveType, LValue, makeInstructionId, Place, @@ -287,7 +288,9 @@ function visitInstruction(context: Context, instr: ReactiveInstruction): void { const range = lvalue.place.identifier.mutableRange; // TODO: only assign Const if the value is never reassigned const kind = - range.end === range.start + 1 ? valueKind(instr.value) : DeclKind.Dynamic; + range.end === range.start + 1 + ? valueKind(lvalue.place.identifier) + : DeclKind.Dynamic; context.declare(lvalue.place.identifier, { kind, id: lvalue.place.identifier.mutableRange.start, @@ -295,31 +298,6 @@ function visitInstruction(context: Context, instr: ReactiveInstruction): void { } } -function valueKind(value: InstructionValue): DeclKind { - switch (value.kind) { - case "BinaryExpression": - case "JSXText": - case "Primitive": { - return DeclKind.Const; - } - case "ComputedLoad": - case "ComputedStore": - case "PropertyStore": - case "PropertyLoad": - case "Identifier": - case "ArrayExpression": - case "CallExpression": - case "JsxExpression": - case "JsxFragment": - case "NewExpression": - case "ObjectExpression": - case "FunctionExpression": - case "OtherStatement": - case "UnaryExpression": { - return DeclKind.Dynamic; - } - default: { - assertExhaustive(value, `Unexpected value kind '${(value as any).kind}'`); - } - } +function valueKind(id: Identifier): DeclKind { + return isPrimitiveType(id) ? DeclKind.Const : DeclKind.Dynamic; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/type-test-polymorphic.expect.md b/compiler/forget/src/__tests__/fixtures/hir/type-test-polymorphic.expect.md index 8b98fb4d11..3ee1f43ab4 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/type-test-polymorphic.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/type-test-polymorphic.expect.md @@ -41,20 +41,18 @@ function component() { o = $[1]; } - const c_2 = $[2] !== p; - const c_3 = $[3] !== o; + const c_2 = $[2] !== o; let x; - if (c_2 || c_3) { + if (c_2) { x = {}; x.t = p; const z = x.t; x.t = o; - $[2] = p; - $[3] = o; - $[4] = x; + $[2] = o; + $[3] = x; } else { - x = $[4]; + x = $[3]; } const y = x.t;