From 65e432db60bde00f15991a7cbdfffbdca2d4038b Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 19 Sep 2024 13:26:47 -0700 Subject: [PATCH] rustdoc: use the correct span for doctests --- src/librustdoc/doctest/rust.rs | 13 ++-------- .../doctest/doctest-output-include-fail.md | 7 ++++++ .../doctest/doctest-output-include-fail.rs | 7 ++++++ .../doctest-output-include-fail.stdout | 24 +++++++++++++++++++ .../rustdoc-ui/doctest/doctest-output.stdout | 2 +- 5 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 tests/rustdoc-ui/doctest/doctest-output-include-fail.md create mode 100644 tests/rustdoc-ui/doctest/doctest-output-include-fail.rs create mode 100644 tests/rustdoc-ui/doctest/doctest-output-include-fail.stdout diff --git a/src/librustdoc/doctest/rust.rs b/src/librustdoc/doctest/rust.rs index 5c0898f28fc..71a0e3d72de 100644 --- a/src/librustdoc/doctest/rust.rs +++ b/src/librustdoc/doctest/rust.rs @@ -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)), ); } diff --git a/tests/rustdoc-ui/doctest/doctest-output-include-fail.md b/tests/rustdoc-ui/doctest/doctest-output-include-fail.md new file mode 100644 index 00000000000..a8e61238f31 --- /dev/null +++ b/tests/rustdoc-ui/doctest/doctest-output-include-fail.md @@ -0,0 +1,7 @@ +With a code sample, that has an error: + +```rust +fn main() { + let x = 234 // no semicolon here! oh no! +} +``` diff --git a/tests/rustdoc-ui/doctest/doctest-output-include-fail.rs b/tests/rustdoc-ui/doctest/doctest-output-include-fail.rs new file mode 100644 index 00000000000..4fc0674a0c9 --- /dev/null +++ b/tests/rustdoc-ui/doctest/doctest-output-include-fail.rs @@ -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")] diff --git a/tests/rustdoc-ui/doctest/doctest-output-include-fail.stdout b/tests/rustdoc-ui/doctest/doctest-output-include-fail.stdout new file mode 100644 index 00000000000..22d15f8743c --- /dev/null +++ b/tests/rustdoc-ui/doctest/doctest-output-include-fail.stdout @@ -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 + diff --git a/tests/rustdoc-ui/doctest/doctest-output.stdout b/tests/rustdoc-ui/doctest/doctest-output.stdout index 35b0e366fb5..c3b1570c43e 100644 --- a/tests/rustdoc-ui/doctest/doctest-output.stdout +++ b/tests/rustdoc-ui/doctest/doctest-output.stdout @@ -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