Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 7 additions & 41 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ use rustc_hir::def_id::{DefId, DefIdSet};
use rustc_hir::{ConstStability, Mutability, RustcVersion, StabilityLevel, StableSince};
use rustc_middle::ty::print::PrintTraitRefExt;
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::DUMMY_SP;
use rustc_span::symbol::{Symbol, sym};
use rustc_span::{BytePos, DUMMY_SP, FileName};
use tracing::{debug, info};

pub(crate) use self::context::*;
Expand Down Expand Up @@ -2779,46 +2779,12 @@ 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();

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()?;
files
.iter()
.find(|file| match &file.name {
FileName::Real(real) => real.local_path().map_or(false, |p| p == rel_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;
};
// For scraped examples, we don't need a real span from the SourceMap.
// The URL is already provided in ScrapedInfo, and sources::print_src
// will use that directly. We use DUMMY_SP as a placeholder.
// Note: DUMMY_SP is safe here because href_from_span won't be called
// for scraped examples.
let file_span = rustc_span::DUMMY_SP;

let mut decoration_info = FxIndexMap::default();
decoration_info.insert("highlight focus", vec![byte_ranges.remove(0)]);
Expand Down
12 changes: 9 additions & 3 deletions src/librustdoc/html/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,15 @@ pub(crate) fn print_src(
lines += line_info.start_line as usize;
}
let code = fmt::from_fn(move |fmt| {
let current_href = context
.href_from_span(clean::Span::new(file_span), false)
.expect("only local crates should have sources emitted");
// For scraped examples, use the URL from ScrapedInfo directly.
// For regular sources, derive it from the span.
let current_href = if let SourceContext::Embedded(info) = source_context {
info.url.to_string()
} else {
context
.href_from_span(clean::Span::new(file_span), false)
.expect("only local crates should have sources emitted")
};
highlight::write_code(
fmt,
s,
Expand Down
Loading