rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

ExplicitBug crash on `m!(a. X::Y)`

Open dtolnay opened this issue 3 years ago • 10 comments

Repro against current master (c19b14539b79d44f5f433ac189d70e07fb152354):

$ echo 'm!(a. X::Y);' | RUST_BACKTRACE=1 cargo run --bin rustfmt
m!(a. X::Y);
thread 'main' panicked at 'Box<dyn Any>', compiler/rustc_errors/src/lib.rs:1425:13
stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: std::panic::panic_any::<rustc_errors::ExplicitBug>
   2: <rustc_errors::HandlerInner as core::ops::drop::Drop>::drop
   3: core::ptr::drop_in_place<rustc_errors::HandlerInner>
             at /rustc/5750a6aa2777382bf421b726f234da23f990a953/library/core/src/ptr/mod.rs:487:1
   4: core::ptr::drop_in_place<core::cell::UnsafeCell<rustc_errors::HandlerInner>>
             at /rustc/5750a6aa2777382bf421b726f234da23f990a953/library/core/src/ptr/mod.rs:487:1
   5: core::ptr::drop_in_place<core::cell::RefCell<rustc_errors::HandlerInner>>
             at /rustc/5750a6aa2777382bf421b726f234da23f990a953/library/core/src/ptr/mod.rs:487:1
   6: core::ptr::drop_in_place<rustc_data_structures::sync::Lock<rustc_errors::HandlerInner>>
             at /rustc/5750a6aa2777382bf421b726f234da23f990a953/library/core/src/ptr/mod.rs:487:1
   7: core::ptr::drop_in_place<rustc_errors::Handler>
             at /rustc/5750a6aa2777382bf421b726f234da23f990a953/library/core/src/ptr/mod.rs:487:1
   8: core::ptr::drop_in_place<rustc_session::parse::ParseSess>
             at /rustc/5750a6aa2777382bf421b726f234da23f990a953/library/core/src/ptr/mod.rs:487:1
   9: core::ptr::drop_in_place<rustfmt_nightly::parse::session::ParseSess>
             at /rustc/5750a6aa2777382bf421b726f234da23f990a953/library/core/src/ptr/mod.rs:487:1
  10: rustfmt_nightly::formatting::format_project
             at ./src/formatting.rs:175:1
  11: rustfmt_nightly::formatting::<impl rustfmt_nightly::Session<T>>::format_input_inner::{{closure}}
             at ./src/formatting.rs:48:33
  12: scoped_tls::ScopedKey<T>::with
             at /cargo/registry/src/github.com-1ecc6299db9ec823/scoped-tls-1.0.0/src/lib.rs:171:13
  13: rustc_span::create_session_if_not_set_then::{{closure}}
             at /rustc/5750a6aa2777382bf421b726f234da23f990a953/compiler/rustc_span/src/lib.rs:141:50
  14: scoped_tls::ScopedKey<T>::set
             at /cargo/registry/src/github.com-1ecc6299db9ec823/scoped-tls-1.0.0/src/lib.rs:137:9
  15: rustc_span::create_session_if_not_set_then
             at /rustc/5750a6aa2777382bf421b726f234da23f990a953/compiler/rustc_span/src/lib.rs:141:9
  16: rustfmt_nightly::formatting::<impl rustfmt_nightly::Session<T>>::format_input_inner
             at ./src/formatting.rs:38:9
  17: rustfmt_nightly::Session<T>::format
             at ./src/lib.rs:453:9
  18: rustfmt::format_and_emit_report
             at ./src/bin/main.rs:359:11
  19: rustfmt::format_string
             at ./src/bin/main.rs:281:5
  20: rustfmt::execute
             at ./src/bin/main.rs:241:39
  21: rustfmt::main
             at ./src/bin/main.rs:26:27

dtolnay avatar Jul 25 '22 01:07 dtolnay

Haven't looked too deeply into this, but the panic doesn't seem to affect formatting:

Input

m!(a. X::Y);

fn main (      ) {}

Output

m!(a. X::Y);

fn main() {}

ytmimi avatar Jul 27 '22 15:07 ytmimi

It's likely that the panic is happening somewhere in rewrite_macro_inner, but we're catching it so rustfmt doesn't crash.

https://github.com/rust-lang/rustfmt/blob/ed77962d243f93e1690ac62f0b8d733383090240/src/macros.rs#L165-L174

We might want to suppress the panic info by following the advice from this stack overflow question

ytmimi avatar Jul 27 '22 15:07 ytmimi

I don't think it's that one, because the exit code is nonzero. If the panic were caught (but still printed) the exit code would be 0.

$ echo 'm!(a. X::Y);' | rustfmt
m!(a. X::Y);
thread 'main' panicked at 'Box<dyn Any>', compiler/rustc_errors/src/lib.rs:1426:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

$ echo $?
101

dtolnay avatar Jul 27 '22 15:07 dtolnay

good point!

ytmimi avatar Jul 27 '22 15:07 ytmimi

Another example

fn main() {
    let handle = core::thread::spawn(|| {});
    assert_eq!(SHARED.fetch_addAtomicUsize::new, 1);
}

matthiaskrgr avatar Mar 30 '23 17:03 matthiaskrgr

looks like its fixed

matthiaskrgr avatar Mar 02 '24 15:03 matthiaskrgr

@matthiaskrgr Do you mean this is fixed on nightly (in rust-lang/rust) or fixed on the latest rustfmt (21f353a)?

ytmimi avatar Mar 04 '24 16:03 ytmimi

I checked the rustfmt distributed by rustup

matthiaskrgr avatar Mar 04 '24 16:03 matthiaskrgr

Thanks for clarifying. For reference, what version of rustfmt did you test?

ytmimi avatar Mar 04 '24 16:03 ytmimi

rustfmt 1.7.0-nightly (516b616 2024-03-03)

matthiaskrgr avatar Mar 04 '24 16:03 matthiaskrgr