Skip to content

Commit 102235a

Browse files
authored
Unrolled build for #146091
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 #144752 related to/builds on #145008 bevy still crashes in the same way as #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](#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 #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`
2 parents a2c8b0b + f67e29e commit 102235a

File tree

8 files changed

+73
-16
lines changed

8 files changed

+73
-16
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,24 +2812,46 @@ fn render_call_locations<W: fmt::Write>(
28122812
let needs_expansion = line_max - line_min > NUM_VISIBLE_LINES;
28132813
let locations_encoded = serde_json::to_string(&line_ranges).unwrap();
28142814

2815-
// Look for the example file in the source map if it exists, otherwise return a dummy span
2816-
let file_span = (|| {
2817-
let source_map = tcx.sess.source_map();
2818-
let crate_src = tcx.sess.local_crate_source_file()?.into_local_path()?;
2815+
let source_map = tcx.sess.source_map();
2816+
let files = source_map.files();
2817+
let local = tcx.sess.local_crate_source_file().unwrap();
2818+
2819+
let get_file_start_pos = || {
2820+
let crate_src = local.clone().into_local_path()?;
28192821
let abs_crate_src = crate_src.canonicalize().ok()?;
28202822
let crate_root = abs_crate_src.parent()?.parent()?;
28212823
let rel_path = path.strip_prefix(crate_root).ok()?;
2822-
let files = source_map.files();
2823-
let file = files.iter().find(|file| match &file.name {
2824-
FileName::Real(RealFileName::LocalPath(other_path)) => rel_path == other_path,
2825-
_ => false,
2826-
})?;
2827-
Some(rustc_span::Span::with_root_ctxt(
2828-
file.start_pos + BytePos(byte_min),
2829-
file.start_pos + BytePos(byte_max),
2830-
))
2831-
})()
2832-
.unwrap_or(DUMMY_SP);
2824+
files
2825+
.iter()
2826+
.find(|file| match &file.name {
2827+
FileName::Real(RealFileName::LocalPath(other_path)) => rel_path == other_path,
2828+
_ => false,
2829+
})
2830+
.map(|file| file.start_pos)
2831+
};
2832+
2833+
// Look for the example file in the source map if it exists, otherwise
2834+
// return a span to the local crate's source file
2835+
let Some(file_span) = get_file_start_pos()
2836+
.or_else(|| {
2837+
files
2838+
.iter()
2839+
.find(|file| match &file.name {
2840+
FileName::Real(file_name) => file_name == &local,
2841+
_ => false,
2842+
})
2843+
.map(|file| file.start_pos)
2844+
})
2845+
.map(|start_pos| {
2846+
rustc_span::Span::with_root_ctxt(
2847+
start_pos + BytePos(byte_min),
2848+
start_pos + BytePos(byte_max),
2849+
)
2850+
})
2851+
else {
2852+
// if the fallback span can't be built, don't render the code for this example
2853+
return false;
2854+
};
28332855

28342856
let mut decoration_info = FxIndexMap::default();
28352857
decoration_info.insert("highlight focus", vec![byte_ranges.remove(0)]);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Check that the line number column has the correct layout.
2+
go-to: "file://" + |DOC_PATH| + "/scrape_ice/struct.ObscurelyNamedType1.html"
3+
wait-for: ".scraped-example-title"
4+
assert-attribute: (".scraped-example-title a", {"href": "../src/bar/bar.rs.html#2"})
5+
click: ".scraped-example-title a"
6+
wait-for-property: ("h1", {"innerText": "bar/\nbar.rs"})

tests/rustdoc-gui/sidebar-source-code.goml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ assert: "//*[@class='dir-entry' and @open]/*[normalize-space()='sub_mod']"
7171
// Only "another_folder" should be "open" in "lib2".
7272
assert: "//*[@class='dir-entry' and not(@open)]/*[normalize-space()='another_mod']"
7373
// All other trees should be collapsed.
74-
assert-count: ("//*[@id='src-sidebar']/details[not(normalize-space()='lib2') and not(@open)]", 12)
74+
assert-count: ("//*[@id='src-sidebar']/details[not(normalize-space()='lib2') and not(@open)]", 13)
7575

7676
// We now switch to mobile mode.
7777
set-window-size: (600, 600)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This file is automatically @generated by Cargo.
2+
# It is not intended for manual editing.
3+
version = 4
4+
5+
[[package]]
6+
name = "scrape_ice"
7+
version = "0.1.0"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "scrape_ice"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[[example]]
7+
name = "bar"
8+
path = "examples/bar.rs"
9+
doc-scrape-examples = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
let mut bar = scrape_ice::ObscurelyNamedType1::new();
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ run-flags:-Zrustdoc-scrape-examples
2+
//@ compile-flags: --html-after-content empty.html
3+
pub struct ObscurelyNamedType1;
4+
5+
impl ObscurelyNamedType1 {
6+
pub fn new() -> Self {
7+
ObscurelyNamedType1
8+
}
9+
}

0 commit comments

Comments
 (0)