Shorten trimmed display of closures

When `with_forced_trimmed_paths` is used, only print filename and start
of the closure's span, to reduce their verbosity.
This commit is contained in:
Esteban Küber 2022-12-13 16:34:04 -08:00
parent 30ae261c42
commit 4d4d4786f9
6 changed files with 28 additions and 8 deletions

View file

@ -16,6 +16,7 @@ use rustc_session::config::TrimmedDefPaths;
use rustc_session::cstore::{ExternCrate, ExternCrateSource}; use rustc_session::cstore::{ExternCrate, ExternCrateSource};
use rustc_session::Limit; use rustc_session::Limit;
use rustc_span::symbol::{kw, Ident, Symbol}; use rustc_span::symbol::{kw, Ident, Symbol};
use rustc_span::FileNameDisplayPreference;
use rustc_target::abi::Size; use rustc_target::abi::Size;
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
use smallvec::SmallVec; use smallvec::SmallVec;
@ -818,11 +819,16 @@ pub trait PrettyPrinter<'tcx>:
p!("@", print_def_path(did.to_def_id(), substs)); p!("@", print_def_path(did.to_def_id(), substs));
} else { } else {
let span = self.tcx().def_span(did); let span = self.tcx().def_span(did);
let preference = if FORCE_TRIMMED_PATH.with(|flag| flag.get()) {
FileNameDisplayPreference::Short
} else {
FileNameDisplayPreference::Remapped
};
p!(write( p!(write(
"@{}", "@{}",
// This may end up in stderr diagnostics but it may also be emitted // This may end up in stderr diagnostics but it may also be emitted
// into MIR. Hence we use the remapped path if available // into MIR. Hence we use the remapped path if available
self.tcx().sess.source_map().span_to_embeddable_string(span) self.tcx().sess.source_map().span_to_string(span, preference)
)); ));
} }
} else { } else {

View file

@ -259,6 +259,10 @@ impl RealFileName {
FileNameDisplayPreference::Remapped => { FileNameDisplayPreference::Remapped => {
self.remapped_path_if_available().to_string_lossy() self.remapped_path_if_available().to_string_lossy()
} }
FileNameDisplayPreference::Short => self
.local_path_if_available()
.file_name()
.map_or_else(|| "".into(), |f| f.to_string_lossy()),
} }
} }
} }
@ -302,6 +306,9 @@ pub enum FileNameDisplayPreference {
/// Display the path before the application of rewrite rules provided via `--remap-path-prefix`. /// Display the path before the application of rewrite rules provided via `--remap-path-prefix`.
/// This is appropriate for use in user-facing output (such as diagnostics). /// This is appropriate for use in user-facing output (such as diagnostics).
Local, Local,
/// Display only the filename, as a way to reduce the verbosity of the output.
/// This is appropriate for use in user-facing output (such as diagnostics).
Short,
} }
pub struct FileNameDisplay<'a> { pub struct FileNameDisplay<'a> {

View file

@ -438,7 +438,11 @@ impl SourceMap {
} }
} }
fn span_to_string(&self, sp: Span, filename_display_pref: FileNameDisplayPreference) -> String { pub 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();
} }
@ -446,12 +450,15 @@ impl SourceMap {
let lo = self.lookup_char_pos(sp.lo()); let lo = self.lookup_char_pos(sp.lo());
let hi = self.lookup_char_pos(sp.hi()); let hi = self.lookup_char_pos(sp.hi());
format!( format!(
"{}:{}:{}: {}:{}", "{}:{}:{}{}",
lo.file.name.display(filename_display_pref), lo.file.name.display(filename_display_pref),
lo.line, lo.line,
lo.col.to_usize() + 1, lo.col.to_usize() + 1,
hi.line, if let FileNameDisplayPreference::Short = filename_display_pref {
hi.col.to_usize() + 1, String::new()
} else {
format!(": {}:{}", hi.line, hi.col.to_usize() + 1)
}
) )
} }

View file

@ -30,7 +30,7 @@ LL | where
LL | F: for<'r> T0<'r, (<Self as Ty<'r>>::V,), O = <B as Ty<'r>>::V>, LL | F: for<'r> T0<'r, (<Self as Ty<'r>>::V,), O = <B as Ty<'r>>::V>,
| ^^^^^^^^^^^^^^^^^^^^ required by this bound in `T1::m` | ^^^^^^^^^^^^^^^^^^^^ required by this bound in `T1::m`
error[E0271]: expected `[closure@$DIR/issue-62203-hrtb-ice.rs:42:16: 42:19]` to be a closure that returns `Unit3`, but it returns `Unit4` error[E0271]: expected `[closure@issue-62203-hrtb-ice.rs:42:16]` to be a closure that returns `Unit3`, but it returns `Unit4`
--> $DIR/issue-62203-hrtb-ice.rs:39:9 --> $DIR/issue-62203-hrtb-ice.rs:39:9
| |
LL | let v = Unit2.m( LL | let v = Unit2.m(

View file

@ -1,4 +1,4 @@
error[E0271]: expected `TakeWhile<&mut IntoIter<u8>, [closure@$DIR/issue-31173.rs:7:21: 7:25]>` to be an iterator that yields `&_`, but it yields `u8` error[E0271]: expected `TakeWhile<&mut IntoIter<u8>, [closure@issue-31173.rs:7:21]>` to be an iterator that yields `&_`, but it yields `u8`
--> $DIR/issue-31173.rs:11:10 --> $DIR/issue-31173.rs:11:10
| |
LL | .cloned() LL | .cloned()

View file

@ -1,4 +1,4 @@
error[E0271]: expected `[closure@$DIR/fallback-closure-wrap.rs:18:40: 18:47]` to be a closure that returns `()`, but it returns `!` error[E0271]: expected `[closure@fallback-closure-wrap.rs:18:40]` to be a closure that returns `()`, but it returns `!`
--> $DIR/fallback-closure-wrap.rs:18:31 --> $DIR/fallback-closure-wrap.rs:18:31
| |
LL | let error = Closure::wrap(Box::new(move || { LL | let error = Closure::wrap(Box::new(move || {