Skip to content

Commit f67e29e

Browse files
committed
squash fix render_call_locations panic when default span points at file outside of local_sources
add test against crashing with --html-after-content file correctly add --html-after-content to env not args formatting fix for rustdoc-call-locations-after-content/rmake.rs Use local crate source file as default span in `render_call_locations` - avoids unwrapping the first file added to the source map as a local file in `href_from_span` move test to tests/rustdoc-gui, rename to scrape_examples_ice test link is correct use rustdocflags, rename path in example, track lock file factor out duplicate function calls use compile-flags to make sure the after.html file is actually included in the rustdoc call fix goml go-to path increment assert-count in sidebar-source-code.goml adjust crate-search width in search-result-display.goml renamed Bar in scrape_examples_ice test make crate name shorter ..
1 parent 154037f commit f67e29e

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)