microts: a model local that every branch assigns raises unused_assignments in the generated Rust

未关闭 适合新手
#756 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
80/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust, typescript
领域
compilers

调研方向

从 microts/compiler/aot-model-codegen.ts:1225 开始,阅读 model 模块的 #![allow(...)] 列表;将其与 microts/compiler/aot-model-frontend.ts:728 比较,后者要求每个 local 都有 initializer。用 issue 中的编译命令和 harness crate 中的 cargo check 复现,然后决定是将 unused_assignments 加入 allow 列表,还是 lower 没有 initializer 的 definitely assigned local。完成意味着生成的 gen/app_model.rs 在 -D warnings 下编译通过,且现有的 AOT model 测试仍然通过。

由索引模型根据 Issue 内容生成。

描述

bug

Version

PocketJS main (2c9fcf30), after #736. macOS 27.2 arm64, Bun 1.4.2, Rust 1.98.1.

Repro

pocket.host.json, pocket.host.config.ts (aot: true, model: "compiled") and main.tsx as in #720.

// app.ts
import { createSignal } from "solid-js";
import type { i32 } from "@pocketjs/framework/solid/std";

export const [value, setValue] = createSignal<i32>(0);

export function step(dir: i32): void {
  const cell = value();
  let next = cell;
  if (dir === 0) {
    next = cell - 1;
  } else {
    next = cell + 1;
  }
  setValue(next);
}
// 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(1)}>
      <Text class="text-lg text-slate-900">{value()}</Text>
    </View>
  );
}

The harness crate of #725 (include!("../../gen/include.rs") in a no_std lib depending on microts):

bun $POCKETJS/tools/pocket.ts compile --config pocket.host.config.ts --aot --out gen
(cd harness && cargo check)

Actual

warning: value assigned to `v_5` is never read
  --> src/../../gen/app_model.rs:64:32
   |
64 |               let mut v_5: i32 = v_4;
   |                                  ^^^ this value is reassigned later and never used
...
76 | /                 v_5 = {
77 | |                     let left_2 = v_4;
78 | |                     let right_2 = 1i32;
79 | |                     left_2.wrapping_add(right_2)
80 | |                 };
   | |_________________- `v_5` is overwritten here before the previous value is read

A crate built with -D warnings fails on it.

step body Warnings
let next = cell; and an if/else that assigns next in both branches 1
the same with an if and no else (the initial value is read on one path) 0
let next: i32; and the same if/else rejected: locals require a simple binder and initializer

An if/else if/else chain that assigns the local in every branch gives the same warning.

Expected

The generated model compiles without warnings, as #736 established for the other cases.

Cause

The frontend requires every model local to have an initializer (microts/compiler/aot-model-frontend.ts:728), so a local that every branch assigns carries an initial value that no path reads, and the generator emits it as let mut v_N: T = <initializer>;. rustc's liveness analysis reports the dead store. The model module's #![allow(...)] list (microts/compiler/aot-model-codegen.ts:1225) holds unused_mut and unused_variables but not unused_assignments.

Either change removes the warning:

  • add unused_assignments to the model module's allow list: the dead store is the source program's, as unused_mut and unused_variables already cover source-level patterns;
  • or admit let x: T; when every path assigns it before a read (TypeScript's definite assignment) and lower it without an initializer.

Workaround

#![allow(unused_assignments)] in the crate that includes include.rs; it also hides the lint for the crate's own code. In the source, a conditional expression in the initializer avoids the dead store where the branches have no other statements.

Related

#725 (closed by #736): the pop/removeAt result declared with a default the assignment overwrote.

主要语言
TypeScript
星标
1.6k
派生
88
PR 合并指标
30 天内没有已合并 PR

环境准备

我们还没有检查这个项目的环境配置文件。先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

pocket-stack/pocketjs 的其他 Issue

查看 pocket-stack/pocketjs 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。