Skip to content

rustdoc: use the correct span for doctests #130582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 25 is here, and this seems fine.

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
Loading