Skip to content

Commit 02a5615

Browse files
committed
Auto merge of rust-lang#115214 - Urgau:rfc-3127-trim-paths, r=compiler-errors
Implement rustc part of RFC 3127 trim-paths This PR implements (or at least tries to) [RFC 3127 trim-paths](rust-lang#111540), the rustc part. That is `-Zremap-path-scope` with all of it's components/scopes. `@rustbot` label: +F-trim-paths
2 parents 187b813 + 5d10381 commit 02a5615

File tree

32 files changed

+558
-92
lines changed

32 files changed

+558
-92
lines changed

compiler/rustc_builtin_macros/src/source_util.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ pub fn expand_file(
6161

6262
let topmost = cx.expansion_cause().unwrap_or(sp);
6363
let loc = cx.source_map().lookup_char_pos(topmost.lo());
64-
base::MacEager::expr(
65-
cx.expr_str(topmost, Symbol::intern(&loc.file.name.prefer_remapped().to_string_lossy())),
66-
)
64+
65+
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
66+
base::MacEager::expr(cx.expr_str(
67+
topmost,
68+
Symbol::intern(
69+
&loc.file.name.for_scope(cx.sess, RemapPathScopeComponents::MACRO).to_string_lossy(),
70+
),
71+
))
6772
}
6873

6974
pub fn expand_stringify(

compiler/rustc_codegen_cranelift/src/common.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,12 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
415415
// Note: must be kept in sync with get_caller_location from cg_ssa
416416
pub(crate) fn get_caller_location(&mut self, mut source_info: mir::SourceInfo) -> CValue<'tcx> {
417417
let span_to_caller_location = |fx: &mut FunctionCx<'_, '_, 'tcx>, span: Span| {
418+
use rustc_session::RemapFileNameExt;
418419
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
419420
let caller = fx.tcx.sess.source_map().lookup_char_pos(topmost.lo());
420421
let const_loc = fx.tcx.const_caller_location((
421422
rustc_span::symbol::Symbol::intern(
422-
&caller.file.name.prefer_remapped().to_string_lossy(),
423+
&caller.file.name.for_codegen(&fx.tcx.sess).to_string_lossy(),
423424
),
424425
caller.line as u32,
425426
caller.col_display as u32 + 1,

compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ impl DebugContext {
9797
match &source_file.name {
9898
FileName::Real(path) => {
9999
let (dir_path, file_name) =
100-
split_path_dir_and_file(path.remapped_path_if_available());
100+
split_path_dir_and_file(if self.should_remap_filepaths {
101+
path.remapped_path_if_available()
102+
} else {
103+
path.local_path_if_available()
104+
});
101105
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
102106
let file_name = osstr_as_utf8_bytes(file_name);
103107

@@ -118,7 +122,14 @@ impl DebugContext {
118122
filename => {
119123
let dir_id = line_program.default_directory();
120124
let dummy_file_name = LineString::new(
121-
filename.prefer_remapped().to_string().into_bytes(),
125+
filename
126+
.display(if self.should_remap_filepaths {
127+
FileNameDisplayPreference::Remapped
128+
} else {
129+
FileNameDisplayPreference::Local
130+
})
131+
.to_string()
132+
.into_bytes(),
122133
line_program.encoding(),
123134
line_strings,
124135
);

compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub(crate) struct DebugContext {
3333

3434
dwarf: DwarfUnit,
3535
unit_range_list: RangeList,
36+
37+
should_remap_filepaths: bool,
3638
}
3739

3840
pub(crate) struct FunctionDebugContext {
@@ -65,12 +67,18 @@ impl DebugContext {
6567

6668
let mut dwarf = DwarfUnit::new(encoding);
6769

70+
let should_remap_filepaths = tcx.sess.should_prefer_remapped_for_codegen();
71+
6872
let producer = producer();
6973
let comp_dir = tcx
7074
.sess
7175
.opts
7276
.working_dir
73-
.to_string_lossy(FileNameDisplayPreference::Remapped)
77+
.to_string_lossy(if should_remap_filepaths {
78+
FileNameDisplayPreference::Remapped
79+
} else {
80+
FileNameDisplayPreference::Local
81+
})
7482
.into_owned();
7583
let (name, file_info) = match tcx.sess.local_crate_source_file() {
7684
Some(path) => {
@@ -104,7 +112,12 @@ impl DebugContext {
104112
root.set(gimli::DW_AT_low_pc, AttributeValue::Address(Address::Constant(0)));
105113
}
106114

107-
DebugContext { endian, dwarf, unit_range_list: RangeList(Vec::new()) }
115+
DebugContext {
116+
endian,
117+
dwarf,
118+
unit_range_list: RangeList(Vec::new()),
119+
should_remap_filepaths,
120+
}
108121
}
109122

110123
pub(crate) fn define_function(

compiler/rustc_codegen_llvm/src/back/write.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,17 @@ pub fn target_machine_factory(
259259
};
260260
let debuginfo_compression = SmallCStr::new(&debuginfo_compression);
261261

262+
let should_prefer_remapped_for_split_debuginfo_paths =
263+
sess.should_prefer_remapped_for_split_debuginfo_paths();
264+
262265
Arc::new(move |config: TargetMachineFactoryConfig| {
263266
let path_to_cstring_helper = |path: Option<PathBuf>| -> CString {
264-
let path = path_mapping.map_prefix(path.unwrap_or_default()).0;
267+
let path = path.unwrap_or_default();
268+
let path = if should_prefer_remapped_for_split_debuginfo_paths {
269+
path_mapping.map_prefix(path).0
270+
} else {
271+
path.into()
272+
};
265273
CString::new(path.to_str().unwrap()).unwrap()
266274
};
267275

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ impl GlobalFileTable {
126126
// Since rustc generates coverage maps with relative paths, the
127127
// compilation directory can be combined with the relative paths
128128
// to get absolute paths, if needed.
129-
let working_dir = Symbol::intern(
130-
&tcx.sess.opts.working_dir.remapped_path_if_available().to_string_lossy(),
131-
);
129+
use rustc_session::RemapFileNameExt;
130+
let working_dir =
131+
Symbol::intern(&tcx.sess.opts.working_dir.for_codegen(&tcx.sess).to_string_lossy());
132132
global_file_table.insert(working_dir);
133133
Self { global_file_table }
134134
}

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+72-36
Original file line numberDiff line numberDiff line change
@@ -539,48 +539,77 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
539539
) -> &'ll DIFile {
540540
debug!(?source_file.name);
541541

542+
use rustc_session::RemapFileNameExt;
542543
let (directory, file_name) = match &source_file.name {
543544
FileName::Real(filename) => {
544545
let working_directory = &cx.sess().opts.working_dir;
545546
debug!(?working_directory);
546547

547-
let filename = cx
548-
.sess()
549-
.source_map()
550-
.path_mapping()
551-
.to_embeddable_absolute_path(filename.clone(), working_directory);
552-
553-
// Construct the absolute path of the file
554-
let abs_path = filename.remapped_path_if_available();
555-
debug!(?abs_path);
556-
557-
if let Ok(rel_path) =
558-
abs_path.strip_prefix(working_directory.remapped_path_if_available())
559-
{
560-
// If the compiler's working directory (which also is the DW_AT_comp_dir of
561-
// the compilation unit) is a prefix of the path we are about to emit, then
562-
// only emit the part relative to the working directory.
563-
// Because of path remapping we sometimes see strange things here: `abs_path`
564-
// might actually look like a relative path
565-
// (e.g. `<crate-name-and-version>/src/lib.rs`), so if we emit it without
566-
// taking the working directory into account, downstream tooling will
567-
// interpret it as `<working-directory>/<crate-name-and-version>/src/lib.rs`,
568-
// which makes no sense. Usually in such cases the working directory will also
569-
// be remapped to `<crate-name-and-version>` or some other prefix of the path
570-
// we are remapping, so we end up with
571-
// `<crate-name-and-version>/<crate-name-and-version>/src/lib.rs`.
572-
// By moving the working directory portion into the `directory` part of the
573-
// DIFile, we allow LLVM to emit just the relative path for DWARF, while
574-
// still emitting the correct absolute path for CodeView.
575-
(
576-
working_directory.to_string_lossy(FileNameDisplayPreference::Remapped),
577-
rel_path.to_string_lossy().into_owned(),
578-
)
548+
if cx.sess().should_prefer_remapped_for_codegen() {
549+
let filename = cx
550+
.sess()
551+
.source_map()
552+
.path_mapping()
553+
.to_embeddable_absolute_path(filename.clone(), working_directory);
554+
555+
// Construct the absolute path of the file
556+
let abs_path = filename.remapped_path_if_available();
557+
debug!(?abs_path);
558+
559+
if let Ok(rel_path) =
560+
abs_path.strip_prefix(working_directory.remapped_path_if_available())
561+
{
562+
// If the compiler's working directory (which also is the DW_AT_comp_dir of
563+
// the compilation unit) is a prefix of the path we are about to emit, then
564+
// only emit the part relative to the working directory.
565+
// Because of path remapping we sometimes see strange things here: `abs_path`
566+
// might actually look like a relative path
567+
// (e.g. `<crate-name-and-version>/src/lib.rs`), so if we emit it without
568+
// taking the working directory into account, downstream tooling will
569+
// interpret it as `<working-directory>/<crate-name-and-version>/src/lib.rs`,
570+
// which makes no sense. Usually in such cases the working directory will also
571+
// be remapped to `<crate-name-and-version>` or some other prefix of the path
572+
// we are remapping, so we end up with
573+
// `<crate-name-and-version>/<crate-name-and-version>/src/lib.rs`.
574+
// By moving the working directory portion into the `directory` part of the
575+
// DIFile, we allow LLVM to emit just the relative path for DWARF, while
576+
// still emitting the correct absolute path for CodeView.
577+
(
578+
working_directory.to_string_lossy(FileNameDisplayPreference::Remapped),
579+
rel_path.to_string_lossy().into_owned(),
580+
)
581+
} else {
582+
("".into(), abs_path.to_string_lossy().into_owned())
583+
}
579584
} else {
580-
("".into(), abs_path.to_string_lossy().into_owned())
585+
let working_directory = working_directory.local_path_if_available();
586+
let filename = filename.local_path_if_available();
587+
588+
debug!(?working_directory, ?filename);
589+
590+
let abs_path: Cow<'_, Path> = if filename.is_absolute() {
591+
filename.into()
592+
} else {
593+
let mut p = PathBuf::new();
594+
p.push(working_directory);
595+
p.push(filename);
596+
p.into()
597+
};
598+
599+
if let Ok(rel_path) = abs_path.strip_prefix(working_directory) {
600+
(
601+
working_directory.to_string_lossy().into(),
602+
rel_path.to_string_lossy().into_owned(),
603+
)
604+
} else {
605+
("".into(), abs_path.to_string_lossy().into_owned())
606+
}
581607
}
582608
}
583-
other => ("".into(), other.prefer_remapped().to_string_lossy().into_owned()),
609+
other => {
610+
debug!(?other);
611+
("".into(), other.for_codegen(cx.sess()).to_string_lossy().into_owned())
612+
}
584613
};
585614

586615
let hash_kind = match source_file.src_hash.kind {
@@ -814,8 +843,9 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
814843
// FIXME(#41252) Remove "clang LLVM" if we can get GDB and LLVM to play nice.
815844
let producer = format!("clang LLVM ({rustc_producer})");
816845

846+
use rustc_session::RemapFileNameExt;
817847
let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
818-
let work_dir = tcx.sess.opts.working_dir.to_string_lossy(FileNameDisplayPreference::Remapped);
848+
let work_dir = tcx.sess.opts.working_dir.for_codegen(&tcx.sess).to_string_lossy();
819849
let flags = "\0";
820850
let output_filenames = tcx.output_filenames(());
821851
let split_name = if tcx.sess.target_can_use_split_dwarf() {
@@ -826,7 +856,13 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
826856
Some(codegen_unit_name),
827857
)
828858
// We get a path relative to the working directory from split_dwarf_path
829-
.map(|f| tcx.sess.source_map().path_mapping().map_prefix(f).0)
859+
.map(|f| {
860+
if tcx.sess.should_prefer_remapped_for_split_debuginfo_paths() {
861+
tcx.sess.source_map().path_mapping().map_prefix(f).0
862+
} else {
863+
f.into()
864+
}
865+
})
830866
} else {
831867
None
832868
}

compiler/rustc_codegen_ssa/src/mir/block.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1454,10 +1454,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
14541454
let tcx = bx.tcx();
14551455

14561456
let mut span_to_caller_location = |span: Span| {
1457+
use rustc_session::RemapFileNameExt;
14571458
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
14581459
let caller = tcx.sess.source_map().lookup_char_pos(topmost.lo());
14591460
let const_loc = tcx.const_caller_location((
1460-
Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()),
1461+
Symbol::intern(&caller.file.name.for_codegen(self.cx.sess()).to_string_lossy()),
14611462
caller.line as u32,
14621463
caller.col_display as u32 + 1,
14631464
));

compiler/rustc_const_eval/src/interpret/intrinsics/caller_location.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
114114
pub(crate) fn location_triple_for_span(&self, span: Span) -> (Symbol, u32, u32) {
115115
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
116116
let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo());
117+
118+
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
117119
(
118-
Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()),
120+
Symbol::intern(
121+
&caller
122+
.file
123+
.name
124+
.for_scope(&self.tcx.sess, RemapPathScopeComponents::DIAGNOSTICS)
125+
.to_string_lossy(),
126+
),
119127
u32::try_from(caller.line).unwrap(),
120128
u32::try_from(caller.col_display).unwrap().checked_add(1).unwrap(),
121129
)

compiler/rustc_metadata/src/rmeta/encoder.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,17 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
525525
// the remapped version -- as is necessary for reproducible builds.
526526
let mut source_file = match source_file.name {
527527
FileName::Real(ref original_file_name) => {
528-
let adapted_file_name = source_map
529-
.path_mapping()
530-
.to_embeddable_absolute_path(original_file_name.clone(), working_directory);
528+
let adapted_file_name = if self.tcx.sess.should_prefer_remapped_for_codegen() {
529+
source_map.path_mapping().to_embeddable_absolute_path(
530+
original_file_name.clone(),
531+
working_directory,
532+
)
533+
} else {
534+
source_map.path_mapping().to_local_embeddable_absolute_path(
535+
original_file_name.clone(),
536+
working_directory,
537+
)
538+
};
531539

532540
if adapted_file_name != *original_file_name {
533541
let mut adapted: SourceFile = (**source_file).clone();

compiler/rustc_mir_transform/src/coverage/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
219219
let tcx = self.tcx;
220220
let source_map = tcx.sess.source_map();
221221
let body_span = self.body_span;
222-
let file_name = Symbol::intern(&self.source_file.name.prefer_remapped().to_string_lossy());
222+
223+
use rustc_session::RemapFileNameExt;
224+
let file_name =
225+
Symbol::intern(&self.source_file.name.for_codegen(self.tcx.sess).to_string_lossy());
223226

224227
for (bcb, spans) in coverage_spans.bcbs_with_coverage_spans() {
225228
let counter_kind = self.coverage_counters.take_bcb_counter(bcb).unwrap_or_else(|| {

0 commit comments

Comments
 (0)