about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2025-09-02 17:08:57 +0200
committerGitHub <noreply@github.com>2025-09-02 17:08:57 +0200
commit15cde9c505df79ce071858b67e63fbb98512f88e (patch)
tree7a6a5b9a0ebcfe57f41370b6766062782b596f26
parentf77a31fbfff6f3fe983ebeedd3165fbf57d655b1 (diff)
parentf67e29e5a82d5272a03028c5e4a91fbe2bbab81f (diff)
downloadrust-15cde9c505df79ce071858b67e63fbb98512f88e.tar.gz
rust-15cde9c505df79ce071858b67e63fbb98512f88e.zip
Rollup merge of #146091 - janis-bhm:rustdoc-default-span-with-simple-test, r=GuillaumeGomez
fix rustdoc `render_call_locations`  panicking because of default span `DUMMY_SP` pointing at non local-source file

fixes https://github.com/rust-lang/rust/issues/144752
related to/builds on https://github.com/rust-lang/rust/pull/145008

bevy still crashes in the same way as rust-lang/rust#144752 when building docs on nightly, and from what I can tell the cause seems to be the following (copied from zulip [#t-rustdoc > docs on nightly with example scrapes crash](https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/docs.20on.20nightly.20with.20example.20scrapes.20crash)):

> render_call_locations tries to [find](https://github.com/rust-lang/rust/blob/84a17470220e7adf249b18d7c0178dfbede89462/src/librustdoc/html/render/mod.rs#L2816) the source span of a call to add as an example, but the example files are never actually in the source map from what I can tell, and so it falls back to the default span, which points at the first file in the source map.
> Now, the issue that guillaume mentions [here](https://github.com/rust-lang/rust/pull/145008) adds new files to the source map in order to get them into the dep info, and that leads to some files, namely docs-rs/trait-tags.html in the case of bevy because it's added with --html-after-content, being added before any source files, so then the default span points at them, and when href_from_span tries to find the [source file](https://github.com/rust-lang/rust/blob/84a17470220e7adf249b18d7c0178dfbede89462/src/librustdoc/html/render/context.rs#L368) corresponding to the span, the file doesn't belong to local_sources, and it short circuits.
> This can be fixed by just not using DUMMY_SP as the default span and calculating, for example, the crates root source file as the span, because I'm not entirely sure what the href from that span is actually used for; it's not what links to the example in the end.
> I think the proper way of fixing this would be to make sure the example files are part of the local_sources or at least the source map, but I don't know nearly enough about rust internals to be able to figure out how to fix that.

I've included a test that's mostly copied from rust-lang/rust#145008's test with the addition of `--html-after-content after.html` in the `RUSTDOCFLAGS`, which panics on master in conjunction with the `-Zrustdoc-scrape-examples` cargo flag.

cc `@GuillaumeGomez`
-rw-r--r--src/librustdoc/html/render/mod.rs52
-rw-r--r--tests/rustdoc-gui/scrape-examples-ice-links.goml6
-rw-r--r--tests/rustdoc-gui/sidebar-source-code.goml2
-rw-r--r--tests/rustdoc-gui/src/scrape_examples_ice/Cargo.lock7
-rw-r--r--tests/rustdoc-gui/src/scrape_examples_ice/Cargo.toml9
-rw-r--r--tests/rustdoc-gui/src/scrape_examples_ice/empty.html1
-rw-r--r--tests/rustdoc-gui/src/scrape_examples_ice/examples/bar.rs3
-rw-r--r--tests/rustdoc-gui/src/scrape_examples_ice/src/lib.rs9
8 files changed, 73 insertions, 16 deletions
diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs
index 6db90c9bf2a..b4ef47d1e26 100644
--- a/src/librustdoc/html/render/mod.rs
+++ b/src/librustdoc/html/render/mod.rs
@@ -2812,24 +2812,46 @@ fn render_call_locations<W: fmt::Write>(
         let needs_expansion = line_max - line_min > NUM_VISIBLE_LINES;
         let locations_encoded = serde_json::to_string(&line_ranges).unwrap();
 
-        // Look for the example file in the source map if it exists, otherwise return a dummy span
-        let file_span = (|| {
-            let source_map = tcx.sess.source_map();
-            let crate_src = tcx.sess.local_crate_source_file()?.into_local_path()?;
+        let source_map = tcx.sess.source_map();
+        let files = source_map.files();
+        let local = tcx.sess.local_crate_source_file().unwrap();
+
+        let get_file_start_pos = || {
+            let crate_src = local.clone().into_local_path()?;
             let abs_crate_src = crate_src.canonicalize().ok()?;
             let crate_root = abs_crate_src.parent()?.parent()?;
             let rel_path = path.strip_prefix(crate_root).ok()?;
-            let files = source_map.files();
-            let file = files.iter().find(|file| match &file.name {
-                FileName::Real(RealFileName::LocalPath(other_path)) => rel_path == other_path,
-                _ => false,
-            })?;
-            Some(rustc_span::Span::with_root_ctxt(
-                file.start_pos + BytePos(byte_min),
-                file.start_pos + BytePos(byte_max),
-            ))
-        })()
-        .unwrap_or(DUMMY_SP);
+            files
+                .iter()
+                .find(|file| match &file.name {
+                    FileName::Real(RealFileName::LocalPath(other_path)) => rel_path == other_path,
+                    _ => false,
+                })
+                .map(|file| file.start_pos)
+        };
+
+        // Look for the example file in the source map if it exists, otherwise
+        // return a span to the local crate's source file
+        let Some(file_span) = get_file_start_pos()
+            .or_else(|| {
+                files
+                    .iter()
+                    .find(|file| match &file.name {
+                        FileName::Real(file_name) => file_name == &local,
+                        _ => false,
+                    })
+                    .map(|file| file.start_pos)
+            })
+            .map(|start_pos| {
+                rustc_span::Span::with_root_ctxt(
+                    start_pos + BytePos(byte_min),
+                    start_pos + BytePos(byte_max),
+                )
+            })
+        else {
+            // if the fallback span can't be built, don't render the code for this example
+            return false;
+        };
 
         let mut decoration_info = FxIndexMap::default();
         decoration_info.insert("highlight focus", vec![byte_ranges.remove(0)]);
diff --git a/tests/rustdoc-gui/scrape-examples-ice-links.goml b/tests/rustdoc-gui/scrape-examples-ice-links.goml
new file mode 100644
index 00000000000..1db220e1a32
--- /dev/null
+++ b/tests/rustdoc-gui/scrape-examples-ice-links.goml
@@ -0,0 +1,6 @@
+// Check that the line number column has the correct layout.
+go-to: "file://" + |DOC_PATH| + "/scrape_ice/struct.ObscurelyNamedType1.html"
+wait-for: ".scraped-example-title"
+assert-attribute: (".scraped-example-title a", {"href": "../src/bar/bar.rs.html#2"})
+click: ".scraped-example-title a"
+wait-for-property: ("h1", {"innerText": "bar/\nbar.rs"})
diff --git a/tests/rustdoc-gui/sidebar-source-code.goml b/tests/rustdoc-gui/sidebar-source-code.goml
index 3f6914a89d6..a3c19d8c6d0 100644
--- a/tests/rustdoc-gui/sidebar-source-code.goml
+++ b/tests/rustdoc-gui/sidebar-source-code.goml
@@ -71,7 +71,7 @@ assert: "//*[@class='dir-entry' and @open]/*[normalize-space()='sub_mod']"
 // Only "another_folder" should be "open" in "lib2".
 assert: "//*[@class='dir-entry' and not(@open)]/*[normalize-space()='another_mod']"
 // All other trees should be collapsed.
-assert-count: ("//*[@id='src-sidebar']/details[not(normalize-space()='lib2') and not(@open)]", 12)
+assert-count: ("//*[@id='src-sidebar']/details[not(normalize-space()='lib2') and not(@open)]", 13)
 
 // We now switch to mobile mode.
 set-window-size: (600, 600)
diff --git a/tests/rustdoc-gui/src/scrape_examples_ice/Cargo.lock b/tests/rustdoc-gui/src/scrape_examples_ice/Cargo.lock
new file mode 100644
index 00000000000..03410a0da12
--- /dev/null
+++ b/tests/rustdoc-gui/src/scrape_examples_ice/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "scrape_ice"
+version = "0.1.0"
diff --git a/tests/rustdoc-gui/src/scrape_examples_ice/Cargo.toml b/tests/rustdoc-gui/src/scrape_examples_ice/Cargo.toml
new file mode 100644
index 00000000000..076ff38ad23
--- /dev/null
+++ b/tests/rustdoc-gui/src/scrape_examples_ice/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "scrape_ice"
+version = "0.1.0"
+edition = "2024"
+
+[[example]]
+name = "bar"
+path = "examples/bar.rs"
+doc-scrape-examples = true
diff --git a/tests/rustdoc-gui/src/scrape_examples_ice/empty.html b/tests/rustdoc-gui/src/scrape_examples_ice/empty.html
new file mode 100644
index 00000000000..8b137891791
--- /dev/null
+++ b/tests/rustdoc-gui/src/scrape_examples_ice/empty.html
@@ -0,0 +1 @@
+
diff --git a/tests/rustdoc-gui/src/scrape_examples_ice/examples/bar.rs b/tests/rustdoc-gui/src/scrape_examples_ice/examples/bar.rs
new file mode 100644
index 00000000000..9f05f9e46e8
--- /dev/null
+++ b/tests/rustdoc-gui/src/scrape_examples_ice/examples/bar.rs
@@ -0,0 +1,3 @@
+fn main() {
+    let mut bar = scrape_ice::ObscurelyNamedType1::new();
+}
diff --git a/tests/rustdoc-gui/src/scrape_examples_ice/src/lib.rs b/tests/rustdoc-gui/src/scrape_examples_ice/src/lib.rs
new file mode 100644
index 00000000000..b854c7722c9
--- /dev/null
+++ b/tests/rustdoc-gui/src/scrape_examples_ice/src/lib.rs
@@ -0,0 +1,9 @@
+//@ run-flags:-Zrustdoc-scrape-examples
+//@ compile-flags: --html-after-content empty.html
+pub struct ObscurelyNamedType1;
+
+impl ObscurelyNamedType1 {
+    pub fn new() -> Self {
+        ObscurelyNamedType1
+    }
+}