Skip to content

Buggy behaviour on EOF spans #465

Description

@BigWingBeat

Using a span with an offset just past the end of the source code has buggy behaviour, depending on the exact offset.
I encountered this while copy/pasting example code from the miette and diagnostic macro docs:

fn main() -> miette::Result<()> {
    let offset = 6;
    Err(miette::miette!(
        labels = vec![miette::LabeledSpan::at_offset(offset, "")],
        ""
    )
    .with_source_code("(2 + 2"))
}
  • With offset = 5 the output is:
Error:   ×
   ╭────
 1 │ (2 + 2
   ·      ▲
   ·      ╰──
   ╰────

which is expected.

  • With offset = 6 the output is:
Error:   ×
   ╭────
 1 │ (2 + 2
   ╰────

which is wrong as the label is silently hidden. This is what the aforementioned doc examples currently do.

  • With offset = 7 the output is:
Error:   ×
   ╭─
thread 'main' (942790) panicked at /home/bigwingbeat/.cargo/git/checkouts/miette-4f06557514a5b20a/df7bcfa/src/source_impls.rs:80:19:
range start index 7 out of range for slice of length 6
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Obviously it shouldn't be panicking. The backtrace is as follows:

Details
Error:   ×
   ╭─
thread 'main' (943097) panicked at /home/bigwingbeat/.cargo/git/checkouts/miette-4f06557514a5b20a/df7bcfa/src/source_impls.rs:80:19:
range start index 7 out of range for slice of length 6
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/std/src/panicking.rs:689:5
   1: core::panicking::panic_fmt
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/core/src/panicking.rs:80:14
   2: core::slice::index::slice_index_fail
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/core/src/panic.rs:177:9
   3: index<u8>
             at /home/bigwingbeat/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/slice/index.rs:443:13
   4: index<u8, core::ops::range::Range<usize>>
             at /home/bigwingbeat/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/slice/index.rs:19:15
   5: context_info
             at /home/bigwingbeat/.cargo/git/checkouts/miette-4f06557514a5b20a/df7bcfa/src/source_impls.rs:80:19
   6: read_span
             at /home/bigwingbeat/.cargo/git/checkouts/miette-4f06557514a5b20a/df7bcfa/src/source_impls.rs:102:24
   7: render_context<core::fmt::Formatter>
             at /home/bigwingbeat/.cargo/git/checkouts/miette-4f06557514a5b20a/df7bcfa/src/handlers/graphical.rs:678:18
   8: render_snippets<core::fmt::Formatter>
             at /home/bigwingbeat/.cargo/git/checkouts/miette-4f06557514a5b20a/df7bcfa/src/handlers/graphical.rs:606:18
   9: render_report_inner<core::fmt::Formatter>
             at /home/bigwingbeat/.cargo/git/checkouts/miette-4f06557514a5b20a/df7bcfa/src/handlers/graphical.rs:259:14
  10: render_report<core::fmt::Formatter>
             at /home/bigwingbeat/.cargo/git/checkouts/miette-4f06557514a5b20a/df7bcfa/src/handlers/graphical.rs:247:14
  11: debug
             at /home/bigwingbeat/.cargo/git/checkouts/miette-4f06557514a5b20a/df7bcfa/src/handlers/graphical.rs:1369:14
  12: <core::fmt::rt::Argument>::fmt
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/core/src/fmt/rt.rs:152:76
  13: core::fmt::write
  14: std::io::default_write_fmt::<std::io::stdio::StderrLock>
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/std/src/io/mod.rs:639:11
  15: <std::io::stdio::StderrLock as std::io::Write>::write_fmt
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/std/src/io/mod.rs:1994:13
  16: <&std::io::stdio::Stderr as std::io::Write>::write_fmt
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/std/src/io/stdio.rs:1066:21
  17: <std::io::stdio::Stderr as std::io::Write>::write_fmt
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/std/src/io/stdio.rs:1040:18
  18: std::io::stdio::attempt_print_to_stderr
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/std/src/io/stdio.rs:1192:22
  19: <core::result::Result<T,E> as std::process::Termination>::report
             at /home/bigwingbeat/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/process.rs:2614:17
  20: {closure#0}<core::result::Result<(), miette::eyreish::Report>>
             at /home/bigwingbeat/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:206:76
  21: <&dyn core::ops::function::Fn<(), Output = i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe as core::ops::function::FnOnce<()>>::call_once
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/core/src/ops/function.rs:287:21
  22: std::panicking::catch_unwind::do_call::<&dyn core::ops::function::Fn<(), Output = i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe, i32>
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/std/src/panicking.rs:581:40
  23: std::panicking::catch_unwind::<i32, &dyn core::ops::function::Fn<(), Output = i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe>
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/std/src/panicking.rs:544:19
  24: std::panic::catch_unwind::<&dyn core::ops::function::Fn<(), Output = i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe, i32>
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/std/src/panic.rs:359:14
  25: std::rt::lang_start_internal::{closure#0}
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/std/src/rt.rs:175:24
  26: std::panicking::catch_unwind::do_call::<std::rt::lang_start_internal::{closure#0}, isize>
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/std/src/panicking.rs:581:40
  27: std::panicking::catch_unwind::<isize, std::rt::lang_start_internal::{closure#0}>
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/std/src/panicking.rs:544:19
  28: std::panic::catch_unwind::<std::rt::lang_start_internal::{closure#0}, isize>
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/std/src/panic.rs:359:14
  29: std::rt::lang_start_internal
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/std/src/rt.rs:171:5
  30: std::rt::lang_start
             at /home/bigwingbeat/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:205:5
  31: <unknown>
  32: __libc_start_main
  33: _start
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
  • With offset = 8 the output is:
Error:   ×
  [Failed to read contents for label `` (offset: 8, length: 0): OutOfBounds]

Finally, this is what the output should be for the smaller offsets too, instead of the current buggy behaviour.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions