Skip to content

Doctest panic line numbers are incorrect #73469

Open

Description

@enterprisey

I tried this code:

/// ```
/// panic!()
/// ```
fn f() {}

I expected to see this happen: either

  • thread 'main' panicked at 'explicit panic', src/lib.rs:2:1, the actual line of the panic
  • thread 'main' panicked at 'explicit panic', src/lib.rs:1:1, the line of the panic inside the doctest

Instead, this happened: thread 'main' panicked at 'explicit panic', src/lib.rs:3:1. Why line 3? (I suspect it's the implicit code insertion, but it would sure be nice if the line numbers were corrected.)

Meta

rustc --version --verbose:

rustc 1.44.0 (49cae5576 2020-06-01)
binary: rustc
commit-hash: 49cae55760da0a43428eba73abcb659bb70cf2e4
commit-date: 2020-06-01
host: x86_64-unknown-linux-gnu
release: 1.44.0
LLVM version: 9.0
Backtrace

thread 'main' panicked at 'explicit panic', src/lib.rs:3:1
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/hub.lumenfield.work-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/libunwind.rs:86
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/hub.lumenfield.work-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:78
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:59
   4: core::fmt::write
             at src/libcore/fmt/mod.rs:1069
   5: std::io::Write::write_fmt
             at src/libstd/io/mod.rs:1504
   6: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:62
   7: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:49
   8: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:198
   9: std::panicking::default_hook
             at src/libstd/panicking.rs:218
  10: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:511
  11: std::panicking::begin_panic
  12: rust_out::main
  13: std::rt::lang_start::{{closure}}
  14: std::rt::lang_start_internal::{{closure}}
             at src/libstd/rt.rs:52
  15: std::panicking::try::do_call
             at src/libstd/panicking.rs:331
  16: std::panicking::try
             at src/libstd/panicking.rs:274
  17: std::panic::catch_unwind
             at src/libstd/panic.rs:394
  18: std::rt::lang_start_internal
             at src/libstd/rt.rs:51
  19: std::rt::lang_start
  20: main
  21: __libc_start_main
  22: _start

Activity

  1. added
    C-bug
    on Jun 18, 2020
  2. added
    A-doctests
    T-rustdoc
    on Jun 18, 2020
  3. lolbinarycat commented on Sep 14, 2024

    @lolbinarycat
    Contributor

    this would require adding a #[line] or similar attribute, which i believe has been rejected before.

  4. cscott commented on Oct 29, 2026

    @cscott

    This still happens in 1.96.1, and it affects line!() as well as panic locations. That matters to snapshot-testing crates: insta and expect-test use file!()/line!() to find the assertion they rewrite. Both have given up on doctests (mitsuhiko/insta#272, #339), and expect-test's UPDATE_EXPECT=1 looks at the wrong line of src/lib.rs.

    Since #164849 just made rustdoc's own doctest line numbers reliable (thanks to @Urgau, @GuillaumeGomez), maybe it's a good time to look at this one again.

    I don't think it needs a #[line] attribute. rustc already knows the offset: rustdoc passes it in UNSTABLE_RUSTDOC_TEST_LINE and the file name becomes FileName::DocTest(path, offset). Diagnostics apply it with SourceMap::doctest_offset_line, so a compile error in a doctest points at the right line of src/lib.rs. file!() uses the mapped name too. But expand_line (line!()) and TyCtxt::span_as_caller_location (panics, #[track_caller]) take lookup_char_pos(..).line as is. So file!() and line!() together name a line of src/lib.rs that is off by however much the doctest is from the top of the file.

    Repro (rustc 1.96.1):

    //! ```
    //! println!("{}:{}", file!(), line!()); // src/lib.rs:2
    //! panic!(); // src/lib.rs:3
    //! ```
    //!
    //! ```standalone_crate
    //! println!("{}:{}", file!(), line!()); // src/lib.rs:7
    //! panic!(); // src/lib.rs:8
    //! ```
    

    edition = "2021", cargo test --doc:

    src/lib.rs:3
    thread 'main' panicked at src/lib.rs:4:1:
    src/lib.rs:3
    thread 'main' panicked at src/lib.rs:4:1:
    

    Both doctests report the same lines (snippet line plus the 2 lines of #![allow(unused)] / fn main() { preamble) wherever they are in the file. A compile error on line 6 of the same file is reported as src/lib.rs:6:14.

    edition = "2024": the standalone_crate doctest is the same as above. The merged one reports the temporary bundle:

    $TMPDIR/rustdoctestmECxZv/doctest_bundle_2024.rs:6
    thread 'main' panicked at $TMPDIR/rustdoctestmECxZv/doctest_bundle_2024.rs:7:1:
    

    So under 2024 even file!() no longer names the source file. (insta also used to detect doctests by the rust_out::main::_doctest function name, and lost its "can't update inline snapshots in doctests" warning as a result; that part is mitsuhiko/insta#956.)

    Would a PR that applies doctest_offset_line in expand_line and span_as_caller_location, for standalone doctests, be welcome? Merged doctests look harder: does the bundle keep enough span information to map each __doctest_N back to its fence, now that #164849 records it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-doctestsC-bugT-rustdoc

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions