Skip to content

Panic in parse_{let|mut|const} with colon and opposed parens/braces #16284

@sholderbach

Description

@sholderbach

Describe the bug

The parsing of binding declarations (let/mut/const) fails under the following circumstances:

  1. Binding keyword
  2. No binding name
  3. : for type specification
  4. No type syntax
  5. opposed pairing of either parentheses or braces (i.e. having block/subexpression properties) Does not trigger for ][.

Found via fuzzing of the parse_with_keywords target :)

How to reproduce

Paste:

let : )(

Observe how your face resembles : )(

× Main thread panicked.
  ├─▶ at crates/nu-parser/src/parser.rs:3509:65
  ╰─▶ index out of bounds: the len is 0 but the index is 0
         0: 0x584c2d85dd2d - rust_begin_unwind
                      at /rustc/05f9846f893b09a1be1fc8560e33fc3c815cfecb/library/std/src/panicking.rs:695
         1: 0x584c2d88d0b0 - core::panicking::panic_fmt::h5764ee7030b7a73d
                      at /rustc/05f9846f893b09a1be1fc8560e33fc3c815cfecb/library/core/src/panicking.rs:75
         2: 0x584c2d88d292 - core::panicking::panic_bounds_check::h0328ca7e7f0749c4
                      at /rustc/05f9846f893b09a1be1fc8560e33fc3c815cfecb/library/core/src/panicking.rs:273
         3: 0x584c2c092797 - <usize as core::slice::index::SliceIndex<[T]>>::index::hd1dd51ab3912034f
                      at /home/stefan/.rustup/toolchains/1.86.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/slice/index.rs:274
         4: 0x584c2c08dad1 - core::slice::index::<impl core::ops::index::Index<I> for [T]>::index::hee57d0016dde1f6a
                      at /home/stefan/.rustup/toolchains/1.86.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/slice/index.rs:16
                       - <alloc::vec::Vec<T,A> as core::ops::index::Index<I>>::index::h3891ab36bc43f826
                      at /home/stefan/.rustup/toolchains/1.86.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:3358
         5: 0x584c2c0c15e6 - nu_parser::parser::parse_var_with_opt_type::hff03f0ecd9f171f0
                      at /home/stefan/nushell/crates/nu-parser/src/parser.rs:3509
         6: 0x584c2c0a4e4f - nu_parser::parser::parse_multispan_value::h5c038c4df499c85e
                      at /home/stefan/nushell/crates/nu-parser/src/parser.rs:850
         7: 0x584c2c0a8ad6 - nu_parser::parser::parse_internal_call::h5a360865c8577ae1
                      at /home/stefan/nushell/crates/nu-parser/src/parser.rs:1234
         8: 0x584c2c07662a - nu_parser::parse_keywords::parse_let::h76b90db079365a21
                      at /home/stefan/nushell/crates/nu-parser/src/parse_keywords.rs:3402
         9: 0x584c2c0df2bf - nu_parser::parser::parse_builtin_commands::hd8391f8e01d382ba
                      at /home/stefan/nushell/crates/nu-parser/src/parser.rs:6058
        10: 0x584c2c0e3634 - nu_parser::parser::parse_pipeline::h0e161b60bfe8cf0e
                      at /home/stefan/nushell/crates/nu-parser/src/parser.rs:6493
        11: 0x584c2c0e3ca9 - nu_parser::parser::parse_block::ha0e7cfb95b101889
                      at /home/stefan/nushell/crates/nu-parser/src/parser.rs:6527
        12: 0x584c2c0e9636 - nu_parser::parser::parse::he6929809bcf933aa
                      at /home/stefan/nushell/crates/nu-parser/src/parser.rs:7083
        13: 0x584c2bc5968e - nu_cli::syntax_highlight::highlight_syntax::hc782dec78cb8cdef
                      at /home/stefan/nushell/crates/nu-cli/src/syntax_highlight.rs:46
        14: 0x584c2bc5933c - <nu_cli::syntax_highlight::NuHighlighter as reedline::highlighter::Highlighter>::highlight::h17a00a5676e86896
                      at /home/stefan/nushell/crates/nu-cli/src/syntax_highlight.rs:21
        15: 0x584c2be489da - reedline::engine::Reedline::buffer_paint::h646fba33d9221b78
                      at /home/stefan/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/reedline-0.41.0/src/engine.rs:1756
        16: 0x584c2be45afb - reedline::engine::Reedline::repaint::h9d096876b213bae7
                      at /home/stefan/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/reedline-0.41.0/src/engine.rs:1521
        17: 0x584c2be3ed8c - reedline::engine::Reedline::read_line_helper::hc16b34bbd176d9a2
                      at /home/stefan/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/reedline-0.41.0/src/engine.rs:814
        18: 0x584c2be3e0dd - reedline::engine::Reedline::read_line::hf1614ad5e2ac10a1
                      at /home/stefan/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/reedline-0.41.0/src/engine.rs:650
        19: 0x584c2bbe2039 - nu_cli::repl::loop_iteration::hc3050af2552c6bf2
                      at /home/stefan/nushell/crates/nu-cli/src/repl.rs:487
        20: 0x584c2bc63e12 - nu_cli::repl::evaluate_repl::{{closure}}::h64418602f50e8d3e
                      at /home/stefan/nushell/crates/nu-cli/src/repl.rs:189
        21: 0x584c2bbd48ff - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::h58f31e1038de5307
                      at /home/stefan/.rustup/toolchains/1.86.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panic/unwind_safe.rs:272
        22: 0x584c2bcd2d28 - std::panicking::try::do_call::h2a6de2c01561b44f
                      at /home/stefan/.rustup/toolchains/1.86.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:587
        23: 0x584c2bca149b - __rust_try
        24: 0x584c2bca13de - std::panicking::try::h017172b414b0dc7b
                      at /home/stefan/.rustup/toolchains/1.86.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:550
                       - std::panic::catch_unwind::he09f0d04db080a36
                      at /home/stefan/.rustup/toolchains/1.86.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:358
        25: 0x584c2bbdba27 - nu_cli::repl::evaluate_repl::hfd4a7fe1f33915fc
                      at /home/stefan/nushell/crates/nu-cli/src/repl.rs:188
        26: 0x584c29e6de89 - nu::run::run_repl::h2609748962168306
                      at /home/stefan/nushell/src/run.rs:207
        27: 0x584c29e9ec70 - nu::main::h4e6b99472a8e8fb7
                      at /home/stefan/nushell/src/main.rs:535
        28: 0x584c29e8da9b - core::ops::function::FnOnce::call_once::h35f768ee557759f3
                      at /home/stefan/.rustup/toolchains/1.86.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250

Alternates:

let : }{
mut : )(
mut : }{
const : )(
const : }{

Expected behavior

No panic, boring syntax errors

Configuration

key value
version 0.106.1
major 0
minor 106
patch 1
branch main
commit_hash c921ead
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.86.0 (05f9846f8 2025-03-31)
rust_channel 1.86.0-x86_64-unknown-linux-gnu
cargo_version cargo 1.86.0 (adf9b6ad1 2025-02-28)
build_time 2025-07-28 22:25:47 +02:00
build_rust_channel debug
allocator standard
features default, plugin, rustls-tls, sqlite, trash-support
installed_plugins plotters 0.1.7+0.105.1
experimental_options example=false, reorder-cell-paths=false

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions