rustdoc: use the correct span for doctests

This commit is contained in:
Michael Howell 2024-09-19 13:26:47 -07:00
parent 749f80ab05
commit 65e432db60
5 changed files with 41 additions and 12 deletions

View File

@ -122,23 +122,14 @@ fn visit_testable<F: FnOnce(&mut Self)>(
// anything else, this will combine them for us.
let attrs = Attributes::from_ast(ast_attrs);
if let Some(doc) = attrs.opt_doc_value() {
// Use the outermost invocation, so that doctest names come from where the docs were written.
let span = ast_attrs
.iter()
.find(|attr| attr.doc_str().is_some())
.map(|attr| attr.span.ctxt().outer_expn().expansion_cause().unwrap_or(attr.span))
.unwrap_or(DUMMY_SP);
let span = span_of_fragments(&attrs.doc_strings).unwrap_or(sp);
self.collector.position = span;
markdown::find_testable_code(
&doc,
&mut self.collector,
self.codes,
self.enable_per_target_ignores,
Some(&crate::html::markdown::ExtraInfo::new(
self.tcx,
def_id,
span_of_fragments(&attrs.doc_strings).unwrap_or(sp),
)),
Some(&crate::html::markdown::ExtraInfo::new(self.tcx, def_id, span)),
);
}

View File

@ -0,0 +1,7 @@
With a code sample, that has an error:
```rust
fn main() {
let x = 234 // no semicolon here! oh no!
}
```

View File

@ -0,0 +1,7 @@
//@ compile-flags:--test --test-args=--test-threads=1
//@ normalize-stdout-test: "tests/rustdoc-ui/doctest" -> "$$DIR"
//@ normalize-stdout-test: "finished in \d+\.\d+s" -> "finished in $$TIME"
//@ failure-status: 101
// https://github.com/rust-lang/rust/issues/130470
#![doc = include_str!("doctest-output-include-fail.md")]

View File

@ -0,0 +1,24 @@
running 1 test
test $DIR/doctest-output-include-fail.md - (line 3) ... FAILED
failures:
---- $DIR/doctest-output-include-fail.md - (line 3) stdout ----
error: expected `;`, found `}`
--> $DIR/doctest-output-include-fail.md:5:16
|
LL | let x = 234 // no semicolon here! oh no!
| ^ help: add `;` here
LL | }
| - unexpected token
error: aborting due to 1 previous error
Couldn't compile the test.
failures:
$DIR/doctest-output-include-fail.md - (line 3)
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME

View File

@ -1,7 +1,7 @@
running 3 tests
test $DIR/doctest-output.rs - (line 8) ... ok
test $DIR/doctest-output.rs - ExpandedStruct (line 24) ... ok
test $DIR/doctest-output.rs - ExpandedStruct (line 25) ... ok
test $DIR/doctest-output.rs - foo::bar (line 18) ... ok
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME