Skip to content

Commit 97f2698

Browse files
committed
Auto merge of rust-lang#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 (rust-lang#87745) with `--remap-path-prefix` where the flag stopped causing diagnostic messages to be remapped as well. The regression was introduced in rust-lang#83813 where we erroneously assumed that remapping of diagnostic messages was not desired anymore (because rust-lang#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 rust-lang#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 rust-lang#88191, which makes diagnostic messages be remapped unconditionally. That approach, however, would effectively revert rust-lang#70642. Fixes rust-lang#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

+1-1
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

+1-1
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

+9-2
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

+1-1
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

+4-4
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

+1-1
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

+1-1
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

+3-6
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

+3-2
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

+1-1
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

compiler/rustc_span/src/lib.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,12 @@ impl RealFileName {
260260
}
261261
}
262262

263-
pub fn to_string_lossy(&self, prefer_local: bool) -> Cow<'_, str> {
264-
if prefer_local {
265-
self.local_path_if_available().to_string_lossy()
266-
} else {
267-
self.remapped_path_if_available().to_string_lossy()
263+
pub fn to_string_lossy(&self, display_pref: FileNameDisplayPreference) -> Cow<'_, str> {
264+
match display_pref {
265+
FileNameDisplayPreference::Local => self.local_path_if_available().to_string_lossy(),
266+
FileNameDisplayPreference::Remapped => {
267+
self.remapped_path_if_available().to_string_lossy()
268+
}
268269
}
269270
}
270271
}
@@ -300,17 +301,23 @@ impl From<PathBuf> for FileName {
300301
}
301302
}
302303

304+
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
305+
pub enum FileNameDisplayPreference {
306+
Remapped,
307+
Local,
308+
}
309+
303310
pub struct FileNameDisplay<'a> {
304311
inner: &'a FileName,
305-
prefer_local: bool,
312+
display_pref: FileNameDisplayPreference,
306313
}
307314

308315
impl fmt::Display for FileNameDisplay<'_> {
309316
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
310317
use FileName::*;
311318
match *self.inner {
312319
Real(ref name) => {
313-
write!(fmt, "{}", name.to_string_lossy(self.prefer_local))
320+
write!(fmt, "{}", name.to_string_lossy(self.display_pref))
314321
}
315322
QuoteExpansion(_) => write!(fmt, "<quote expansion>"),
316323
MacroExpansion(_) => write!(fmt, "<macro expansion>"),
@@ -328,7 +335,7 @@ impl fmt::Display for FileNameDisplay<'_> {
328335
impl FileNameDisplay<'_> {
329336
pub fn to_string_lossy(&self) -> Cow<'_, str> {
330337
match self.inner {
331-
FileName::Real(ref inner) => inner.to_string_lossy(self.prefer_local),
338+
FileName::Real(ref inner) => inner.to_string_lossy(self.display_pref),
332339
_ => Cow::from(format!("{}", self)),
333340
}
334341
}
@@ -352,13 +359,17 @@ impl FileName {
352359
}
353360

354361
pub fn prefer_remapped(&self) -> FileNameDisplay<'_> {
355-
FileNameDisplay { inner: self, prefer_local: false }
362+
FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Remapped }
356363
}
357364

358365
// This may include transient local filesystem information.
359366
// Must not be embedded in build outputs.
360367
pub fn prefer_local(&self) -> FileNameDisplay<'_> {
361-
FileNameDisplay { inner: self, prefer_local: true }
368+
FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Local }
369+
}
370+
371+
pub fn display(&self, display_pref: FileNameDisplayPreference) -> FileNameDisplay<'_> {
372+
FileNameDisplay { inner: self, display_pref }
362373
}
363374

364375
pub fn macro_expansion_source_code(src: &str) -> FileName {

compiler/rustc_span/src/source_map.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ impl SourceMap {
427427
}
428428
}
429429

