Skip to content

Commit ee2898d

Browse files
committed
Make local_crate_source_file return a RealFileName
so it can be remapped (or not) by callers
1 parent 106146f commit ee2898d

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

Diff for: compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,16 @@ impl DebugContext {
9999
FileNameDisplayPreference::Local
100100
})
101101
.into_owned();
102+
102103
let (name, file_info) = match tcx.sess.local_crate_source_file() {
103104
Some(path) => {
104-
let name = path.to_string_lossy().into_owned();
105+
let name = path
106+
.to_string_lossy(if should_remap_filepaths {
107+
FileNameDisplayPreference::Remapped
108+
} else {
109+
FileNameDisplayPreference::Local
110+
})
111+
.into_owned();
105112
(name, None)
106113
}
107114
None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -838,9 +838,11 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
838838
codegen_unit_name: &str,
839839
debug_context: &CodegenUnitDebugContext<'ll, 'tcx>,
840840
) -> &'ll DIDescriptor {
841+
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
841842
let mut name_in_debuginfo = tcx
842843
.sess
843844
.local_crate_source_file()
845+
.map(|src| src.for_scope(&tcx.sess, RemapPathScopeComponents::DEBUGINFO).to_path_buf())
844846
.unwrap_or_else(|| PathBuf::from(tcx.crate_name(LOCAL_CRATE).as_str()));
845847

846848
// 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>(
868870
// FIXME(#41252) Remove "clang LLVM" if we can get GDB and LLVM to play nice.
869871
let producer = format!("clang LLVM ({rustc_producer})");
870872

871-
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
872873
let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
873874
let work_dir = tcx
874875
.sess

Diff for: compiler/rustc_passes/src/entry.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_hir::{ItemId, Node, CRATE_HIR_ID};
77
use rustc_middle::query::Providers;
88
use rustc_middle::ty::TyCtxt;
99
use rustc_session::config::{sigpipe, CrateType, EntryFnType};
10+
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
1011
use rustc_span::symbol::sym;
1112
use rustc_span::{Span, Symbol};
1213

@@ -165,10 +166,14 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) {
165166

166167
// There is no main function.
167168
let mut has_filename = true;
168-
let filename = tcx.sess.local_crate_source_file().unwrap_or_else(|| {
169-
has_filename = false;
170-
Default::default()
171-
});
169+
let filename = tcx
170+
.sess
171+
.local_crate_source_file()
172+
.map(|src| src.for_scope(&tcx.sess, RemapPathScopeComponents::DIAGNOSTICS).to_path_buf())
173+
.unwrap_or_else(|| {
174+
has_filename = false;
175+
Default::default()
176+
});
172177
let main_def_opt = tcx.resolutions(()).main_def;
173178
let code = E0601;
174179
let add_teach_note = tcx.sess.teach(code);

Diff for: compiler/rustc_session/src/session.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use rustc_macros::HashStable_Generic;
2929
pub use rustc_span::def_id::StableCrateId;
3030
use rustc_span::edition::Edition;
3131
use rustc_span::source_map::{FileLoader, FilePathMapping, RealFileLoader, SourceMap};
32+
use rustc_span::RealFileName;
3233
use rustc_span::{SourceFileHashAlgorithm, Span, Symbol};
3334
use rustc_target::asm::InlineAsmArch;
3435
use rustc_target::spec::{CodeModel, PanicStrategy, RelocModel, RelroLevel};
@@ -250,14 +251,9 @@ impl Session {
250251
self.miri_unleashed_features.lock().push((span, feature_gate));
251252
}
252253

253-
pub fn local_crate_source_file(&self) -> Option<PathBuf> {
254+
pub fn local_crate_source_file(&self) -> Option<RealFileName> {
254255
let path = self.io.input.opt_path()?;
255-
// FIXME: The remap path scope should probably not be hardcoded.
256-
if self.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO) {
257-
Some(self.opts.file_path_mapping().map_prefix(path).0.into_owned())
258-
} else {
259-
Some(path.to_path_buf())
260-
}
256+
Some(RealFileName::LocalPath(path.to_path_buf()))
261257
}
262258

263259
fn check_miri_unleashed_features(&self) -> Option<ErrorGuaranteed> {

Diff for: src/librustdoc/html/render/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2506,7 +2506,7 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &mut Context<'_>, item: &c
25062506
// Look for the example file in the source map if it exists, otherwise return a dummy span
25072507
let file_span = (|| {
25082508
let source_map = tcx.sess.source_map();
2509-
let crate_src = tcx.sess.local_crate_source_file()?;
2509+
let crate_src = tcx.sess.local_crate_source_file()?.into_local_path()?;
25102510
let abs_crate_src = crate_src.canonicalize().ok()?;
25112511
let crate_root = abs_crate_src.parent()?.parent()?;
25122512
let rel_path = path.strip_prefix(crate_root).ok()?;

0 commit comments

Comments
 (0)