From fb7ab6de1619128cc1708332b2ccc97987bda24d Mon Sep 17 00:00:00 2001 From: Urgau Date: Fri, 5 Dec 2025 20:44:34 +0100 Subject: [PATCH] Adapt rustdoc to the overhauled filename handling --- src/librustdoc/clean/types.rs | 17 ++++++++++++----- src/librustdoc/doctest.rs | 13 +++---------- src/librustdoc/doctest/markdown.rs | 11 ++++++++--- src/librustdoc/doctest/rust.rs | 10 ---------- src/librustdoc/html/render/context.rs | 4 ++-- src/librustdoc/html/render/mod.rs | 4 ++-- src/librustdoc/html/sources.rs | 12 ++++++------ src/librustdoc/passes/calculate_doc_coverage.rs | 10 +++++++--- 8 files changed, 40 insertions(+), 41 deletions(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 7a4650feac1c..a390a03ff114 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -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(), } } diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 19e8fe3e3ed2..92b4a01409dd 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -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 { '_' }) diff --git a/src/librustdoc/doctest/markdown.rs b/src/librustdoc/doctest/markdown.rs index 7f26605f2562..eba2b0adae5c 100644 --- a/src/librustdoc/doctest/markdown.rs +++ b/src/librustdoc/doctest/markdown.rs @@ -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()); diff --git a/src/librustdoc/doctest/rust.rs b/src/librustdoc/doctest/rust.rs index ee1419d17d6e..b4397b1f01ff 100644 --- a/src/librustdoc/doctest/rust.rs +++ b/src/librustdoc/doctest/rust.rs @@ -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 } diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 3d4dff4a17d2..ee71564a1417 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -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(), }, diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 8740b5935973..4529f5a8c016 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -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( 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) diff --git a/src/librustdoc/html/sources.rs b/src/librustdoc/html/sources.rs index 55f8ddab25b1..89fd78979839 100644 --- a/src/librustdoc/html/sources.rs +++ b/src/librustdoc/html/sources.rs @@ -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(), diff --git a/src/librustdoc/passes/calculate_doc_coverage.rs b/src/librustdoc/passes/calculate_doc_coverage.rs index 66d8b667a4ca..2782baae5e20 100644 --- a/src/librustdoc/passes/calculate_doc_coverage.rs +++ b/src/librustdoc/passes/calculate_doc_coverage.rs @@ -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::>(), ) .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.),