Adapt rustdoc to the overhauled filename handling

This commit is contained in:
Urgau 2025-12-05 20:44:34 +01:00
parent 8cbfb26383
commit fb7ab6de16
8 changed files with 40 additions and 41 deletions

View file

@ -25,7 +25,7 @@ use rustc_resolve::rustdoc::{
use rustc_session::Session;
use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{Symbol, kw, sym};
use rustc_span::{DUMMY_SP, FileName, Loc};
use rustc_span::{DUMMY_SP, FileName, Loc, RemapPathScopeComponents};
use tracing::{debug, trace};
use {rustc_ast as ast, rustc_hir as hir};
@ -148,10 +148,17 @@ impl ExternalCrate {
pub(crate) fn src_root(&self, tcx: TyCtxt<'_>) -> PathBuf {
match self.src(tcx) {
FileName::Real(ref p) => match p.local_path_if_available().parent() {
Some(p) => p.to_path_buf(),
None => PathBuf::new(),
},
FileName::Real(ref p) => {
match p
.local_path()
.or(Some(p.path(RemapPathScopeComponents::MACRO)))
.unwrap()
.parent()
{
Some(p) => p.to_path_buf(),
None => PathBuf::new(),
}
}
_ => PathBuf::new(),
}
}

View file

@ -29,7 +29,7 @@ use rustc_middle::ty::TyCtxt;
use rustc_session::config::{self, CrateType, ErrorOutputType, Input};
use rustc_session::lint;
use rustc_span::edition::Edition;
use rustc_span::{FileName, Span};
use rustc_span::{FileName, RemapPathScopeComponents, Span};
use rustc_target::spec::{Target, TargetTuple};
use tempfile::{Builder as TempFileBuilder, TempDir};
use tracing::debug;
@ -952,14 +952,7 @@ impl ScrapedDocTest {
}
fn path(&self) -> PathBuf {
match &self.filename {
FileName::Real(path) => {
if let Some(local_path) = path.local_path() {
local_path.to_path_buf()
} else {
// Somehow we got the filename from the metadata of another crate, should never happen
unreachable!("doctest from a different crate");
}
}
FileName::Real(name) => name.path(RemapPathScopeComponents::DIAGNOSTICS).to_path_buf(),
_ => PathBuf::from(r"doctest.rs"),
}
}
@ -1007,7 +1000,7 @@ impl CreateRunnableDocTests {
// For example `module/file.rs` would become `module_file_rs`
let file = scraped_test
.filename
.prefer_local()
.prefer_local_unconditionally()
.to_string_lossy()
.chars()
.map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })

View file

@ -4,7 +4,8 @@ use std::fs::read_to_string;
use std::sync::{Arc, Mutex};
use rustc_session::config::Input;
use rustc_span::{DUMMY_SP, FileName};
use rustc_span::source_map::FilePathMapping;
use rustc_span::{DUMMY_SP, FileName, RealFileName};
use tempfile::tempdir;
use super::{
@ -105,8 +106,12 @@ pub(crate) fn test(input: &Input, options: Options) -> Result<(), String> {
cur_path: vec![],
filename: input
.opt_path()
.map(ToOwned::to_owned)
.map(FileName::from)
.map(|f| {
// We don't have access to a rustc Session so let's just use a dummy
// filepath mapping to create a real filename.
let file_mapping = FilePathMapping::empty();
FileName::Real(file_mapping.to_real_filename(&RealFileName::empty(), f))
})
.unwrap_or(FileName::Custom("input".to_owned())),
};
let codes = ErrorCodes::from(options.unstable_features.is_nightly_build());

View file

@ -1,7 +1,6 @@
//! Doctest functionality used only for doctests in `.rs` source files.
use std::cell::Cell;
use std::env;
use std::str::FromStr;
use std::sync::Arc;
@ -31,15 +30,6 @@ struct RustCollector {
impl RustCollector {
fn get_filename(&self) -> FileName {
let filename = self.source_map.span_to_filename(self.position);
if let FileName::Real(ref filename) = filename {
let path = filename.remapped_path_if_available();
// Strip the cwd prefix from the path. This will likely exist if
// the path was not remapped.
let path = env::current_dir()
.map(|cur_dir| path.strip_prefix(&cur_dir).unwrap_or(path))
.unwrap_or(path);
return path.to_owned().into();
}
filename
}

View file

@ -365,7 +365,7 @@ impl<'tcx> Context<'tcx> {
// We can safely ignore synthetic `SourceFile`s.
let file = match span.filename(self.sess()) {
FileName::Real(ref path) => path.local_path_if_available().to_path_buf(),
FileName::Real(ref path) => path.local_path()?.to_path_buf(),
_ => return None,
};
let file = &file;
@ -499,7 +499,7 @@ impl<'tcx> Context<'tcx> {
} = options;
let src_root = match krate.src(tcx) {
FileName::Real(ref p) => match p.local_path_if_available().parent() {
FileName::Real(ref p) => match p.local_path().map(|p| p.parent()).flatten() {
Some(p) => p.to_path_buf(),
None => PathBuf::new(),
},

View file

@ -60,7 +60,7 @@ use rustc_hir::{ConstStability, Mutability, RustcVersion, StabilityLevel, Stable
use rustc_middle::ty::print::PrintTraitRefExt;
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::symbol::{Symbol, sym};
use rustc_span::{BytePos, DUMMY_SP, FileName, RealFileName};
use rustc_span::{BytePos, DUMMY_SP, FileName};
use tracing::{debug, info};
pub(crate) use self::context::*;
@ -2772,7 +2772,7 @@ fn render_call_locations<W: fmt::Write>(
files
.iter()
.find(|file| match &file.name {
FileName::Real(RealFileName::LocalPath(other_path)) => rel_path == other_path,
FileName::Real(real) => real.local_path().map_or(false, |p| p == rel_path),
_ => false,
})
.map(|file| file.start_pos)

View file

@ -8,7 +8,7 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_span::{FileName, FileNameDisplayPreference, RealFileName};
use rustc_span::{FileName, RealFileName, RemapPathScopeComponents};
use tracing::info;
use super::render::Context;
@ -148,7 +148,10 @@ impl DocVisitor<'_> for SourceCollector<'_, '_> {
span,
format!(
"failed to render source code for `{filename}`: {e}",
filename = filename.to_string_lossy(FileNameDisplayPreference::Local),
filename = filename
.path(RemapPathScopeComponents::DIAGNOSTICS)
.to_string_lossy()
.into_owned(),
),
);
false
@ -224,10 +227,7 @@ impl SourceCollector<'_, '_> {
cur.push(&fname);
let title = format!("{} - source", src_fname.to_string_lossy());
let desc = format!(
"Source of the Rust file `{}`.",
file.to_string_lossy(FileNameDisplayPreference::Remapped)
);
let desc = format!("Source of the Rust file `{}`.", p.to_string_lossy());
let page = layout::Page {
title: &title,
short_title: &src_fname.to_string_lossy(),

View file

@ -7,7 +7,7 @@ use rustc_hir as hir;
use rustc_lint::builtin::MISSING_DOCS;
use rustc_middle::lint::{LevelAndSource, LintLevelSource};
use rustc_session::lint;
use rustc_span::FileName;
use rustc_span::{FileName, RemapPathScopeComponents};
use serde::Serialize;
use tracing::debug;
@ -124,7 +124,7 @@ impl CoverageCalculator<'_, '_> {
&self
.items
.iter()
.map(|(k, v)| (k.prefer_local().to_string(), v))
.map(|(k, v)| (k.prefer_local_unconditionally().to_string(), v))
.collect::<BTreeMap<String, &ItemCount>>(),
)
.expect("failed to convert JSON data to string")
@ -167,7 +167,11 @@ impl CoverageCalculator<'_, '_> {
for (file, &count) in &self.items {
if let Some(percentage) = count.percentage() {
print_table_record(
&limit_filename_len(file.prefer_local().to_string_lossy().into()),
&limit_filename_len(
file.display(RemapPathScopeComponents::DIAGNOSTICS)
.to_string_lossy()
.into(),
),
count,
percentage,
count.examples_percentage().unwrap_or(0.),