microts: generated Rust raises non_shorthand_field_patterns for a union variant with fields

Aperta Adatta ai principianti
#746 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
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust, typescript
Ambito
compilers, tooling

Direzione di ricerca

Inizia da pattern in microts/compiler/rust-printer.ts:68, che attualmente scrive sempre name: pattern; controlla i suoi chiamanti in microts/compiler/aot-model-codegen.ts — il match encode vicino alla riga 902 e la lettura del membro union vicino alla riga 496 — entrambi legano ogni campo a una variabile con lo stesso nome. Emetti shorthand quando il nome legato è uguale al nome del campo, incluso il caso rest .., poi riesegui nell'harness bun tools/pocket.ts compile --aot --out gen insieme a cargo check (idealmente con -D warnings) per confermare che gen/app_model.rs è privo di warning.

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

Descrizione

bug

Version

PocketJS main (2c9fcf30). 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; the cargo harness as in #725.

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

export type Flag = { kind: "off" } | { kind: "on"; value: i32 };

export const [flag, setFlag] = createSignal<Flag>({ kind: "off" });
export const [count, setCount] = createSignal<i32>(0);

export function toggle(): void {
  const current = flag();
  if (current.kind === "on") {
    setCount(current.value);
    setFlag({ kind: "off" });
  } else {
    setFlag({ kind: "on", value: count() + 1 });
  }
}
// app.tsx
import { Text, View } from "@pocketjs/framework/solid/components";
import { count, toggle } from "./app";

export default function App() {
  return (
    <View class="w-full h-full p-4 bg-slate-100" focusable onPress={() => toggle()}>
      <Text class="text-lg text-slate-900">{count()}</Text>
    </View>
  );
}
bun $POCKETJS/tools/pocket.ts compile --config pocket.host.config.ts --aot --out gen
(cd harness && cargo check)

Actual

warning: the `value:` in this pattern is redundant
  --> src/../../gen/app_model.rs:21:24
   |
21 | ...   Flag::On { value: value } => Value::Object(alloc::vec!((String::from("kind"), Value::String(String::from("on"))), (String::fro...
   |                  ^^^^^^^^^^^^ help: use shorthand field pattern: `value`
   |
   = note: `#[warn(non_shorthand_field_patterns)]` on by default
warning: the `value:` in this pattern is redundant
   --> src/../../gen/app_model.rs:110:32
    |
110 |                     Flag::On { value: value, .. } => value,
    |                                ^^^^^^^^^^^^ help: use shorthand field pattern: `value`

A crate built with -D warnings fails on a model that has a union variant with fields.

Expected

Generated code compiles without warnings: Flag::On { value } and Flag::On { value, .. }. #736 removed the warnings of the unused lint group; this lint is outside that group.

Cause

pattern in microts/compiler/rust-printer.ts:68 writes name: pattern for every field of a variant pattern that carries a pattern. The codec encoder (microts/compiler/aot-model-codegen.ts, the encode match near line 902) and the member read of a union (near line 496) bind each field to a variable of the same name.

Workaround

#![allow(non_shorthand_field_patterns)] in the crate that includes include.rs.

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.