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

Đang mở
#720 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
62/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
typescript
Lĩnh vực
compilers

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
TypeScript
Star
1.6k
Fork
88
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Chúng tôi chưa kiểm tra các tệp thiết lập môi trường của dự án này. Hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của pocket-stack/pocketjs

Tất cả issue của pocket-stack/pocketjs

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.