Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thread 'main' panicked at 'SourceAnnotation range (58, 60) is bigger than source length 58' #5888

Closed
arifd opened this issue Aug 17, 2023 · 2 comments
Labels
bug Panic, non-idempotency, invalid code, etc. duplicate e-trailing whitespace error[internal]: left behind trailing whitespace

Comments

@arifd
Copy link

arifd commented Aug 17, 2023

this fn:

pub(crate) fn sanity_needs_ocr<T: HasWord + HasRect>(collection: &[T]) -> bool {
    let needs_ocr = collection.is_empty()
    // the presence of 'UNICODE REPLACEMENT CHARACTER'
    || collection.iter().any(|c| c.text().contains('�'))  
    // (yes, empty pages will also be OCR'd)
    || collection_is_void_of_text(collection)
    || is_gibberish(&collection.to_rendered_text());

    #[cfg(debug_assertions)]
    if needs_ocr {
        tracing::trace!(
            "sanity check failed, with text: {:?}",
            collection.to_rendered_text()
        );
    }

    needs_ocr
}

will cause this panic:

thread 'main' panicked at 'SourceAnnotation range `(58, 60)` is bigger than source length `58`', /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/annotate-snippets-0.9.1/src/display_list/from_snippet.rs:286:9
stack backtrace:
   0:        0x105726ce0 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h3fbf720fbb1cb41b
   1:        0x105775a84 - core::fmt::write::h8ab6230d6f2a4204
   2:        0x10571cdbc - std::io::Write::write_fmt::h15ecdca1916a179e
   3:        0x105726b34 - std::sys_common::backtrace::print::hb6e94bab886a9ccf
   4:        0x105729598 - std::panicking::default_hook::{{closure}}::h4a0e30e90cb1c7bb
   5:        0x1057293a0 - std::panicking::default_hook::h0bb5bbf65ac7c64d
   6:        0x10dbe299c - rustc_driver_impl[732cfd3dd2e325d3]::install_ice_hook::{closure#0}
   7:        0x105729c4c - std::panicking::rust_panic_with_hook::h17facd9c53870157
   8:        0x105729a2c - std::panicking::begin_panic_handler::{{closure}}::h9eab8195c369d860
   9:        0x1057270c0 - std::sys_common::backtrace::__rust_end_short_backtrace::hce5f67454da3493d
  10:        0x1057297a0 - _rust_begin_unwind
  11:        0x1057a0a00 - core::panicking::panic_fmt::hc7e96873bfc1c7ba
  12:        0x104dfc1e0 - annotate_snippets[df16a5f80ce3f00b]::display_list::from_snippet::format_slice
  13:        0x104df9834 - <annotate_snippets[df16a5f80ce3f00b]::display_list::structs::DisplayList as core[d635094314abd71]::convert::From<annotate_snippets[df16a5f80ce3f00b]::snippet::Snippet>>::from
  14:        0x104c9f934 - <rustfmt_nightly[8e8f5e083afe373d]::format_report_formatter::FormatReportFormatter as core[d635094314abd71]::fmt::Display>::fmt
  15:        0x105775a84 - core::fmt::write::h8ab6230d6f2a4204
  16:        0x10571b1bc - <&std::io::stdio::Stderr as std::io::Write>::write_fmt::he681787e1cd27b2e
  17:        0x10571bbc4 - std::io::stdio::_eprint::h2b97df9945de12f7
  18:        0x104c536e8 - rustfmt[5d1e7ead7f8f506]::format_and_emit_report::<std[efadba9af302a35a]::io::stdio::Stdout>
  19:        0x104c439f8 - <rustfmt_nightly[8e8f5e083afe373d]::Session<std[efadba9af302a35a]::io::stdio::Stdout>>::override_config::<rustfmt[5d1e7ead7f8f506]::format::{closure#0}, ()>
  20:        0x104c52880 - rustfmt[5d1e7ead7f8f506]::execute
  21:        0x104c51354 - rustfmt[5d1e7ead7f8f506]::main
  22:        0x104c46c08 - std[efadba9af302a35a]::sys_common::backtrace::__rust_begin_short_backtrace::<fn(), ()>
  23:        0x104c46c20 - std[efadba9af302a35a]::rt::lang_start::<()>::{closure#0}
  24:        0x10570fdf8 - std::rt::lang_start_internal::h434fe84e11a92429
  25:        0x104c54130 - _main

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rustfmt/issues/new?labels=bug

note: rustc 1.71.0 (8ede3aae2 2023-07-12) running on aarch64-apple-darwin

query stack during panic:
end of query stack

to fix the panic:

pub(crate) fn sanity_needs_ocr<T: HasWord + HasRect>(collection: &[T]) -> bool {
     let needs_ocr = collection.is_empty()
     // the presence of 'UNICODE REPLACEMENT CHARACTER'
-    || collection.iter().any(|c| c.text().contains('�'))  
+    || collection.iter().any(|c| c.text().contains('�'))
     // (yes, empty pages will also be OCR'd)
     || collection_is_void_of_text(collection)
     || is_gibberish(&collection.to_rendered_text());
@ytmimi
Copy link
Contributor

ytmimi commented Aug 17, 2023

This is almost certainly related / a duplicate of #5876. The code snippet here is nice because it helps reproduce the issue without a let-chain.

@ytmimi ytmimi added bug Panic, non-idempotency, invalid code, etc. duplicate e-trailing whitespace error[internal]: left behind trailing whitespace labels Aug 17, 2023
@ytmimi
Copy link
Contributor

ytmimi commented Aug 17, 2023

Here's some additional info about what's happening here:

In #5876 (comment) I go into a little detail about the issue as it relates to #5876.

In your example, rustfmt fails to format the binary expression because we don't handle comments inside binary expressions #3591.

Generally, when rustfmt can't format an AST node it outputs the entire content of that AST node verbatim into the formatted output. In this case that means including the trailing whitespace. As I explain in #5876 (comment), there's some post formatting work that rustfmt does to double check that formatting succeeded (are lines formatted within max_width, is all trailing whitespace removed, etc.), and if something went wrong rustfmt attempts to output a message to the user.

The bug occurs during this error reporting phase, and it's the unicode char '�' that's throwing rustfmt off since we aren't handling the size of the unicode char properly.

You're correct that removing the trailing space prevents this issue because it prevents code execution from going down the error reporting code path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Panic, non-idempotency, invalid code, etc. duplicate e-trailing whitespace error[internal]: left behind trailing whitespace
Projects
None yet
Development

No branches or pull requests

2 participants