diff --git a/src/librustdoc/doctest/rust.rs b/src/librustdoc/doctest/rust.rs index a9ab02e29cd95..27d0be133fea8 100644 --- a/src/librustdoc/doctest/rust.rs +++ b/src/librustdoc/doctest/rust.rs @@ -113,7 +113,19 @@ impl<'tcx> HirCollector<'tcx> { let attrs = Attributes::from_ast(ast_attrs); if let Some(doc) = attrs.opt_doc_value() { let span = span_of_fragments(&attrs.doc_strings).unwrap_or(sp); - self.collector.position = span; + self.collector.position = if span.edition().at_least_rust_2024() { + span + } else { + // this span affects filesystem path resolution, + // so we need to keep it the same as it was previously + 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) + }; markdown::find_testable_code( &doc, &mut self.collector, diff --git a/tests/rustdoc-ui/doctest/auxiliary/extern_macros_2024.rs b/tests/rustdoc-ui/doctest/auxiliary/extern_macros_2024.rs new file mode 100644 index 0000000000000..354427000bf71 --- /dev/null +++ b/tests/rustdoc-ui/doctest/auxiliary/extern_macros_2024.rs @@ -0,0 +1,10 @@ +//@ edition:2024 +//@ compile-flags:-Z unstable-options +#![crate_name="extern_macros"] +#[macro_export] +macro_rules! attrs_on_struct { + ( $( #[$attr:meta] )* ) => { + $( #[$attr] )* + pub struct ExpandedStruct; + } +} diff --git a/tests/rustdoc-ui/doctest/auxiliary/relative-dir-empty-file b/tests/rustdoc-ui/doctest/auxiliary/relative-dir-empty-file new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/tests/rustdoc-ui/doctest/auxiliary/relative-dir.md b/tests/rustdoc-ui/doctest/auxiliary/relative-dir.md new file mode 100644 index 0000000000000..90097ce82fb0a --- /dev/null +++ b/tests/rustdoc-ui/doctest/auxiliary/relative-dir.md @@ -0,0 +1,3 @@ +```rust +let x = include_bytes!("relative-dir-empty-file"); +``` diff --git a/tests/rustdoc-ui/doctest/doctest-output-include-fail.rs b/tests/rustdoc-ui/doctest/doctest-output-include-fail.rs index 4fc0674a0c98b..bae61992eb212 100644 --- a/tests/rustdoc-ui/doctest/doctest-output-include-fail.rs +++ b/tests/rustdoc-ui/doctest/doctest-output-include-fail.rs @@ -1,4 +1,5 @@ -//@ compile-flags:--test --test-args=--test-threads=1 +//@ edition:2024 +//@ compile-flags:--test --test-args=--test-threads=1 -Z unstable-options //@ normalize-stdout-test: "tests/rustdoc-ui/doctest" -> "$$DIR" //@ normalize-stdout-test: "finished in \d+\.\d+s" -> "finished in $$TIME" //@ failure-status: 101 diff --git a/tests/rustdoc-ui/doctest/doctest-output.edition2015.stdout b/tests/rustdoc-ui/doctest/doctest-output.edition2015.stdout new file mode 100644 index 0000000000000..0e2e30390ad93 --- /dev/null +++ b/tests/rustdoc-ui/doctest/doctest-output.edition2015.stdout @@ -0,0 +1,8 @@ + +running 3 tests +test $DIR/doctest-output.rs - (line 12) ... ok +test $DIR/doctest-output.rs - ExpandedStruct (line 28) ... ok +test $DIR/doctest-output.rs - foo::bar (line 22) ... ok + +test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME + diff --git a/tests/rustdoc-ui/doctest/doctest-output.edition2024.stdout b/tests/rustdoc-ui/doctest/doctest-output.edition2024.stdout new file mode 100644 index 0000000000000..0e2e30390ad93 --- /dev/null +++ b/tests/rustdoc-ui/doctest/doctest-output.edition2024.stdout @@ -0,0 +1,8 @@ + +running 3 tests +test $DIR/doctest-output.rs - (line 12) ... ok +test $DIR/doctest-output.rs - ExpandedStruct (line 28) ... ok +test $DIR/doctest-output.rs - foo::bar (line 22) ... ok + +test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME + diff --git a/tests/rustdoc-ui/doctest/doctest-output.rs b/tests/rustdoc-ui/doctest/doctest-output.rs index 72394a4bed311..0e5ccf0b090bb 100644 --- a/tests/rustdoc-ui/doctest/doctest-output.rs +++ b/tests/rustdoc-ui/doctest/doctest-output.rs @@ -1,6 +1,10 @@ -//@ edition:2018 -//@ aux-build:extern_macros.rs -//@ compile-flags:--test --test-args=--test-threads=1 +//@ revisions: edition2015 edition2024 +//@[edition2015]edition:2015 +//@[edition2015]aux-build:extern_macros.rs +//@[edition2015]compile-flags:--test --test-args=--test-threads=1 +//@[edition2024]edition:2015 +//@[edition2024]aux-build:extern_macros.rs +//@[edition2024]compile-flags:--test --test-args=--test-threads=1 -Z unstable-options //@ normalize-stdout-test: "tests/rustdoc-ui/doctest" -> "$$DIR" //@ normalize-stdout-test: "finished in \d+\.\d+s" -> "finished in $$TIME" //@ check-pass diff --git a/tests/rustdoc-ui/doctest/doctest-output.stdout b/tests/rustdoc-ui/doctest/doctest-output.stdout deleted file mode 100644 index c3b1570c43ec1..0000000000000 --- a/tests/rustdoc-ui/doctest/doctest-output.stdout +++ /dev/null @@ -1,8 +0,0 @@ - -running 3 tests -test $DIR/doctest-output.rs - (line 8) ... 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 - diff --git a/tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.edition2015.stdout b/tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.edition2015.stdout new file mode 100644 index 0000000000000..ca6e77502640e --- /dev/null +++ b/tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.edition2015.stdout @@ -0,0 +1,24 @@ + +running 1 test +test $DIR/relative-path-include-bytes-132203.rs - (line 18) ... FAILED + +failures: + +---- $DIR/relative-path-include-bytes-132203.rs - (line 18) stdout ---- +error: couldn't read `$DIR/relative-dir-empty-file`: No such file or directory (os error 2) + --> $DIR/relative-path-include-bytes-132203.rs:19:9 + | +LL | let x = include_bytes!("relative-dir-empty-file"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 1 previous error + +Couldn't compile the test. + +failures: + $DIR/relative-path-include-bytes-132203.rs - (line 18) + +test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME + diff --git a/tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.edition2024.stdout b/tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.edition2024.stdout new file mode 100644 index 0000000000000..e4c657030819f --- /dev/null +++ b/tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.edition2024.stdout @@ -0,0 +1,6 @@ + +running 1 test +test $DIR/auxiliary/relative-dir.md - (line 1) ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME + diff --git a/tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.rs b/tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.rs new file mode 100644 index 0000000000000..b393d126306aa --- /dev/null +++ b/tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.rs @@ -0,0 +1,18 @@ +//@ ignore-windows +//@ revisions: edition2015 edition2024 +//@[edition2015]edition:2015 +//@[edition2015]check-fail +//@[edition2015]failure-status: 101 +//@[edition2015]compile-flags:--test --test-args=--test-threads=1 +//@[edition2024]edition:2024 +//@[edition2024]check-pass +//@[edition2024]compile-flags:--test --test-args=--test-threads=1 -Z unstable-options +//@ normalize-stdout-test: "tests/rustdoc-ui/doctest" -> "$$DIR" +//@ normalize-stdout-test: "finished in \d+\.\d+s" -> "finished in $$TIME" + +// /~https://github.com/rust-lang/rust/issues/132203 +// This version, because it's edition2024, passes thanks to the new +// relative path. The edition2015 version fails, because paths are +// resolved relative to the rs file instead of relative to the md file. + +#![doc=include_str!("auxiliary/relative-dir.md")]