microts: a module constant read twice fails with "Invalid Model IR: duplicate binder" when its initializer hoists temporaries

Open
#720 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
62/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
compilers

Research direction

Start at loadConstant in microts/compiler/aot-model-frontend.ts (lines 229-236) and read at line 261, where the cached sequence is shallow-copied as { ...value, loc } and carries the hoisted let _argN binder ids into every use. Read bind and assertModelProgram in microts/compiler/aot-model-tasks.ts (lines 152-168) to see why the reused ids are rejected. Reproduce with the app.ts constant read twice in step, then make both compile commands succeed and add a regression test covering a hoisting initializer read twice.

Written by the indexing model from the issue text.

Description

bug

Version

PocketJS v0.17.0 (83c6cac9, current main). macOS 27.2 arm64, Bun 1.4.2.

Repro

pocket.host.json, pocket.host.config.ts, main.tsx
{
  "format": "pocket-host/2",
  "id": "repro-esp32s3",
  "platform": "esp-idf",
  "form": "takeover",
  "cadences": [30],
  "display": {
    "logicalViewports": [[240, 240]],
    "presentations": ["native"],
    "rasterDensity": 1,
    "animation": "repaint"
  },
  "panel": { "primary": [240, 240] },
  "capabilities": ["input.buttons", "text.glyphs.baked"]
}
// pocket.host.config.ts
import { defineHostConfig } from "@pocketjs/framework/host-config";

export default defineHostConfig({
  host: "./pocket.host.json",
  app: {
    id: "dev.example.repro",
    title: "Repro",
    version: "0.1.0",
    entry: "main.tsx",
    output: "repro",
    framework: "solid",
    viewport: { logical: [240, 240], presentation: "native" },
    features: ["input.buttons", "text.glyphs.baked"],
    aot: true,
    model: "compiled",
  },
});
// main.tsx
import { mount } from "@pocketjs/framework/solid";
import App from "./app.tsx";
mount(() => <App />);
// app.ts
import { createSignal } from "solid-js";
import { f32 } from "@pocketjs/framework/solid/std";

const K = f32(0.5) * f32(2);

export const [value, setValue] = createSignal<f32>(f32(1));

export function step(): void {
  setValue(value() * K + K);
}
// app.tsx
import { Text, View } from "@pocketjs/framework/solid/components";
import { step, value } from "./app";

export default function App() {
  return (
    <View class="w-full h-full p-4 bg-slate-100" focusable onPress={() => step()}>
      <Text class="text-lg text-slate-900">{value()}</Text>
    </View>
  );
}
bun $POCKETJS/tools/pocket.ts compile --config pocket.host.config.ts --aot --out gen
bun $POCKETJS/tools/pocket.ts compile --config pocket.host.config.ts --output out/app.pkb

Actual

Both builds stop:

PocketJS: Invalid Model IR: duplicate binder 4
error: Invalid Model IR: duplicate binder 4
      at fail (microts/compiler/aot-model-tasks.ts:152:56)
      at bind (microts/compiler/aot-model-tasks.ts:168:127)
      at checkBlock (microts/compiler/aot-model-tasks.ts:222:39)
      at checkExpr (microts/compiler/aot-model-tasks.ts:181:58)
      at checkExpr (microts/compiler/aot-model-tasks.ts:184:53)
      at checkBlock (microts/compiler/aot-model-tasks.ts:229:80)
      at assertModelProgram (microts/compiler/aot-model-tasks.ts:272:7)
      at analyzeModel (microts/compiler/aot-model-frontend.ts:1091:3)
Constant and uses in step Result
const K = f32(0.5) * f32(2);, read twice duplicate binder
const A = f32(0.5); const K = A * f32(2);, K read twice duplicate binder
const K = f32(0.5) * f32(2);, read once compiles
const K = f32(0.5);, read twice compiles
const K: f32 = 1;, read twice compiles

Expected

The model compiles, and step multiplies by 1 and adds 1, as in TypeScript.

Cause

loadConstant (microts/compiler/aot-model-frontend.ts:229–236) caches the result of isolated(initializer). When lowering the initializer hoists operands into temporaries (the two f32() calls become let _argN statements), the cached value is a sequence expression that owns those let binders. read (aot-model-frontend.ts:261) inlines the constant at each use as { ...value, loc }, a shallow copy that keeps the binder ids. The second use in one function declares the same binders again, and assertModelProgram rejects them (aot-model-tasks.ts:168).

Workaround

Write the folded value with a type annotation (const K: f32 = 1;), or keep the initializer to one conversion call (const K = f32(1);).

Dominant language
TypeScript
Stars
1.6k
Forks
88
PR merge metrics
No merged PRs in 30d

Getting set up

We have not checked this project's setup files yet. Start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pocket-stack/pocketjs

All issues in pocket-stack/pocketjs

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.