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

Aperta Adatta ai principianti
#756 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
80/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust, typescript
Ambito
compilers

Direzione di ricerca

Inizia da microts/compiler/aot-model-codegen.ts:1225 e leggi la lista #![allow(...)] del modulo model; confrontala con microts/compiler/aot-model-frontend.ts:728, che richiede un inizializzatore per ogni locale. Riproduci con il comando di compilazione dell'issue e cargo check nel crate harness, poi decidi se aggiungere unused_assignments alla allow list o abbassare i locali sicuramente assegnati senza inizializzatore. Fatto significa che il gen/app_model.rs generato compila con -D warnings e che i test AOT del modello esistenti continuano a passare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
TypeScript
Stelle
1.6k
Fork
88
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Non abbiamo ancora controllato i file di configurazione di questo progetto. Parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pocket-stack/pocketjs

Tutte le issue di pocket-stack/pocketjs

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.