Skip to content

Commit

Permalink
Rollup merge of #132210 - notriddle:notriddle/doctest-span-hack, r=Gu…
Browse files Browse the repository at this point in the history
…illaumeGomez

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.
  • Loading branch information
workingjubilee authored Oct 30, 2024
2 parents 6b60f03 + ac7de1a commit 62ba25d
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 13 deletions.
14 changes: 13 additions & 1 deletion src/librustdoc/doctest/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions tests/rustdoc-ui/doctest/auxiliary/extern_macros_2024.rs
Original file line number Diff line number Diff line change
@@ -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;
}
}
Empty file.
3 changes: 3 additions & 0 deletions tests/rustdoc-ui/doctest/auxiliary/relative-dir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```rust
let x = include_bytes!("relative-dir-empty-file");
```
3 changes: 2 additions & 1 deletion tests/rustdoc-ui/doctest/doctest-output-include-fail.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/rustdoc-ui/doctest/doctest-output.edition2015.stdout
Original file line number Diff line number Diff line change
@@ -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

8 changes: 8 additions & 0 deletions tests/rustdoc-ui/doctest/doctest-output.edition2024.stdout
Original file line number Diff line number Diff line change
@@ -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

10 changes: 7 additions & 3 deletions tests/rustdoc-ui/doctest/doctest-output.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 0 additions & 8 deletions tests/rustdoc-ui/doctest/doctest-output.stdout

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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

18 changes: 18 additions & 0 deletions tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.rs
Original file line number Diff line number Diff line change
@@ -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")]

0 comments on commit 62ba25d

Please sign in to comment.