From 1819b4fa2339fcf416c8f3856f19190cab8c54ff Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Sat, 26 Oct 2024 17:46:22 -0700 Subject: [PATCH 1/2] 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. --- src/librustdoc/doctest/rust.rs | 14 ++++++++++- .../doctest/auxiliary/extern_macros_2024.rs | 10 ++++++++ .../doctest/auxiliary/relative-dir-empty-file | 0 .../doctest/auxiliary/relative-dir.md | 3 +++ .../doctest/doctest-output-include-fail.rs | 3 ++- .../doctest/doctest-output.edition2015.stdout | 8 +++++++ .../doctest/doctest-output.edition2024.stdout | 8 +++++++ tests/rustdoc-ui/doctest/doctest-output.rs | 10 +++++--- .../rustdoc-ui/doctest/doctest-output.stdout | 8 ------- ...th-include-bytes-132203.edition2015.stdout | 24 +++++++++++++++++++ ...th-include-bytes-132203.edition2024.stdout | 6 +++++ .../relative-path-include-bytes-132203.rs | 17 +++++++++++++ 12 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 tests/rustdoc-ui/doctest/auxiliary/extern_macros_2024.rs create mode 100644 tests/rustdoc-ui/doctest/auxiliary/relative-dir-empty-file create mode 100644 tests/rustdoc-ui/doctest/auxiliary/relative-dir.md create mode 100644 tests/rustdoc-ui/doctest/doctest-output.edition2015.stdout create mode 100644 tests/rustdoc-ui/doctest/doctest-output.edition2024.stdout delete mode 100644 tests/rustdoc-ui/doctest/doctest-output.stdout create mode 100644 tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.edition2015.stdout create mode 100644 tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.edition2024.stdout create mode 100644 tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.rs 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..535d681750190 --- /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 17) ... FAILED + +failures: + +---- $DIR/relative-path-include-bytes-132203.rs - (line 17) 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:18: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 17) + +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..6ec41a3c05283 --- /dev/null +++ b/tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.rs @@ -0,0 +1,17 @@ +//@ 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")] From ac7de1a0d925af4504f9fb100be071e2fd346d13 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Tue, 29 Oct 2024 23:21:54 -0700 Subject: [PATCH 2/2] Ignore Windows due to its differing path syntax --- .../relative-path-include-bytes-132203.edition2015.stdout | 8 ++++---- .../doctest/relative-path-include-bytes-132203.rs | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) 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 index 535d681750190..ca6e77502640e 100644 --- a/tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.edition2015.stdout +++ b/tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.edition2015.stdout @@ -1,12 +1,12 @@ running 1 test -test $DIR/relative-path-include-bytes-132203.rs - (line 17) ... FAILED +test $DIR/relative-path-include-bytes-132203.rs - (line 18) ... FAILED failures: ----- $DIR/relative-path-include-bytes-132203.rs - (line 17) stdout ---- +---- $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:18:9 + --> $DIR/relative-path-include-bytes-132203.rs:19:9 | LL | let x = include_bytes!("relative-dir-empty-file"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -18,7 +18,7 @@ error: aborting due to 1 previous error Couldn't compile the test. failures: - $DIR/relative-path-include-bytes-132203.rs - (line 17) + $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.rs b/tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.rs index 6ec41a3c05283..b393d126306aa 100644 --- a/tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.rs +++ b/tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.rs @@ -1,3 +1,4 @@ +//@ ignore-windows //@ revisions: edition2015 edition2024 //@[edition2015]edition:2015 //@[edition2015]check-fail