Skip to content

Commit 7ab22cd

Browse files
authored
Rollup merge of #152664 - Embers-of-the-Fire:fix-152601, r=GuillaumeGomez
Fix mis-constructed `file_span` when generating scraped examples Fixes #152601. Seemingly relative with #147399 but I could not reproduce the original ICE. This PR removes the `file_span` logic from scraped example generation. The original implementation did not read or write items using `file_span`; it only used it to locate a source file, `context.href_from_span`. However, the span was validated against the wrong file, which could trigger ICEs on inputs such as multibyte characters due to an incorrectly constructed span. Since scraped examples do not use the span and the `url` is already given, the safest and simplest fix is to remove it. Tested against the crate and MCVE documented in the original issue. P.S. there seems to be some bug when rendering call sites, but since fixing mis-behavior is a change rather than a bug-fix that would be implemented in another PR.
2 parents 910d4f4 + d13828b commit 7ab22cd

2 files changed

Lines changed: 16 additions & 44 deletions

File tree

src/librustdoc/html/render/mod.rs

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ use rustc_hir::def_id::{DefId, DefIdSet};
5959
use rustc_hir::{ConstStability, Mutability, RustcVersion, StabilityLevel, StableSince};
6060
use rustc_middle::ty::print::PrintTraitRefExt;
6161
use rustc_middle::ty::{self, TyCtxt};
62+
use rustc_span::DUMMY_SP;
6263
use rustc_span::symbol::{Symbol, sym};
63-
use rustc_span::{BytePos, DUMMY_SP, FileName};
6464
use tracing::{debug, info};
6565

6666
pub(crate) use self::context::*;
@@ -2785,46 +2785,12 @@ fn render_call_locations<W: fmt::Write>(
27852785
let needs_expansion = line_max - line_min > NUM_VISIBLE_LINES;
27862786
let locations_encoded = serde_json::to_string(&line_ranges).unwrap();
27872787

2788-
let source_map = tcx.sess.source_map();
2789-
let files = source_map.files();
2790-
let local = tcx.sess.local_crate_source_file().unwrap();
2791-
2792-
let get_file_start_pos = || {
2793-
let crate_src = local.clone().into_local_path()?;
2794-
let abs_crate_src = crate_src.canonicalize().ok()?;
2795-
let crate_root = abs_crate_src.parent()?.parent()?;
2796-
let rel_path = path.strip_prefix(crate_root).ok()?;
2797-
files
2798-
.iter()
2799-
.find(|file| match &file.name {
2800-
FileName::Real(real) => real.local_path().map_or(false, |p| p == rel_path),
2801-
_ => false,
2802-
})
2803-
.map(|file| file.start_pos)
2804-
};
2805-
2806-
// Look for the example file in the source map if it exists, otherwise
2807-
// return a span to the local crate's source file
2808-
let Some(file_span) = get_file_start_pos()
2809-
.or_else(|| {
2810-
files
2811-
.iter()
2812-
.find(|file| match &file.name {
2813-
FileName::Real(file_name) => file_name == &local,
2814-
_ => false,
2815-
})
2816-
.map(|file| file.start_pos)
2817-
})
2818-
.map(|start_pos| {
2819-
rustc_span::Span::with_root_ctxt(
2820-
start_pos + BytePos(byte_min),
2821-
start_pos + BytePos(byte_max),
2822-
)
2823-
})
2824-
else {
2825-
// if the fallback span can't be built, don't render the code for this example
2826-
return false;
2827-
};
2788+
// For scraped examples, we don't need a real span from the SourceMap.
2789+
// The URL is already provided in ScrapedInfo, and sources::print_src
2790+
// will use that directly. We use DUMMY_SP as a placeholder.
2791+
// Note: DUMMY_SP is safe here because href_from_span won't be called
2792+
// for scraped examples.
2793+
let file_span = rustc_span::DUMMY_SP;
28282794

28292795
let mut decoration_info = FxIndexMap::default();
28302796
decoration_info.insert("highlight focus", vec![byte_ranges.remove(0)]);

src/librustdoc/html/sources.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,15 @@ pub(crate) fn print_src(
344344
lines += line_info.start_line as usize;
345345
}
346346
let code = fmt::from_fn(move |fmt| {
347-
let current_href = context
348-
.href_from_span(clean::Span::new(file_span), false)
349-
.expect("only local crates should have sources emitted");
347+
// For scraped examples, use the URL from ScrapedInfo directly.
348+
// For regular sources, derive it from the span.
349+
let current_href = if let SourceContext::Embedded(info) = source_context {
350+
info.url.to_string()
351+
} else {
352+
context
353+
.href_from_span(clean::Span::new(file_span), false)
354+
.expect("only local crates should have sources emitted")
355+
};
350356
highlight::write_code(
351357
fmt,
352358
s,

0 commit comments

Comments
 (0)