wilsonzlin / minify-js

Extremely fast JavaScript minifier, available for Rust and Node.js
Apache License 2.0
124 stars 5 forks source link

[rust panic] assertion failed: cons_expr.returns && alt_expr.returns (on if else) #26

Open Electrenator opened 6 months ago

Electrenator commented 6 months ago

The rust/src/minify/pass1.rs file panics at line 422:13 when encountering an if else block. Encountered this on version V0.6.0 while running RUST_BACKTRACE=1 minify-js-cli --mode module ifElse.js --output ifElse.small.js. Running that gives the following stack trace;

thread 'main' panicked at rust/src/minify/pass1.rs:331:13:
assertion failed: cons_expr.returns && alt_expr.returns
stack backtrace:
   0: rust_begin_unwind
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/std/src/panicking.rs:647:5
   1: core::panicking::panic_fmt
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/panicking.rs:72:14
   2: core::panicking::panic
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/panicking.rs:144:5
   3: <minify_js::minify::pass1::Pass1 as parse_js::visit::Visitor>::on_syntax_up
             at /usr/local/cargo/git/checkouts/minify-js-4f5cda0ad742d80b/d0b2fa9/rust/src/minify/pass1.rs:331:13
   4: parse_js::visit::Visitor::visit
             at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/parse-js-0.20.1/src/visit.rs:475:7
   5: parse_js::visit::Visitor::visit
             at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/parse-js-0.20.1/src/visit.rs:134:11
   6: parse_js::visit::Visitor::visit
             at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/parse-js-0.20.1/src/visit.rs:278:11
   7: parse_js::visit::Visitor::visit
             at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/parse-js-0.20.1/src/visit.rs:77:11
   8: minify_js::minify::minify_js
             at /usr/local/cargo/git/checkouts/minify-js-4f5cda0ad742d80b/d0b2fa9/rust/src/minify/mod.rs:36:3
   9: minify_js::minify
             at /usr/local/cargo/git/checkouts/minify-js-4f5cda0ad742d80b/d0b2fa9/rust/src/lib.rs:50:3
  10: minify_js_cli::main
             at /usr/local/cargo/git/checkouts/minify-js-4f5cda0ad742d80b/d0b2fa9/cli/src/main.rs:38:3
  11: core::ops::function::FnOnce::call_once
             at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

The input file for this was quite a simple JS file;

if ("a" === "a")
    console.log("a")
else if ("b" === "b")
    console.log("b")
else
    console.log("c")

It paniced becouse of the assertion at the line given within the stacktrace. Which is here in the code;

https://github.com/wilsonzlin/minify-js/blob/d0b2fa9a70abae6d80afd8e34910d932b805d2dc/rust/src/minify/pass1.rs#L331

This failed assertion also persists through to the latest main commit.

Black-Platypus commented 3 months ago

Yes, the problem also extends, of course, to use within Node.js.

thread '\<unnamed>' panicked at rust\src\minify\pass1.rs:331:13: assertion failed: cons_expr.returns && alt_expr.returns

Not that that should be surprising, but it may be nice to have that squared away for sanity checking. Also this is me adding my +1.

Electrenator commented 3 months ago

See just now that this is probably related to #21. Looks like the same problem as they have there