Path remapping: Make behavior of diagnostics output dependent on presence of --remap-path-prefix.

This commit is contained in:
Michael Woerister 2021-08-26 12:46:01 +02:00
parent dfd6306d26
commit af1b65cb18
13 changed files with 65 additions and 41 deletions

View file

@ -66,7 +66,7 @@ impl<'tcx> DebugContext<'tcx> {
rustc_interface::util::version_str().unwrap_or("unknown version"), rustc_interface::util::version_str().unwrap_or("unknown version"),
cranelift_codegen::VERSION, cranelift_codegen::VERSION,
); );
let comp_dir = tcx.sess.opts.working_dir.to_string_lossy(false).into_owned(); let comp_dir = tcx.sess.opts.working_dir.to_string_lossy(FileNameDisplayPreference::Remapped).into_owned();
let (name, file_info) = match tcx.sess.local_crate_source_file.clone() { let (name, file_info) = match tcx.sess.local_crate_source_file.clone() {
Some(path) => { Some(path) => {
let name = path.to_string_lossy().into_owned(); let name = path.to_string_lossy().into_owned();

View file

@ -74,7 +74,7 @@ mod vtable;
mod prelude { mod prelude {
pub(crate) use std::convert::{TryFrom, TryInto}; pub(crate) use std::convert::{TryFrom, TryInto};
pub(crate) use rustc_span::Span; pub(crate) use rustc_span::{Span, FileNameDisplayPreference};
pub(crate) use rustc_hir::def_id::{DefId, LOCAL_CRATE}; pub(crate) use rustc_hir::def_id::{DefId, LOCAL_CRATE};
pub(crate) use rustc_middle::bug; pub(crate) use rustc_middle::bug;

View file

@ -35,6 +35,7 @@ use rustc_middle::ty::{self, AdtKind, GeneratorSubsts, ParamEnv, Ty, TyCtxt};
use rustc_middle::{bug, span_bug}; use rustc_middle::{bug, span_bug};
use rustc_session::config::{self, DebugInfo}; use rustc_session::config::{self, DebugInfo};
use rustc_span::symbol::{Interner, Symbol}; use rustc_span::symbol::{Interner, Symbol};
use rustc_span::FileNameDisplayPreference;
use rustc_span::{self, SourceFile, SourceFileHash, Span}; use rustc_span::{self, SourceFile, SourceFileHash, Span};
use rustc_target::abi::{Abi, Align, HasDataLayout, Integer, LayoutOf, TagEncoding}; use rustc_target::abi::{Abi, Align, HasDataLayout, Integer, LayoutOf, TagEncoding};
use rustc_target::abi::{Int, Pointer, F32, F64}; use rustc_target::abi::{Int, Pointer, F32, F64};
@ -771,7 +772,13 @@ pub fn file_metadata(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll
let hash = Some(&source_file.src_hash); let hash = Some(&source_file.src_hash);
let file_name = Some(source_file.name.prefer_remapped().to_string()); let file_name = Some(source_file.name.prefer_remapped().to_string());
let directory = if source_file.is_real_file() && !source_file.is_imported() { let directory = if source_file.is_real_file() && !source_file.is_imported() {
Some(cx.sess().opts.working_dir.to_string_lossy(false).to_string()) Some(
cx.sess()
.opts
.working_dir
.to_string_lossy(FileNameDisplayPreference::Remapped)
.to_string(),
)
} else { } else {
// If the path comes from an upstream crate we assume it has been made // If the path comes from an upstream crate we assume it has been made
// independent of the compiler's working directory one way or another. // independent of the compiler's working directory one way or another.
@ -999,7 +1006,7 @@ pub fn compile_unit_metadata(
let producer = format!("clang LLVM ({})", rustc_producer); let producer = format!("clang LLVM ({})", rustc_producer);
let name_in_debuginfo = name_in_debuginfo.to_string_lossy(); let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
let work_dir = tcx.sess.opts.working_dir.to_string_lossy(false); let work_dir = tcx.sess.opts.working_dir.to_string_lossy(FileNameDisplayPreference::Remapped);
let flags = "\0"; let flags = "\0";
let output_filenames = tcx.output_filenames(()); let output_filenames = tcx.output_filenames(());
let out_dir = &output_filenames.out_directory; let out_dir = &output_filenames.out_directory;

View file

@ -126,7 +126,7 @@ impl AnnotateSnippetEmitterWriter {
} }
// owned: line source, line index, annotations // owned: line source, line index, annotations
type Owned = (String, usize, Vec<crate::snippet::Annotation>); type Owned = (String, usize, Vec<crate::snippet::Annotation>);
let filename = primary_lo.file.name.prefer_local(); let filename = source_map.filename_for_diagnostics(&primary_lo.file.name);
let origin = filename.to_string_lossy(); let origin = filename.to_string_lossy();
let annotated_files: Vec<Owned> = annotated_files let annotated_files: Vec<Owned> = annotated_files
.into_iter() .into_iter()

View file

@ -1320,7 +1320,7 @@ impl EmitterWriter {
buffer_msg_line_offset, buffer_msg_line_offset,
&format!( &format!(
"{}:{}:{}", "{}:{}:{}",
loc.file.name.prefer_local(), sm.filename_for_diagnostics(&loc.file.name),
sm.doctest_offset_line(&loc.file.name, loc.line), sm.doctest_offset_line(&loc.file.name, loc.line),
loc.col.0 + 1, loc.col.0 + 1,
), ),
@ -1334,7 +1334,7 @@ impl EmitterWriter {
0, 0,
&format!( &format!(
"{}:{}:{}: ", "{}:{}:{}: ",
loc.file.name.prefer_local(), sm.filename_for_diagnostics(&loc.file.name),
sm.doctest_offset_line(&loc.file.name, loc.line), sm.doctest_offset_line(&loc.file.name, loc.line),
loc.col.0 + 1, loc.col.0 + 1,
), ),
@ -1362,12 +1362,12 @@ impl EmitterWriter {
}; };
format!( format!(
"{}:{}{}", "{}:{}{}",
annotated_file.file.name.prefer_local(), sm.filename_for_diagnostics(&annotated_file.file.name),
sm.doctest_offset_line(&annotated_file.file.name, first_line.line_index), sm.doctest_offset_line(&annotated_file.file.name, first_line.line_index),
col col
) )
} else { } else {
format!("{}", annotated_file.file.name.prefer_local()) format!("{}", sm.filename_for_diagnostics(&annotated_file.file.name))
}; };
buffer.append(buffer_msg_line_offset + 1, &loc, Style::LineAndColumn); buffer.append(buffer_msg_line_offset + 1, &loc, Style::LineAndColumn);
for _ in 0..max_line_num_len { for _ in 0..max_line_num_len {

View file

@ -464,7 +464,7 @@ impl DiagnosticSpan {
}); });
DiagnosticSpan { DiagnosticSpan {
file_name: start.file.name.prefer_local().to_string(), file_name: je.sm.filename_for_diagnostics(&start.file.name).to_string(),
byte_start: start.file.original_relative_byte_pos(span.lo()).0, byte_start: start.file.original_relative_byte_pos(span.lo()).0,
byte_end: start.file.original_relative_byte_pos(span.hi()).0, byte_end: start.file.original_relative_byte_pos(span.hi()).0,
line_start: start.line, line_start: start.line,

View file

@ -1113,7 +1113,7 @@ impl<'a> ExtCtxt<'a> {
span, span,
&format!( &format!(
"cannot resolve relative path in non-file source `{}`", "cannot resolve relative path in non-file source `{}`",
other.prefer_local() self.source_map().filename_for_diagnostics(&other)
), ),
)); ));
} }

View file

@ -1626,14 +1626,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
(TypeError::Sorts(values), extra) => { (TypeError::Sorts(values), extra) => {
let sort_string = |ty: Ty<'tcx>| match (extra, ty.kind()) { let sort_string = |ty: Ty<'tcx>| match (extra, ty.kind()) {
(true, ty::Opaque(def_id, _)) => { (true, ty::Opaque(def_id, _)) => {
let pos = self let sm = self.tcx.sess.source_map();
.tcx let pos = sm.lookup_char_pos(self.tcx.def_span(*def_id).lo());
.sess
.source_map()
.lookup_char_pos(self.tcx.def_span(*def_id).lo());
format!( format!(
" (opaque type at <{}:{}:{}>)", " (opaque type at <{}:{}:{}>)",
pos.file.name.prefer_local(), sm.filename_for_diagnostics(&pos.file.name),
pos.line, pos.line,
pos.col.to_usize() + 1, pos.col.to_usize() + 1,
) )

View file

@ -272,11 +272,12 @@ impl<'tcx> fmt::Display for FrameInfo<'tcx> {
write!(f, "inside `{}`", self.instance)?; write!(f, "inside `{}`", self.instance)?;
} }
if !self.span.is_dummy() { if !self.span.is_dummy() {
let lo = tcx.sess.source_map().lookup_char_pos(self.span.lo()); let sm = tcx.sess.source_map();
let lo = sm.lookup_char_pos(self.span.lo());
write!( write!(
f, f,
" at {}:{}:{}", " at {}:{}:{}",
lo.file.name.prefer_local(), sm.filename_for_diagnostics(&lo.file.name),
lo.line, lo.line,
lo.col.to_usize() + 1 lo.col.to_usize() + 1
)?; )?;

View file

@ -190,7 +190,7 @@ pub fn maybe_file_to_stream(
let src = source_file.src.as_ref().unwrap_or_else(|| { let src = source_file.src.as_ref().unwrap_or_else(|| {
sess.span_diagnostic.bug(&format!( sess.span_diagnostic.bug(&format!(
"cannot lex `source_file` without source: {}", "cannot lex `source_file` without source: {}",
source_file.name.prefer_local() sess.source_map().filename_for_diagnostics(&source_file.name)
)); ));
}); });

View file

@ -260,11 +260,12 @@ impl RealFileName {
} }
} }
pub fn to_string_lossy(&self, prefer_local: bool) -> Cow<'_, str> { pub fn to_string_lossy(&self, display_pref: FileNameDisplayPreference) -> Cow<'_, str> {
if prefer_local { match display_pref {
self.local_path_if_available().to_string_lossy() FileNameDisplayPreference::Local => self.local_path_if_available().to_string_lossy(),
} else { FileNameDisplayPreference::Remapped => {
self.remapped_path_if_available().to_string_lossy() self.remapped_path_if_available().to_string_lossy()
}
} }
} }
} }
@ -300,9 +301,15 @@ impl From<PathBuf> for FileName {
} }
} }
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
pub enum FileNameDisplayPreference {
Remapped,
Local,
}
pub struct FileNameDisplay<'a> { pub struct FileNameDisplay<'a> {
inner: &'a FileName, inner: &'a FileName,
prefer_local: bool, display_pref: FileNameDisplayPreference,
} }
impl fmt::Display for FileNameDisplay<'_> { impl fmt::Display for FileNameDisplay<'_> {
@ -310,7 +317,7 @@ impl fmt::Display for FileNameDisplay<'_> {
use FileName::*; use FileName::*;
match *self.inner { match *self.inner {
Real(ref name) => { Real(ref name) => {
write!(fmt, "{}", name.to_string_lossy(self.prefer_local)) write!(fmt, "{}", name.to_string_lossy(self.display_pref))
} }
QuoteExpansion(_) => write!(fmt, "<quote expansion>"), QuoteExpansion(_) => write!(fmt, "<quote expansion>"),
MacroExpansion(_) => write!(fmt, "<macro expansion>"), MacroExpansion(_) => write!(fmt, "<macro expansion>"),
@ -328,7 +335,7 @@ impl fmt::Display for FileNameDisplay<'_> {
impl FileNameDisplay<'_> { impl FileNameDisplay<'_> {
pub fn to_string_lossy(&self) -> Cow<'_, str> { pub fn to_string_lossy(&self) -> Cow<'_, str> {
match self.inner { match self.inner {
FileName::Real(ref inner) => inner.to_string_lossy(self.prefer_local), FileName::Real(ref inner) => inner.to_string_lossy(self.display_pref),
_ => Cow::from(format!("{}", self)), _ => Cow::from(format!("{}", self)),
} }
} }
@ -352,13 +359,17 @@ impl FileName {
} }
pub fn prefer_remapped(&self) -> FileNameDisplay<'_> { pub fn prefer_remapped(&self) -> FileNameDisplay<'_> {
FileNameDisplay { inner: self, prefer_local: false } FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Remapped }
} }
// This may include transient local filesystem information. // This may include transient local filesystem information.
// Must not be embedded in build outputs. // Must not be embedded in build outputs.
pub fn prefer_local(&self) -> FileNameDisplay<'_> { pub fn prefer_local(&self) -> FileNameDisplay<'_> {
FileNameDisplay { inner: self, prefer_local: true } FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Local }
}
pub fn display(&self, display_pref: FileNameDisplayPreference) -> FileNameDisplay<'_> {
FileNameDisplay { inner: self, display_pref }
} }
pub fn macro_expansion_source_code(src: &str) -> FileName { pub fn macro_expansion_source_code(src: &str) -> FileName {

View file

@ -427,7 +427,7 @@ impl SourceMap {
} }
} }
fn span_to_string(&self, sp: Span, prefer_local: bool) -> String { fn span_to_string(&self, sp: Span, filename_display_pref: FileNameDisplayPreference) -> String {
if self.files.borrow().source_files.is_empty() || sp.is_dummy() { if self.files.borrow().source_files.is_empty() || sp.is_dummy() {
return "no-location".to_string(); return "no-location".to_string();
} }
@ -436,7 +436,7 @@ impl SourceMap {
let hi = self.lookup_char_pos(sp.hi()); let hi = self.lookup_char_pos(sp.hi());
format!( format!(
"{}:{}:{}: {}:{}", "{}:{}:{}: {}:{}",
if prefer_local { lo.file.name.prefer_local() } else { lo.file.name.prefer_remapped() }, lo.file.name.display(filename_display_pref),
lo.line, lo.line,
lo.col.to_usize() + 1, lo.col.to_usize() + 1,
hi.line, hi.line,
@ -446,20 +446,24 @@ impl SourceMap {
/// Format the span location suitable for embedding in build artifacts /// Format the span location suitable for embedding in build artifacts
pub fn span_to_embeddable_string(&self, sp: Span) -> String { pub fn span_to_embeddable_string(&self, sp: Span) -> String {
self.span_to_string(sp, false) self.span_to_string(sp, FileNameDisplayPreference::Remapped)
} }
/// Format the span location to be printed in diagnostics. Must not be emitted /// Format the span location to be printed in diagnostics. Must not be emitted
/// to build artifacts as this may leak local file paths. Use span_to_embeddable_string /// to build artifacts as this may leak local file paths. Use span_to_embeddable_string
/// for string suitable for embedding. /// for string suitable for embedding.
pub fn span_to_diagnostic_string(&self, sp: Span) -> String { pub fn span_to_diagnostic_string(&self, sp: Span) -> String {
self.span_to_string(sp, true) self.span_to_string(sp, self.path_mapping.filename_display_for_diagnostics)
} }
pub fn span_to_filename(&self, sp: Span) -> FileName { pub fn span_to_filename(&self, sp: Span) -> FileName {
self.lookup_char_pos(sp.lo()).file.name.clone() self.lookup_char_pos(sp.lo()).file.name.clone()
} }
pub fn filename_for_diagnostics<'a>(&self, filename: &'a FileName) -> FileNameDisplay<'a> {
filename.display(self.path_mapping.filename_display_for_diagnostics)
}
pub fn is_multiline(&self, sp: Span) -> bool { pub fn is_multiline(&self, sp: Span) -> bool {
let lo = self.lookup_source_file_idx(sp.lo()); let lo = self.lookup_source_file_idx(sp.lo());
let hi = self.lookup_source_file_idx(sp.hi()); let hi = self.lookup_source_file_idx(sp.hi());
@ -1002,15 +1006,22 @@ impl SourceMap {
#[derive(Clone)] #[derive(Clone)]
pub struct FilePathMapping { pub struct FilePathMapping {
mapping: Vec<(PathBuf, PathBuf)>, mapping: Vec<(PathBuf, PathBuf)>,
filename_display_for_diagnostics: FileNameDisplayPreference,
} }
impl FilePathMapping { impl FilePathMapping {
pub fn empty() -> FilePathMapping { pub fn empty() -> FilePathMapping {
FilePathMapping { mapping: vec![] } FilePathMapping::new(Vec::new())
} }
pub fn new(mapping: Vec<(PathBuf, PathBuf)>) -> FilePathMapping { pub fn new(mapping: Vec<(PathBuf, PathBuf)>) -> FilePathMapping {
FilePathMapping { mapping } let filename_display_for_diagnostics = if mapping.is_empty() {
FileNameDisplayPreference::Local
} else {
FileNameDisplayPreference::Remapped
};
FilePathMapping { mapping, filename_display_for_diagnostics }
} }
/// Applies any path prefix substitution as defined by the mapping. /// Applies any path prefix substitution as defined by the mapping.

View file

@ -47,11 +47,8 @@ pub struct MacroRefData {
impl MacroRefData { impl MacroRefData {
pub fn new(name: String, callee: Span, cx: &LateContext<'_>) -> Self { pub fn new(name: String, callee: Span, cx: &LateContext<'_>) -> Self {
let mut path = cx let sm = cx.sess().source_map();
.sess() let mut path = sm.filename_for_diagnostics(&sm.span_to_filename(callee))
.source_map()
.span_to_filename(callee)
.prefer_local()
.to_string(); .to_string();
// std lib paths are <::std::module::file type> // std lib paths are <::std::module::file type>