430-
fn span_to_string(&self, sp: Span, prefer_local: bool) -> String {
430+
fn span_to_string(&self, sp: Span, filename_display_pref: FileNameDisplayPreference) -> String {
431431
if self.files.borrow().source_files.is_empty() || sp.is_dummy() {
432432
return "no-location".to_string();
433433
}
@@ -436,7 +436,7 @@ impl SourceMap {
436436
let hi = self.lookup_char_pos(sp.hi());
437437
format!(
438438
"{}:{}:{}: {}:{}",
439-
if prefer_local { lo.file.name.prefer_local() } else { lo.file.name.prefer_remapped() },
439+
lo.file.name.display(filename_display_pref),
440440
lo.line,
441441
lo.col.to_usize() + 1,
442442
hi.line,
@@ -446,20 +446,24 @@ impl SourceMap {
446446

447447
/// Format the span location suitable for embedding in build artifacts
448448
pub fn span_to_embeddable_string(&self, sp: Span) -> String {
449-
self.span_to_string(sp, false)
449+
self.span_to_string(sp, FileNameDisplayPreference::Remapped)
450450
}
451451

452452
/// Format the span location to be printed in diagnostics. Must not be emitted
453453
/// to build artifacts as this may leak local file paths. Use span_to_embeddable_string
454454
/// for string suitable for embedding.
455455
pub fn span_to_diagnostic_string(&self, sp: Span) -> String {
456-
self.span_to_string(sp, true)
456+
self.span_to_string(sp, self.path_mapping.filename_display_for_diagnostics)
457457
}
458458

459459
pub fn span_to_filename(&self, sp: Span) -> FileName {
460460
self.lookup_char_pos(sp.lo()).file.name.clone()
461461
}
462462

463+
pub fn filename_for_diagnostics<'a>(&self, filename: &'a FileName) -> FileNameDisplay<'a> {
464+
filename.display(self.path_mapping.filename_display_for_diagnostics)
465+
}
466+
463467
pub fn is_multiline(&self, sp: Span) -> bool {
464468
let lo = self.lookup_source_file_idx(sp.lo());
465469
let hi = self.lookup_source_file_idx(sp.hi());
@@ -1013,15 +1017,22 @@ impl SourceMap {
10131017
#[derive(Clone)]
10141018
pub struct FilePathMapping {
10151019
mapping: Vec<(PathBuf, PathBuf)>,
1020+
filename_display_for_diagnostics: FileNameDisplayPreference,
10161021
}
10171022

10181023
impl FilePathMapping {
10191024
pub fn empty() -> FilePathMapping {
1020-
FilePathMapping { mapping: vec![] }
1025+
FilePathMapping::new(Vec::new())
10211026
}
10221027

10231028
pub fn new(mapping: Vec<(PathBuf, PathBuf)>) -> FilePathMapping {
1024-
FilePathMapping { mapping }
1029+
let filename_display_for_diagnostics = if mapping.is_empty() {
1030+
FileNameDisplayPreference::Local
1031+
} else {
1032+
FileNameDisplayPreference::Remapped
1033+
};
1034+
1035+
FilePathMapping { mapping, filename_display_for_diagnostics }
10251036
}
10261037

10271038
/// Applies any path prefix substitution as defined by the mapping.

src/test/ui/remap-path-prefix.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// compile-flags: --remap-path-prefix={{src-base}}=remapped
2+
3+
fn main() {
4+
// We cannot actually put an ERROR marker here because
5+
// the file name in the error message is not what the
6+
// test framework expects (since the filename gets remapped).
7+
// We still test the expected error in the stderr file.
8+
ferris
9+
}

src/test/ui/remap-path-prefix.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0425]: cannot find value `ferris` in this scope
2+
--> remapped/remap-path-prefix.rs:8:5
3+
|
4+
LL | ferris
5+
| ^^^^^^ not found in this scope
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0425`.

src/tools/clippy/clippy_lints/src/macro_use.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,8 @@ pub struct MacroRefData {
4747

4848
impl MacroRefData {
4949
pub fn new(name: String, callee: Span, cx: &LateContext<'_>) -> Self {
50-
let mut path = cx
51-
.sess()
52-
.source_map()
53-
.span_to_filename(callee)
54-
.prefer_local()
50+
let sm = cx.sess().source_map();
51+
let mut path = sm.filename_for_diagnostics(&sm.span_to_filename(callee))
5552
.to_string();
5653

5754
// std lib paths are <::std::module::file type>

0 commit comments

Comments
 (0)