Skip to content

Commit

Permalink
use outermost invocation span for doctest names
Browse files Browse the repository at this point in the history
Fixes #70090.
  • Loading branch information
euclio committed Jun 28, 2020
1 parent af88ce5 commit 6088079
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/librustdoc/test.rs
Expand Up @@ -944,7 +944,12 @@ impl<'a, 'hir, 'tcx> HirCollector<'a, 'hir, 'tcx> {
// The collapse-docs pass won't combine sugared/raw doc attributes, or included files with
// anything else, this will combine them for us.
if let Some(doc) = attrs.collapsed_doc_value() {
self.collector.set_position(attrs.span.unwrap_or(DUMMY_SP));
// Use the outermost invocation, so that doctest names come from where the docs were written.
let span = attrs
.span
.map(|span| span.ctxt().outer_expn().expansion_cause().unwrap_or(span))
.unwrap_or(DUMMY_SP);
self.collector.set_position(span);
markdown::find_testable_code(
&doc,
self.collector,
Expand Down
7 changes: 7 additions & 0 deletions src/test/rustdoc-ui/auxiliary/extern_macros.rs
@@ -0,0 +1,7 @@
#[macro_export]
macro_rules! attrs_on_struct {
( $( #[$attr:meta] )* ) => {
$( #[$attr] )*
pub struct ExpandedStruct;
}
}
12 changes: 12 additions & 0 deletions src/test/rustdoc-ui/doctest-output.rs
@@ -1,3 +1,5 @@
// edition:2018
// aux-build:extern_macros.rs
// compile-flags:--test --test-args=--test-threads=1
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
// check-pass
Expand All @@ -6,10 +8,20 @@
//! assert_eq!(1 + 1, 2);
//! ```

extern crate extern_macros as macros;

use macros::attrs_on_struct;

pub mod foo {

/// ```
/// assert_eq!(1 + 1, 2);
/// ```
pub fn bar() {}
}

attrs_on_struct! {
/// ```
/// assert!(true);
/// ```
}
9 changes: 5 additions & 4 deletions src/test/rustdoc-ui/doctest-output.stdout
@@ -1,7 +1,8 @@

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

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

0 comments on commit 6088079

Please sign in to comment.