microts: negating a negative module constant emits `--` and breaks the guest build of a compiled model

Đang mở Phù hợp với người mới
#729 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ó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
74/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ệ
rust, typescript
Lĩnh vực
compilers

Hướng nghiên cứu

Bắt đầu trong microts/compiler/aot-model-js.ts, tại các nhánh rawExpr case "literal" và case "unary", nơi toán tử được nối vào toán hạng đã có dấu; phía Rust có cùng hình dạng trong microts/compiler/aot-model-codegen.ts quanh các dòng 526-529. Tái hiện bằng hai lệnh tools/pocket.ts compile và kiểm tra gen/app_model.rs để xem đầu ra --24f64. Hoàn tất nghĩa là bản build guest phân tích cú pháp, bản build AOT không phát ra cảnh báo double_negations, và flip() trả về 24.

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, 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 { f64 } from "@pocketjs/framework/solid/std";

const TILT: f64 = -24;

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

export function flip(): void {
  setValue(-TILT);
}
// app.tsx
import { Text, View } from "@pocketjs/framework/solid/components";
import { flip, value } from "./app";

export default function App() {
  return (
    <View class="w-full h-full p-4 bg-slate-100" focusable onPress={() => flip()}>
      <Text class="text-lg text-slate-900">{value()}</Text>
    </View>
  );
}
bun $POCKETJS/tools/pocket.ts compile --config pocket.host.config.ts --output out/app.pkb
bun $POCKETJS/tools/pocket.ts compile --config pocket.host.config.ts --aot --out gen

Actual

The guest build stops:

SyntaxError: app.ts: Invalid left-hand side in prefix operation. (9:55)
  code: "BABEL_PARSE_ERROR"

The AOT build succeeds; gen/app_model.rs contains let write_0 = --24f64;, which rustc compiles as a double negation and reports:

warning: use of a double negation
  = note: `#[warn(double_negations)]` on by default
Constant and use Guest build
const TILT: f64 = -24; and -TILT Invalid left-hand side in prefix operation
const TILT: f64 = -24; and -(TILT) same error
const TILT = -24; and -TILT same error
const TILT: f64 = 24; and -TILT builds
const TILT: f64 = -24; and 1 - TILT builds

Expected

Both builds compile, flip sets the value to 24, and the generated Rust has no warning.

Cause

The constant is inlined as a literal, and a negative literal is written with its sign: rawExpr case "literal" (microts/compiler/aot-model-js.ts:64) returns JSON.stringify(-24), that is -24. case "unary" (aot-model-js.ts:73) concatenates the operator and the operand without a separator, (${e.operator}${expr(e.operand)}), giving (--24), a prefix decrement of a literal. A -0 literal is written as -0 and meets the same path. The Rust generator builds a unary node over the same literal (microts/compiler/aot-model-codegen.ts:526–529), which prints as --24f64.

A space or parentheses around the operand ((- -24), -(-24f64)) fixes both outputs.

Workaround

Subtract from zero: setValue(0 - TILT).

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.