Skip to content

Commit ee4afa3

Browse files
authored
Rollup merge of #130582 - notriddle:notriddle/doctest-span, r=GuillaumeGomez
rustdoc: use the correct span for doctests Fixes #130470
2 parents ee2bec0 + 65e432d commit ee4afa3

File tree

5 files changed

+41
-12
lines changed

5 files changed

+41
-12
lines changed

src/librustdoc/doctest/rust.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,14 @@ impl<'a, 'tcx> HirCollector<'a, 'tcx> {
122122
// anything else, this will combine them for us.
123123
let attrs = Attributes::from_ast(ast_attrs);
124124
if let Some(doc) = attrs.opt_doc_value() {
125-
// Use the outermost invocation, so that doctest names come from where the docs were written.
126-
let span = ast_attrs
127-
.iter()
128-
.find(|attr| attr.doc_str().is_some())
129-
.map(|attr| attr.span.ctxt().outer_expn().expansion_cause().unwrap_or(attr.span))
130-
.unwrap_or(DUMMY_SP);
125+
let span = span_of_fragments(&attrs.doc_strings).unwrap_or(sp);
131126
self.collector.position = span;
132127
markdown::find_testable_code(
133128
&doc,
134129
&mut self.collector,
135130
self.codes,
136131
self.enable_per_target_ignores,
137-
Some(&crate::html::markdown::ExtraInfo::new(
138-
self.tcx,
139-
def_id,
140-
span_of_fragments(&attrs.doc_strings).unwrap_or(sp),
141-
)),
132+
Some(&crate::html::markdown::ExtraInfo::new(self.tcx, def_id, span)),
142133
);
143134
}
144135

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
With a code sample, that has an error:
2+
3+
```rust
4+
fn main() {
5+
let x = 234 // no semicolon here! oh no!
6+
}
7+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//@ compile-flags:--test --test-args=--test-threads=1
2+
//@ normalize-stdout-test: "tests/rustdoc-ui/doctest" -> "$$DIR"
3+
//@ normalize-stdout-test: "finished in \d+\.\d+s" -> "finished in $$TIME"
4+
//@ failure-status: 101
5+
6+
// https://github.com/rust-lang/rust/issues/130470
7+
#![doc = include_str!("doctest-output-include-fail.md")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
running 1 test
3+
test $DIR/doctest-output-include-fail.md - (line 3) ... FAILED
4+
5+
failures:
6+
7+
---- $DIR/doctest-output-include-fail.md - (line 3) stdout ----
8+
error: expected `;`, found `}`
9+
--> $DIR/doctest-output-include-fail.md:5:16
10+
|
11+
LL | let x = 234 // no semicolon here! oh no!
12+
| ^ help: add `;` here
13+
LL | }
14+
| - unexpected token
15+
16+
error: aborting due to 1 previous error
17+
18+
Couldn't compile the test.
19+
20+
failures:
21+
$DIR/doctest-output-include-fail.md - (line 3)
22+
23+
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
24+

tests/rustdoc-ui/doctest/doctest-output.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
running 3 tests
33
test $DIR/doctest-output.rs - (line 8) ... ok
4-
test $DIR/doctest-output.rs - ExpandedStruct (line 24) ... ok
4+
test $DIR/doctest-output.rs - ExpandedStruct (line 25) ... ok
55
test $DIR/doctest-output.rs - foo::bar (line 18) ... ok
66

77
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME

0 commit comments

Comments
 (0)