Skip to content

Commit 6088079

Browse files
committed
use outermost invocation span for doctest names
Fixes #70090.
1 parent af88ce5 commit 6088079

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

src/librustdoc/test.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,12 @@ impl<'a, 'hir, 'tcx> HirCollector<'a, 'hir, 'tcx> {
944944
// The collapse-docs pass won't combine sugared/raw doc attributes, or included files with
945945
// anything else, this will combine them for us.
946946
if let Some(doc) = attrs.collapsed_doc_value() {
947-
self.collector.set_position(attrs.span.unwrap_or(DUMMY_SP));
947+
// Use the outermost invocation, so that doctest names come from where the docs were written.
948+
let span = attrs
949+
.span
950+
.map(|span| span.ctxt().outer_expn().expansion_cause().unwrap_or(span))
951+
.unwrap_or(DUMMY_SP);
952+
self.collector.set_position(span);
948953
markdown::find_testable_code(
949954
&doc,
950955
self.collector,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[macro_export]
2+
macro_rules! attrs_on_struct {
3+
( $( #[$attr:meta] )* ) => {
4+
$( #[$attr] )*
5+
pub struct ExpandedStruct;
6+
}
7+
}

src/test/rustdoc-ui/doctest-output.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// edition:2018
2+
// aux-build:extern_macros.rs
13
// compile-flags:--test --test-args=--test-threads=1
24
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
35
// check-pass
@@ -6,10 +8,20 @@
68
//! assert_eq!(1 + 1, 2);
79
//! ```
810
11+
extern crate extern_macros as macros;
12+
13+
use macros::attrs_on_struct;
14+
915
pub mod foo {
1016

1117
/// ```
1218
/// assert_eq!(1 + 1, 2);
1319
/// ```
1420
pub fn bar() {}
1521
}
22+
23+
attrs_on_struct! {
24+
/// ```
25+
/// assert!(true);
26+
/// ```
27+
}
+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

2-
running 2 tests
3-
test $DIR/doctest-output.rs - (line 5) ... ok
4-
test $DIR/doctest-output.rs - foo::bar (line 11) ... ok
2+
running 3 tests
3+
test $DIR/doctest-output.rs - (line 7) ... ok
4+
test $DIR/doctest-output.rs - ExpandedStruct (line 23) ... ok
5+
test $DIR/doctest-output.rs - foo::bar (line 17) ... ok
56

6-
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
7+
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
78

0 commit comments

Comments
 (0)