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

Open Beginner friendly
#729 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
74/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust, typescript
Domain
compilers

Research direction

Start in microts/compiler/aot-model-js.ts, at the rawExpr case "literal" and case "unary" branches, where the operator is concatenated onto the already-signed operand; the Rust side has the same shape in microts/compiler/aot-model-codegen.ts around lines 526-529. Reproduce with the two tools/pocket.ts compile commands and inspect gen/app_model.rs for the --24f64 output. Done means the guest build parses, the AOT build emits no double_negations warning, and flip() yields 24.

Written by the indexing model from the issue text.

Description

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

Dominant language
TypeScript
Stars
1.6k
Forks
88
PR merge metrics
No merged PRs in 30d

Getting set up

We have not checked this project's setup files yet. Start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pocket-stack/pocketjs

All issues in pocket-stack/pocketjs

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.