Skip to content

Commit

Permalink
Rollup merge of #130582 - notriddle:notriddle/doctest-span, r=Guillau…
Browse files Browse the repository at this point in the history
…meGomez

rustdoc: use the correct span for doctests

Fixes #130470
  • Loading branch information
matthiaskrgr committed Sep 20, 2024
2 parents ee2bec0 + 65e432d commit ee4afa3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
13 changes: 2 additions & 11 deletions src/librustdoc/doctest/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,14 @@ impl<'a, 'tcx> HirCollector<'a, 'tcx> {
// 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)),
);
}

Expand Down
7 changes: 7 additions & 0 deletions tests/rustdoc-ui/doctest/doctest-output-include-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
With a code sample, that has an error:

```rust
fn main() {
let x = 234 // no semicolon here! oh no!
}
```
7 changes: 7 additions & 0 deletions tests/rustdoc-ui/doctest/doctest-output-include-fail.rs
Original file line number Diff line number Diff line change
@@ -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")]
24 changes: 24 additions & 0 deletions tests/rustdoc-ui/doctest/doctest-output-include-fail.stdout
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion tests/rustdoc-ui/doctest/doctest-output.stdout
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit ee4afa3

Please sign in to comment.