From 106146fd958c3c0d3428cfc7be1f75c5bc81698f Mon Sep 17 00:00:00 2001 From: Urgau Date: Tue, 19 Mar 2024 13:51:22 +0100 Subject: Replace `RemapFileNameExt::for_codegen` with explicit calls --- compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'compiler/rustc_codegen_cranelift/src/debuginfo') diff --git a/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs b/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs index 1bb0e590513..c9f59ae9f29 100644 --- a/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs @@ -84,7 +84,9 @@ impl DebugContext { let mut dwarf = DwarfUnit::new(encoding); - let should_remap_filepaths = tcx.sess.should_prefer_remapped_for_codegen(); + use rustc_session::config::RemapPathScopeComponents; + let should_remap_filepaths = + tcx.sess.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO); let producer = producer(tcx.sess); let comp_dir = tcx -- cgit 1.4.1-3-g733a5 From ee2898d3f1cbece34153581823fafa7f572bbff0 Mon Sep 17 00:00:00 2001 From: Urgau Date: Tue, 19 Mar 2024 13:51:22 +0100 Subject: Make local_crate_source_file return a RealFileName so it can be remapped (or not) by callers --- compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs | 9 ++++++++- compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs | 3 ++- compiler/rustc_passes/src/entry.rs | 13 +++++++++---- compiler/rustc_session/src/session.rs | 10 +++------- src/librustdoc/html/render/mod.rs | 2 +- 5 files changed, 23 insertions(+), 14 deletions(-) (limited to 'compiler/rustc_codegen_cranelift/src/debuginfo') diff --git a/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs b/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs index c9f59ae9f29..222dc56a2b1 100644 --- a/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs @@ -99,9 +99,16 @@ impl DebugContext { FileNameDisplayPreference::Local }) .into_owned(); + let (name, file_info) = match tcx.sess.local_crate_source_file() { Some(path) => { - let name = path.to_string_lossy().into_owned(); + let name = path + .to_string_lossy(if should_remap_filepaths { + FileNameDisplayPreference::Remapped + } else { + FileNameDisplayPreference::Local + }) + .into_owned(); (name, None) } None => (tcx.crate_name(LOCAL_CRATE).to_string(), None), diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index 51179e66298..cd97929024a 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -838,9 +838,11 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>( codegen_unit_name: &str, debug_context: &CodegenUnitDebugContext<'ll, 'tcx>, ) -> &'ll DIDescriptor { + use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt}; let mut name_in_debuginfo = tcx .sess .local_crate_source_file() + .map(|src| src.for_scope(&tcx.sess, RemapPathScopeComponents::DEBUGINFO).to_path_buf()) .unwrap_or_else(|| PathBuf::from(tcx.crate_name(LOCAL_CRATE).as_str())); // To avoid breaking split DWARF, we need to ensure that each codegen unit @@ -868,7 +870,6 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>( // FIXME(#41252) Remove "clang LLVM" if we can get GDB and LLVM to play nice. let producer = format!("clang LLVM ({rustc_producer})"); - use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt}; let name_in_debuginfo = name_in_debuginfo.to_string_lossy(); let work_dir = tcx .sess diff --git a/compiler/rustc_passes/src/entry.rs b/compiler/rustc_passes/src/entry.rs index f34e8d96f05..438c583db49 100644 --- a/compiler/rustc_passes/src/entry.rs +++ b/compiler/rustc_passes/src/entry.rs @@ -7,6 +7,7 @@ use rustc_hir::{ItemId, Node, CRATE_HIR_ID}; use rustc_middle::query::Providers; use rustc_middle::ty::TyCtxt; use rustc_session::config::{sigpipe, CrateType, EntryFnType}; +use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt}; use rustc_span::symbol::sym; use rustc_span::{Span, Symbol}; @@ -165,10 +166,14 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) { // There is no main function. let mut has_filename = true; - let filename = tcx.sess.local_crate_source_file().unwrap_or_else(|| { - has_filename = false; - Default::default() - }); + let filename = tcx + .sess + .local_crate_source_file() + .map(|src| src.for_scope(&tcx.sess, RemapPathScopeComponents::DIAGNOSTICS).to_path_buf()) + .unwrap_or_else(|| { + has_filename = false; + Default::default() + }); let main_def_opt = tcx.resolutions(()).main_def; let code = E0601; let add_teach_note = tcx.sess.teach(code); diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 48b00c3f5a5..65ae666f209 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -29,6 +29,7 @@ use rustc_macros::HashStable_Generic; pub use rustc_span::def_id::StableCrateId; use rustc_span::edition::Edition; use rustc_span::source_map::{FileLoader, FilePathMapping, RealFileLoader, SourceMap}; +use rustc_span::RealFileName; use rustc_span::{SourceFileHashAlgorithm, Span, Symbol}; use rustc_target::asm::InlineAsmArch; use rustc_target::spec::{CodeModel, PanicStrategy, RelocModel, RelroLevel}; @@ -250,14 +251,9 @@ impl Session { self.miri_unleashed_features.lock().push((span, feature_gate)); } - pub fn local_crate_source_file(&self) -> Option { + pub fn local_crate_source_file(&self) -> Option { let path = self.io.input.opt_path()?; - // FIXME: The remap path scope should probably not be hardcoded. - if self.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO) { - Some(self.opts.file_path_mapping().map_prefix(path).0.into_owned()) - } else { - Some(path.to_path_buf()) - } + Some(RealFileName::LocalPath(path.to_path_buf())) } fn check_miri_unleashed_features(&self) -> Option { diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index f1887684797..1ddcb3d8728 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -2506,7 +2506,7 @@ fn render_call_locations(mut w: W, cx: &mut Context<'_>, item: &c // Look for the example file in the source map if it exists, otherwise return a dummy span let file_span = (|| { let source_map = tcx.sess.source_map(); - let crate_src = tcx.sess.local_crate_source_file()?; + let crate_src = tcx.sess.local_crate_source_file()?.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()?; -- cgit 1.4.1-3-g733a5 From 4f4fa42b0ee1cf6c988d3f7ed6bcb4a51e788282 Mon Sep 17 00:00:00 2001 From: Urgau Date: Thu, 21 Mar 2024 21:13:06 +0100 Subject: Introduce `FileNameMapping::to_real_filename` and use it everywhere --- .../rustc_codegen_cranelift/src/debuginfo/mod.rs | 20 ++++++-------- compiler/rustc_codegen_llvm/src/back/write.rs | 5 ++-- .../rustc_codegen_llvm/src/debuginfo/metadata.rs | 31 +++++++++------------- compiler/rustc_session/src/config.rs | 9 ++----- compiler/rustc_session/src/session.rs | 3 +-- compiler/rustc_span/src/source_map.rs | 15 +++++++++++ 6 files changed, 42 insertions(+), 41 deletions(-) (limited to 'compiler/rustc_codegen_cranelift/src/debuginfo') diff --git a/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs b/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs index 222dc56a2b1..b661fa185b8 100644 --- a/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs @@ -85,6 +85,8 @@ impl DebugContext { let mut dwarf = DwarfUnit::new(encoding); use rustc_session::config::RemapPathScopeComponents; + use rustc_session::RemapFileNameExt; + let should_remap_filepaths = tcx.sess.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO); @@ -93,22 +95,16 @@ impl DebugContext { .sess .opts .working_dir - .to_string_lossy(if should_remap_filepaths { - FileNameDisplayPreference::Remapped - } else { - FileNameDisplayPreference::Local - }) - .into_owned(); + .for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO) + .to_string_lossy() + .to_string(); let (name, file_info) = match tcx.sess.local_crate_source_file() { Some(path) => { let name = path - .to_string_lossy(if should_remap_filepaths { - FileNameDisplayPreference::Remapped - } else { - FileNameDisplayPreference::Local - }) - .into_owned(); + .for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO) + .to_string_lossy() + .to_string(); (name, None) } None => (tcx.crate_name(LOCAL_CRATE).to_string(), None), diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index 98480dc90c6..d27df052d43 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -264,10 +264,11 @@ pub fn target_machine_factory( Arc::new(move |config: TargetMachineFactoryConfig| { let path_to_cstring_helper = |path: Option| -> CString { let path = path.unwrap_or_default(); + let path = path_mapping.to_real_filename(path); let path = if should_prefer_remapped_paths { - path_mapping.map_prefix(path).0 + path.remapped_path_if_available() } else { - path.into() + path.local_path_if_available() }; CString::new(path.to_str().unwrap()).unwrap() }; diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index cd97929024a..4dd51adc67b 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -878,26 +878,21 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>( .for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO) .to_string_lossy(); let output_filenames = tcx.output_filenames(()); - let split_name = if tcx.sess.target_can_use_split_dwarf() { - output_filenames - .split_dwarf_path( - tcx.sess.split_debuginfo(), - tcx.sess.opts.unstable_opts.split_dwarf_kind, - Some(codegen_unit_name), - ) - // We get a path relative to the working directory from split_dwarf_path - .map(|f| { - if tcx.sess.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO) { - tcx.sess.source_map().path_mapping().map_prefix(f).0 - } else { - f.into() - } - }) + let split_name = if tcx.sess.target_can_use_split_dwarf() + && let Some(f) = output_filenames.split_dwarf_path( + tcx.sess.split_debuginfo(), + tcx.sess.opts.unstable_opts.split_dwarf_kind, + Some(codegen_unit_name), + ) { + // We get a path relative to the working directory from split_dwarf_path + Some(tcx.sess.source_map().path_mapping().to_real_filename(f)) } else { None - } - .unwrap_or_default(); - let split_name = split_name.to_str().unwrap(); + }; + let split_name = split_name + .as_ref() + .map(|f| f.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO).to_string_lossy()) + .unwrap_or_default(); let kind = DebugEmissionKind::from_generic(tcx.sess.opts.debuginfo); let dwarf_version = diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index fb9005f3a2e..afda432d3ab 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -2842,13 +2842,8 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M early_dcx.early_fatal(format!("Current directory is invalid: {e}")); }); - let remap = file_path_mapping(remap_path_prefix.clone(), &unstable_opts); - let (path, remapped) = remap.map_prefix(&working_dir); - let working_dir = if remapped { - RealFileName::Remapped { virtual_name: path.into_owned(), local_path: Some(working_dir) } - } else { - RealFileName::LocalPath(path.into_owned()) - }; + let file_mapping = file_path_mapping(remap_path_prefix.clone(), &unstable_opts); + let working_dir = file_mapping.to_real_filename(&working_dir); let verbose = matches.opt_present("verbose") || unstable_opts.verbose_internals; diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 65ae666f209..0e15646841e 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -252,8 +252,7 @@ impl Session { } pub fn local_crate_source_file(&self) -> Option { - let path = self.io.input.opt_path()?; - Some(RealFileName::LocalPath(path.to_path_buf())) + Some(self.source_map().path_mapping().to_real_filename(self.io.input.opt_path()?)) } fn check_miri_unleashed_features(&self) -> Option { diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index df7635e447d..770624d331d 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -1129,6 +1129,21 @@ impl FilePathMapping { } } + /// Applies any path prefix substitution as defined by the mapping. + /// The return value is the local path with a "virtual path" representing the remapped + /// part if any remapping was performed. + pub fn to_real_filename<'a>(&self, local_path: impl Into>) -> RealFileName { + let local_path = local_path.into(); + if let (remapped_path, true) = self.map_prefix(&*local_path) { + RealFileName::Remapped { + virtual_name: remapped_path.into_owned(), + local_path: Some(local_path.into_owned()), + } + } else { + RealFileName::LocalPath(local_path.into_owned()) + } + } + /// Expand a relative path to an absolute path with remapping taken into account. /// Use this when absolute paths are required (e.g. debuginfo or crate metadata). /// -- cgit 1.4.1-3-g733a5 From fefb8f1f9cb830e1ca0b0ba449db7c8e7d4ff7ba Mon Sep 17 00:00:00 2001 From: Urgau Date: Fri, 22 Mar 2024 15:27:17 +0100 Subject: Replace Session should_remap_filepaths with filename_display_preference --- .../src/debuginfo/line_info.rs | 15 ++------------ .../rustc_codegen_cranelift/src/debuginfo/mod.rs | 23 +++++++--------------- compiler/rustc_codegen_llvm/src/back/write.rs | 16 +++++++-------- .../rustc_codegen_llvm/src/debuginfo/metadata.rs | 15 ++++++-------- compiler/rustc_session/src/session.rs | 17 +++++++++++++--- compiler/rustc_span/src/lib.rs | 12 +++++++++++ 6 files changed, 48 insertions(+), 50 deletions(-) (limited to 'compiler/rustc_codegen_cranelift/src/debuginfo') diff --git a/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs b/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs index 380eba437c2..32b9c824ded 100644 --- a/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs +++ b/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs @@ -89,11 +89,7 @@ impl DebugContext { match &source_file.name { FileName::Real(path) => { let (dir_path, file_name) = - split_path_dir_and_file(if self.should_remap_filepaths { - path.remapped_path_if_available() - } else { - path.local_path_if_available() - }); + split_path_dir_and_file(path.to_path(self.filename_display_preference)); let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str()); let file_name = osstr_as_utf8_bytes(file_name); @@ -115,14 +111,7 @@ impl DebugContext { filename => { let dir_id = line_program.default_directory(); let dummy_file_name = LineString::new( - filename - .display(if self.should_remap_filepaths { - FileNameDisplayPreference::Remapped - } else { - FileNameDisplayPreference::Local - }) - .to_string() - .into_bytes(), + filename.display(self.filename_display_preference).to_string().into_bytes(), line_program.encoding(), line_strings, ); diff --git a/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs b/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs index b661fa185b8..5d943b5d996 100644 --- a/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs @@ -42,7 +42,7 @@ pub(crate) struct DebugContext { namespace_map: DefIdMap, array_size_type: UnitEntryId, - should_remap_filepaths: bool, + filename_display_preference: FileNameDisplayPreference, } pub(crate) struct FunctionDebugContext { @@ -85,26 +85,17 @@ impl DebugContext { let mut dwarf = DwarfUnit::new(encoding); use rustc_session::config::RemapPathScopeComponents; - use rustc_session::RemapFileNameExt; - let should_remap_filepaths = - tcx.sess.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO); + let filename_display_preference = + tcx.sess.filename_display_preference(RemapPathScopeComponents::DEBUGINFO); let producer = producer(tcx.sess); - let comp_dir = tcx - .sess - .opts - .working_dir - .for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO) - .to_string_lossy() - .to_string(); + let comp_dir = + tcx.sess.opts.working_dir.to_string_lossy(filename_display_preference).to_string(); let (name, file_info) = match tcx.sess.local_crate_source_file() { Some(path) => { - let name = path - .for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO) - .to_string_lossy() - .to_string(); + let name = path.to_string_lossy(filename_display_preference).to_string(); (name, None) } None => (tcx.crate_name(LOCAL_CRATE).to_string(), None), @@ -161,7 +152,7 @@ impl DebugContext { stack_pointer_register, namespace_map: DefIdMap::default(), array_size_type, - should_remap_filepaths, + filename_display_preference, } } diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index d27df052d43..4efea66a7f1 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -258,19 +258,17 @@ pub fn target_machine_factory( }; let debuginfo_compression = SmallCStr::new(&debuginfo_compression); - let should_prefer_remapped_paths = - sess.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO); + let file_name_display_preference = + sess.filename_display_preference(RemapPathScopeComponents::DEBUGINFO); Arc::new(move |config: TargetMachineFactoryConfig| { let path_to_cstring_helper = |path: Option| -> CString { let path = path.unwrap_or_default(); - let path = path_mapping.to_real_filename(path); - let path = if should_prefer_remapped_paths { - path.remapped_path_if_available() - } else { - path.local_path_if_available() - }; - CString::new(path.to_str().unwrap()).unwrap() + let path = path_mapping + .to_real_filename(path) + .to_string_lossy(file_name_display_preference) + .into_owned(); + CString::new(path).unwrap() }; let split_dwarf_file = path_to_cstring_helper(config.split_dwarf_file); diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index 4dd51adc67b..e5fecddec52 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -554,13 +554,16 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> ) -> &'ll DIFile { debug!(?source_file.name); - use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt}; + let filename_display_preference = + cx.sess().filename_display_preference(RemapPathScopeComponents::DEBUGINFO); + + use rustc_session::config::RemapPathScopeComponents; let (directory, file_name) = match &source_file.name { FileName::Real(filename) => { let working_directory = &cx.sess().opts.working_dir; debug!(?working_directory); - if cx.sess().should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO) { + if filename_display_preference == FileNameDisplayPreference::Remapped { let filename = cx .sess() .source_map() @@ -623,13 +626,7 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> } other => { debug!(?other); - ( - "".into(), - other - .for_scope(cx.sess(), RemapPathScopeComponents::DEBUGINFO) - .to_string_lossy() - .into_owned(), - ) + ("".into(), other.display(filename_display_preference).to_string()) } }; diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 0e15646841e..85428303382 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -29,7 +29,7 @@ use rustc_macros::HashStable_Generic; pub use rustc_span::def_id::StableCrateId; use rustc_span::edition::Edition; use rustc_span::source_map::{FileLoader, FilePathMapping, RealFileLoader, SourceMap}; -use rustc_span::RealFileName; +use rustc_span::{FileNameDisplayPreference, RealFileName}; use rustc_span::{SourceFileHashAlgorithm, Span, Symbol}; use rustc_target::asm::InlineAsmArch; use rustc_target::spec::{CodeModel, PanicStrategy, RelocModel, RelroLevel}; @@ -882,8 +882,19 @@ impl Session { self.opts.cg.link_dead_code.unwrap_or(false) } - pub fn should_prefer_remapped(&self, scope: RemapPathScopeComponents) -> bool { - self.opts.unstable_opts.remap_path_scope.contains(scope) + pub fn filename_display_preference( + &self, + scope: RemapPathScopeComponents, + ) -> FileNameDisplayPreference { + assert!( + scope.bits().count_ones() == 1, + "one and only one scope should be passed to `Session::filename_display_preference`" + ); + if self.opts.unstable_opts.remap_path_scope.contains(scope) { + FileNameDisplayPreference::Remapped + } else { + FileNameDisplayPreference::Local + } } } diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 0c974ef4ca3..7ce879807ca 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -271,6 +271,18 @@ impl RealFileName { } } + /// Return the path remmapped or not depending on the [`FileNameDisplayPreference`]. + /// + /// For the purpose of this function, local and short preference are equal. + pub fn to_path(&self, display_pref: FileNameDisplayPreference) -> &Path { + match display_pref { + FileNameDisplayPreference::Local | FileNameDisplayPreference::Short => { + self.local_path_if_available() + } + FileNameDisplayPreference::Remapped => self.remapped_path_if_available(), + } + } + pub fn to_string_lossy(&self, display_pref: FileNameDisplayPreference) -> Cow<'_, str> { match display_pref { FileNameDisplayPreference::Local => self.local_path_if_available().to_string_lossy(), -- cgit 1.4.1-3-g733a5