Skip to content

Commit 97f2698

Browse files
committed
Auto merge of #88363 - michaelwoerister:remapped-diagnostics, r=estebank
Path remapping: Make behavior of diagnostics output dependent on presence of --remap-path-prefix. This PR fixes a regression (#87745) with `--remap-path-prefix` where the flag stopped causing diagnostic messages to be remapped as well. The regression was introduced in #83813 where we erroneously assumed that remapping of diagnostic messages was not desired anymore (because #70642 partially undid that functionality with nobody objecting). The issue is fixed by making `--remap-path-prefix` remap diagnostic messages again, including for paths that have been remapped in upstream crates (e.g. the standard library). This means that "sysroot-localization" (implemented in #70642) is also disabled if `rustc` is invoked with `--remap-path-prefix`. The assumption is that once someone starts explicitly remapping paths they also don't want paths to their local Rust installation in their build output. In the future we might want to give more fine-grained control over this behavior via compiler flags (see rust-lang/rfcs#3127 for a related RFC). For now this PR is intended as a regression fix. This PR is an alternative to #88191, which makes diagnostic messages be remapped unconditionally. That approach, however, would effectively revert #70642. Fixes #87745. cc `@cbeuw` r? `@ghost`
2 parents 371f3cd + c296c89 commit 97f2698

File tree

15 files changed

+83
-41
lines changed

15 files changed

+83
-41
lines changed

compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<'tcx> DebugContext<'tcx> {
6666
rustc_interface::util::version_str().unwrap_or("unknown version"),
6767
cranelift_codegen::VERSION,
6868
);
69-
let comp_dir = tcx.sess.opts.working_dir.to_string_lossy(false).into_owned();
69+
let comp_dir = tcx.sess.opts.working_dir.to_string_lossy(FileNameDisplayPreference::Remapped).into_owned();
7070
let (name, file_info) = match tcx.sess.local_crate_source_file.clone() {
7171
Some(path) => {
7272
let name = path.to_string_lossy().into_owned();

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ mod vtable;
7474
mod prelude {
7575
pub(crate) use std::convert::{TryFrom, TryInto};
7676

77-
pub(crate) use rustc_span::Span;
77+
pub(crate) use rustc_span::{Span, FileNameDisplayPreference};
7878

7979
pub(crate) use rustc_hir::def_id::{DefId, LOCAL_CRATE};
8080
pub(crate) use rustc_middle::bug;

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use rustc_middle::ty::{self, AdtKind, GeneratorSubsts, ParamEnv, Ty, TyCtxt};
3535
use rustc_middle::{bug, span_bug};
3636
use rustc_session::config::{self, DebugInfo};
3737
use rustc_span::symbol::{Interner, Symbol};
38+
use rustc_span::FileNameDisplayPreference;
3839
use rustc_span::{self, SourceFile, SourceFileHash, Span};
3940
use rustc_target::abi::{Abi, Align, HasDataLayout, Integer, LayoutOf, TagEncoding};
4041
use rustc_target::abi::{Int, Pointer, F32, F64};
@@ -771,7 +772,13 @@ pub fn file_metadata(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll
771772
let hash = Some(&source_file.src_hash);
772773
let file_name = Some(source_file.name.prefer_remapped().to_string());
773774
let directory = if source_file.is_real_file() && !source_file.is_imported() {
774-
Some(cx.sess().opts.working_dir.to_string_lossy(false).to_string())
775+
Some(
776+
cx.sess()
777+
.opts
778+
.working_dir
779+
.to_string_lossy(FileNameDisplayPreference::Remapped)
780+
.to_string(),
781+
)
775782
} else {
776783
// If the path comes from an upstream crate we assume it has been made
777784
// independent of the compiler's working directory one way or another.
@@ -999,7 +1006,7 @@ pub fn compile_unit_metadata(
9991006
let producer = format!("clang LLVM ({})", rustc_producer);
10001007

10011008
let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
1002-
let work_dir = tcx.sess.opts.working_dir.to_string_lossy(false);
1009+
let work_dir = tcx.sess.opts.working_dir.to_string_lossy(FileNameDisplayPreference::Remapped);
10031010
let flags = "\0";
10041011
let output_filenames = tcx.output_filenames(());
10051012
let out_dir = &output_filenames.out_directory;

compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl AnnotateSnippetEmitterWriter {
126126
}
127127
// owned: line source, line index, annotations
128128
type Owned = (String, usize, Vec<crate::snippet::Annotation>);
129-
let filename = primary_lo.file.name.prefer_local();
129+
let filename = source_map.filename_for_diagnostics(&primary_lo.file.name);
130130
let origin = filename.to_string_lossy();
131131
let annotated_files: Vec<Owned> = annotated_files
132132
.into_iter()

compiler/rustc_errors/src/emitter.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,7 @@ impl EmitterWriter {
13201320
buffer_msg_line_offset,
13211321
&format!(
13221322
"{}:{}:{}",
1323-
loc.file.name.prefer_local(),
1323+
sm.filename_for_diagnostics(&loc.file.name),
13241324
sm.doctest_offset_line(&loc.file.name, loc.line),
13251325
loc.col.0 + 1,
13261326
),
@@ -1334,7 +1334,7 @@ impl EmitterWriter {
13341334
0,
13351335
&format!(
13361336
"{}:{}:{}: ",
1337-
loc.file.name.prefer_local(),
1337+
sm.filename_for_diagnostics(&loc.file.name),
13381338
sm.doctest_offset_line(&loc.file.name, loc.line),
13391339
loc.col.0 + 1,
13401340
),
@@ -1362,12 +1362,12 @@ impl EmitterWriter {
13621362
};
13631363
format!(
13641364
"{}:{}{}",
1365-
annotated_file.file.name.prefer_local(),
1365+
sm.filename_for_diagnostics(&annotated_file.file.name),
13661366
sm.doctest_offset_line(&annotated_file.file.name, first_line.line_index),
13671367
col
13681368
)
13691369
} else {
1370-
format!("{}", annotated_file.file.name.prefer_local())
1370+
format!("{}", sm.filename_for_diagnostics(&annotated_file.file.name))
13711371
};
13721372
buffer.append(buffer_msg_line_offset + 1, &loc, Style::LineAndColumn);
13731373
for _ in 0..max_line_num_len {

compiler/rustc_errors/src/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ impl DiagnosticSpan {
464464
});
465465

466466
DiagnosticSpan {
467-
file_name: start.file.name.prefer_local().to_string(),
467+
file_name: je.sm.filename_for_diagnostics(&start.file.name).to_string(),
468468
byte_start: start.file.original_relative_byte_pos(span.lo()).0,
469469
byte_end: start.file.original_relative_byte_pos(span.hi()).0,
470470
line_start: start.line,

compiler/rustc_expand/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ impl<'a> ExtCtxt<'a> {
11131113
span,
11141114
&format!(
11151115
"cannot resolve relative path in non-file source `{}`",
1116-
other.prefer_local()
1116+
self.source_map().filename_for_diagnostics(&other)
11171117
),
11181118
));
11191119
}

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,14 +1630,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
16301630
(TypeError::Sorts(values), extra) => {
16311631
let sort_string = |ty: Ty<'tcx>| match (extra, ty.kind()) {
16321632
(true, ty::Opaque(def_id, _)) => {
1633-
let pos = self
1634-
.tcx
1635-
.sess
1636-
.source_map()
1637-
.lookup_char_pos(self.tcx.def_span(*def_id).lo());
1633+
let sm = self.tcx.sess.source_map();
1634+
let pos = sm.lookup_char_pos(self.tcx.def_span(*def_id).lo());
16381635
format!(
16391636
" (opaque type at <{}:{}:{}>)",
1640-
pos.file.name.prefer_local(),
1637+
sm.filename_for_diagnostics(&pos.file.name),
16411638
pos.line,
16421639
pos.col.to_usize() + 1,
16431640
)

compiler/rustc_mir/src/interpret/eval_context.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,12 @@ impl<'tcx> fmt::Display for FrameInfo<'tcx> {
272272
write!(f, "inside `{}`", self.instance)?;
273273
}
274274
if !self.span.is_dummy() {
275-
let lo = tcx.sess.source_map().lookup_char_pos(self.span.lo());
275+
let sm = tcx.sess.source_map();
276+
let lo = sm.lookup_char_pos(self.span.lo());
276277
write!(
277278
f,
278279
" at {}:{}:{}",
279-
lo.file.name.prefer_local(),
280+
sm.filename_for_diagnostics(&lo.file.name),
280281
lo.line,
281282
lo.col.to_usize() + 1
282283
)?;

compiler/rustc_parse/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ pub fn maybe_file_to_stream(
190190
let src = source_file.src.as_ref().unwrap_or_else(|| {
191191
sess.span_diagnostic.bug(&format!(
192192
"cannot lex `source_file` without source: {}",
193-
source_file.name.prefer_local()
193+
sess.source_map().filename_for_diagnostics(&source_file.name)
194194
));
195195
});
196196

0 commit comments

Comments
 (0)