Skip to content

Commit 4d4d478

Browse files
committed
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.
1 parent 30ae261 commit 4d4d478

File tree

6 files changed

+28
-8
lines changed

6 files changed

+28
-8
lines changed

compiler/rustc_middle/src/ty/print/pretty.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_session::config::TrimmedDefPaths;
1616
use rustc_session::cstore::{ExternCrate, ExternCrateSource};
1717
use rustc_session::Limit;
1818
use rustc_span::symbol::{kw, Ident, Symbol};
19+
use rustc_span::FileNameDisplayPreference;
1920
use rustc_target::abi::Size;
2021
use rustc_target::spec::abi::Abi;
2122
use smallvec::SmallVec;
@@ -818,11 +819,16 @@ pub trait PrettyPrinter<'tcx>:
818819
p!("@", print_def_path(did.to_def_id(), substs));
819820
} else {
820821
let span = self.tcx().def_span(did);
822+
let preference = if FORCE_TRIMMED_PATH.with(|flag| flag.get()) {
823+
FileNameDisplayPreference::Short
824+
} else {
825+
FileNameDisplayPreference::Remapped
826+
};
821827
p!(write(
822828
"@{}",
823829
// This may end up in stderr diagnostics but it may also be emitted
824830
// into MIR. Hence we use the remapped path if available
825-
self.tcx().sess.source_map().span_to_embeddable_string(span)
831+
self.tcx().sess.source_map().span_to_string(span, preference)
826832
));
827833
}
828834
} else {

compiler/rustc_span/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ impl RealFileName {
259259
FileNameDisplayPreference::Remapped => {
260260
self.remapped_path_if_available().to_string_lossy()
261261
}
262+
FileNameDisplayPreference::Short => self
263+
.local_path_if_available()
264+
.file_name()
265+
.map_or_else(|| "".into(), |f| f.to_string_lossy()),
262266
}
263267
}
264268
}
@@ -302,6 +306,9 @@ pub enum FileNameDisplayPreference {
302306
/// Display the path before the application of rewrite rules provided via `--remap-path-prefix`.
303307
/// This is appropriate for use in user-facing output (such as diagnostics).
304308
Local,
309+
/// Display only the filename, as a way to reduce the verbosity of the output.
310+
/// This is appropriate for use in user-facing output (such as diagnostics).
311+
Short,
305312
}
306313

307314
pub struct FileNameDisplay<'a> {

compiler/rustc_span/src/source_map.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -438,20 +438,27 @@ impl SourceMap {
438438
}
439439
}
440440

441-
fn span_to_string(&self, sp: Span, filename_display_pref: FileNameDisplayPreference) -> String {
441+
pub fn span_to_string(
442+
&self,
443+
sp: Span,
444+
filename_display_pref: FileNameDisplayPreference,
445+
) -> String {
442446
if self.files.borrow().source_files.is_empty() || sp.is_dummy() {
443447
return "no-location".to_string();
444448
}
445449

446450
let lo = self.lookup_char_pos(sp.lo());
447451
let hi = self.lookup_char_pos(sp.hi());
448452
format!(
449-
"{}:{}:{}: {}:{}",
453+
"{}:{}:{}{}",
450454
lo.file.name.display(filename_display_pref),
451455
lo.line,
452456
lo.col.to_usize() + 1,
453-
hi.line,
454-
hi.col.to_usize() + 1,
457+
if let FileNameDisplayPreference::Short = filename_display_pref {
458+
String::new()
459+
} else {
460+
format!(": {}:{}", hi.line, hi.col.to_usize() + 1)
461+
}
455462
)
456463
}
457464

src/test/ui/higher-rank-trait-bounds/issue-62203-hrtb-ice.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ LL | where
3030
LL | F: for<'r> T0<'r, (<Self as Ty<'r>>::V,), O = <B as Ty<'r>>::V>,
3131
| ^^^^^^^^^^^^^^^^^^^^ required by this bound in `T1::m`
3232

33-
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`
33+
error[E0271]: expected `[[email protected]:42:16]` to be a closure that returns `Unit3`, but it returns `Unit4`
3434
--> $DIR/issue-62203-hrtb-ice.rs:39:9
3535
|
3636
LL | let v = Unit2.m(

src/test/ui/issues/issue-31173.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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`
1+
error[E0271]: expected `TakeWhile<&mut IntoIter<u8>, [[email protected]:7:21]>` to be an iterator that yields `&_`, but it yields `u8`
22
--> $DIR/issue-31173.rs:11:10
33
|
44
LL | .cloned()

src/test/ui/never_type/fallback-closure-wrap.fallback.stderr

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

0 commit comments

Comments
 (0)