Skip to content

Commit 62ba25d

Browse files
Rollup merge of #132210 - notriddle:notriddle/doctest-span-hack, r=GuillaumeGomez
rustdoc: make doctest span tweak a 2024 edition change Fixes #132203 This is a compatibility hack, because I think the new behavior is better. When an A `include_str!` B, and B `include_str!` C, the path to C should be resolved relative to B, not A. That's how `include!` itself works, so that's how `include_str!` with should work.
2 parents 6b60f03 + ac7de1a commit 62ba25d

12 files changed

+99
-13
lines changed

src/librustdoc/doctest/rust.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,19 @@ impl<'tcx> HirCollector<'tcx> {
113113
let attrs = Attributes::from_ast(ast_attrs);
114114
if let Some(doc) = attrs.opt_doc_value() {
115115
let span = span_of_fragments(&attrs.doc_strings).unwrap_or(sp);
116-
self.collector.position = span;
116+
self.collector.position = if span.edition().at_least_rust_2024() {
117+
span
118+
} else {
119+
// this span affects filesystem path resolution,
120+
// so we need to keep it the same as it was previously
121+
ast_attrs
122+
.iter()
123+
.find(|attr| attr.doc_str().is_some())
124+
.map(|attr| {
125+
attr.span.ctxt().outer_expn().expansion_cause().unwrap_or(attr.span)
126+
})
127+
.unwrap_or(DUMMY_SP)
128+
};
117129
markdown::find_testable_code(
118130
&doc,
119131
&mut self.collector,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ edition:2024
2+
//@ compile-flags:-Z unstable-options
3+
#![crate_name="extern_macros"]
4+
#[macro_export]
5+
macro_rules! attrs_on_struct {
6+
( $( #[$attr:meta] )* ) => {
7+
$( #[$attr] )*
8+
pub struct ExpandedStruct;
9+
}
10+
}

tests/rustdoc-ui/doctest/auxiliary/relative-dir-empty-file

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```rust
2+
let x = include_bytes!("relative-dir-empty-file");
3+
```

tests/rustdoc-ui/doctest/doctest-output-include-fail.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//@ compile-flags:--test --test-args=--test-threads=1
1+
//@ edition:2024
2+
//@ compile-flags:--test --test-args=--test-threads=1 -Z unstable-options
23
//@ normalize-stdout-test: "tests/rustdoc-ui/doctest" -> "$$DIR"
34
//@ normalize-stdout-test: "finished in \d+\.\d+s" -> "finished in $$TIME"
45
//@ failure-status: 101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
running 3 tests
3+
test $DIR/doctest-output.rs - (line 12) ... ok
4+
test $DIR/doctest-output.rs - ExpandedStruct (line 28) ... ok
5+
test $DIR/doctest-output.rs - foo::bar (line 22) ... ok
6+
7+
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
running 3 tests
3+
test $DIR/doctest-output.rs - (line 12) ... ok
4+
test $DIR/doctest-output.rs - ExpandedStruct (line 28) ... ok
5+
test $DIR/doctest-output.rs - foo::bar (line 22) ... ok
6+
7+
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
8+

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
//@ edition:2018
2-
//@ aux-build:extern_macros.rs
3-
//@ compile-flags:--test --test-args=--test-threads=1
1+
//@ revisions: edition2015 edition2024
2+
//@[edition2015]edition:2015
3+
//@[edition2015]aux-build:extern_macros.rs
4+
//@[edition2015]compile-flags:--test --test-args=--test-threads=1
5+
//@[edition2024]edition:2015
6+
//@[edition2024]aux-build:extern_macros.rs
7+
//@[edition2024]compile-flags:--test --test-args=--test-threads=1 -Z unstable-options
48
//@ normalize-stdout-test: "tests/rustdoc-ui/doctest" -> "$$DIR"
59
//@ normalize-stdout-test: "finished in \d+\.\d+s" -> "finished in $$TIME"
610
//@ check-pass

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

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
running 1 test
3+
test $DIR/relative-path-include-bytes-132203.rs - (line 18) ... FAILED
4+
5+
failures:
6+
7+
---- $DIR/relative-path-include-bytes-132203.rs - (line 18) stdout ----
8+
error: couldn't read `$DIR/relative-dir-empty-file`: No such file or directory (os error 2)
9+
--> $DIR/relative-path-include-bytes-132203.rs:19:9
10+
|
11+
LL | let x = include_bytes!("relative-dir-empty-file");
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
|
14+
= note: this error originates in the macro `include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info)
15+
16+
error: aborting due to 1 previous error
17+
18+
Couldn't compile the test.
19+
20+
failures:
21+
$DIR/relative-path-include-bytes-132203.rs - (line 18)
22+
23+
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
running 1 test
3+
test $DIR/auxiliary/relative-dir.md - (line 1) ... ok
4+
5+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ ignore-windows
2+
//@ revisions: edition2015 edition2024
3+
//@[edition2015]edition:2015
4+
//@[edition2015]check-fail
5+
//@[edition2015]failure-status: 101
6+
//@[edition2015]compile-flags:--test --test-args=--test-threads=1
7+
//@[edition2024]edition:2024
8+
//@[edition2024]check-pass
9+
//@[edition2024]compile-flags:--test --test-args=--test-threads=1 -Z unstable-options
10+
//@ normalize-stdout-test: "tests/rustdoc-ui/doctest" -> "$$DIR"
11+
//@ normalize-stdout-test: "finished in \d+\.\d+s" -> "finished in $$TIME"
12+
13+
// https://github.com/rust-lang/rust/issues/132203
14+
// This version, because it's edition2024, passes thanks to the new
15+
// relative path. The edition2015 version fails, because paths are
16+
// resolved relative to the rs file instead of relative to the md file.
17+
18+
#![doc=include_str!("auxiliary/relative-dir.md")]

0 commit comments

Comments
 (0)