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

オープン
#720 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
62/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
typescript
領域
compilers

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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);).

主要言語
TypeScript
スター
1.6k
フォーク
88
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトの環境構築ファイルはまだ確認していません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

pocket-stack/pocketjs のほかの issue

pocket-stack/pocketjs の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。