Repository navigation
Open
Labels
A-doctestsArea: Documentation tests, run by rustdocC-bugCategory: This is a bug.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.
Description
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 panicthread '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
Reacted by jade, Askar Safin, Jacob Hoffman-Andrews, Ben Kimock, Moritz Hedtke, Sludge and Peter Lyons Kehl
Metadata
Metadata
Assignees
Labels
A-doctestsArea: Documentation tests, run by rustdocC-bugCategory: This is a bug.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.
Type
Projects
- StatusShow more project fieldsNo status
Activity
lolbinarycat commentedon Sep 14, 2024
this would require adding a
#[line]or similar attribute, which i believe has been rejected before.cscott commentedon Oct 29, 2026
This still happens in 1.96.1, and it affects
line!()as well as panic locations. That matters to snapshot-testing crates:instaandexpect-testusefile!()/line!()to find the assertion they rewrite. Both have given up on doctests (mitsuhiko/insta#272, #339), andexpect-test'sUPDATE_EXPECT=1looks at the wrong line ofsrc/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 inUNSTABLE_RUSTDOC_TEST_LINEand the file name becomesFileName::DocTest(path, offset). Diagnostics apply it withSourceMap::doctest_offset_line, so a compile error in a doctest points at the right line ofsrc/lib.rs.file!()uses the mapped name too. Butexpand_line(line!()) andTyCtxt::span_as_caller_location(panics,#[track_caller]) takelookup_char_pos(..).lineas is. Sofile!()andline!()together name a line ofsrc/lib.rsthat is off by however much the doctest is from the top of the file.Repro (rustc 1.96.1):
edition = "2021",cargo test --doc: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 assrc/lib.rs:6:14.edition = "2024": thestandalone_cratedoctest is the same as above. The merged one reports the temporary bundle:So under 2024 even
file!()no longer names the source file. (insta also used to detect doctests by therust_out::main::_doctestfunction 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_lineinexpand_lineandspan_as_caller_location, for standalone doctests, be welcome? Merged doctests look harder: does the bundle keep enough span information to map each__doctest_Nback to its fence, now that #164849 records it?