Skip to content

Commit 9561346

Browse files
committed
Auto merge of #149709 - Urgau:overhaul-filenames, r=davidtwco
Overhaul filename handling for cross-compiler consistency This PR overhauls the way we handle filenames in the compiler and `rmeta` in order to achieve achieve cross-compiler consistency (ie. having the same path no matter if the filename was created in the current compiler session or is coming from `rmeta`). This is required as some parts of the compiler rely on consistent paths for the soundness of generated code (see #148328). In order to achieved consistency multiple steps are being taken by this PR: - by making `RealFileName` immutable - by only having `SourceMap::to_real_filename` create `RealFileName` - currently `RealFileName` can be created from any `Path` and are remapped afterwards, which creates consistency issue - by also making `RealFileName` holds it's working directory, embeddable name and the remapped scopes - this removes the need for a `Session`, to know the current(!) scopes and cwd, which is invalid as they may not be equal to the scopes used when creating the filename In order for `SourceMap::to_real_filename` to know which scopes to apply `FilePathMapping` now takes the current remapping scopes to apply, which makes `FileNameDisplayPreference` and company useless and are removed. This PR is split-up in multiple commits (unfortunately not atomic), but should help review the changes. Unblocks #147611 Fixes #148328
2 parents e3f9bce + d55b61a commit 9561346

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/debuginfo/line_info.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use std::path::{Component, Path};
66
use cranelift_codegen::MachSrcLoc;
77
use cranelift_codegen::binemit::CodeOffset;
88
use gimli::write::{FileId, FileInfo, LineProgram, LineString, LineStringTable};
9-
use rustc_span::{FileName, Pos, SourceFile, SourceFileAndLine, SourceFileHashAlgorithm, hygiene};
9+
use rustc_span::{
10+
FileName, Pos, RemapPathScopeComponents, SourceFile, SourceFileAndLine,
11+
SourceFileHashAlgorithm, hygiene,
12+
};
1013

1114
use crate::debuginfo::FunctionDebugContext;
1215
use crate::debuginfo::emit::address_for_func;
@@ -95,7 +98,7 @@ impl DebugContext {
9598
match &source_file.name {
9699
FileName::Real(path) => {
97100
let (dir_path, file_name) =
98-
split_path_dir_and_file(path.to_path(self.filename_display_preference));
101+
split_path_dir_and_file(path.path(RemapPathScopeComponents::DEBUGINFO));
99102
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
100103
let file_name = osstr_as_utf8_bytes(file_name);
101104

src/debuginfo/mod.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_hir::def::DefKind;
2121
use rustc_hir::def_id::DefIdMap;
2222
use rustc_session::Session;
2323
use rustc_session::config::DebugInfo;
24-
use rustc_span::{FileNameDisplayPreference, SourceFileHash, StableSourceFileId};
24+
use rustc_span::{RemapPathScopeComponents, SourceFileHash, StableSourceFileId};
2525
use rustc_target::callconv::FnAbi;
2626

2727
pub(crate) use self::emit::{DebugReloc, DebugRelocName};
@@ -44,7 +44,6 @@ pub(crate) struct DebugContext {
4444
namespace_map: DefIdMap<UnitEntryId>,
4545
array_size_type: Option<UnitEntryId>,
4646

47-
filename_display_preference: FileNameDisplayPreference,
4847
embed_source: bool,
4948
}
5049

@@ -102,18 +101,18 @@ impl DebugContext {
102101

103102
let mut dwarf = DwarfUnit::new(encoding);
104103

105-
use rustc_session::config::RemapPathScopeComponents;
106-
107-
let filename_display_preference =
108-
tcx.sess.filename_display_preference(RemapPathScopeComponents::DEBUGINFO);
109-
110104
let producer = producer(tcx.sess);
111-
let comp_dir =
112-
tcx.sess.opts.working_dir.to_string_lossy(filename_display_preference).to_string();
105+
let comp_dir = tcx
106+
.sess
107+
.source_map()
108+
.working_dir()
109+
.path(RemapPathScopeComponents::DEBUGINFO)
110+
.to_string_lossy();
113111

114112
let (name, file_info) = match tcx.sess.local_crate_source_file() {
115113
Some(path) => {
116-
let name = path.to_string_lossy(filename_display_preference).to_string();
114+
let name =
115+
path.path(RemapPathScopeComponents::DEBUGINFO).to_string_lossy().into_owned();
117116
(name, None)
118117
}
119118
None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
@@ -137,7 +136,7 @@ impl DebugContext {
137136

138137
{
139138
let name = dwarf.strings.add(format!("{name}/@/{cgu_name}"));
140-
let comp_dir = dwarf.strings.add(comp_dir);
139+
let comp_dir = dwarf.strings.add(&*comp_dir);
141140

142141
let root = dwarf.unit.root();
143142
let root = dwarf.unit.get_mut(root);
@@ -180,7 +179,6 @@ impl DebugContext {
180179
stack_pointer_register,
181180
namespace_map: DefIdMap::default(),
182181
array_size_type,
183-
filename_display_preference,
184182
embed_source,
185183
})
186184
}

0 commit comments

Comments
 (0)