1
Fork 0

pretty: trim paths of unique symbols

If a symbol name can only be imported from one place for a type, and
as long as it was not glob-imported anywhere in the current crate, we
can trim its printed path and print only the name.

This has wide implications on error messages with types, for example,
shortening `std::vec::Vec` to just `Vec`, as long as there is no other
`Vec` importable anywhere.

This adds a new '-Z trim-diagnostic-paths=false' option to control this
feature.

On the good path, with no diagnosis printed, we should try to avoid
issuing this query, so we need to prevent trimmed_def_paths query on
several cases.

This change also relies on a previous commit that differentiates
between `Debug` and `Display` on various rustc types, where the latter
is trimmed and presented to the user and the former is not.
This commit is contained in:
Dan Aloni 2020-09-02 10:40:56 +03:00
parent 7b2deb5628
commit 07e7823c01
1325 changed files with 4806 additions and 4546 deletions

View file

@ -21,7 +21,7 @@ use rustc_hir_pretty::{enum_def_to_string, fn_to_string, ty_to_string};
use rustc_middle::hir::map::Map;
use rustc_middle::middle::cstore::ExternCrate;
use rustc_middle::middle::privacy::AccessLevels;
use rustc_middle::ty::{self, DefIdTree, TyCtxt};
use rustc_middle::ty::{self, print::with_no_trimmed_paths, DefIdTree, TyCtxt};
use rustc_middle::{bug, span_bug};
use rustc_session::config::{CrateType, Input, OutputType};
use rustc_session::output::{filename_for_metadata, out_filename};
@ -989,32 +989,34 @@ pub fn process_crate<'l, 'tcx, H: SaveHandler>(
config: Option<Config>,
mut handler: H,
) {
tcx.dep_graph.with_ignore(|| {
info!("Dumping crate {}", cratename);
with_no_trimmed_paths(|| {
tcx.dep_graph.with_ignore(|| {
info!("Dumping crate {}", cratename);
// Privacy checking requires and is done after type checking; use a
// fallback in case the access levels couldn't have been correctly computed.
let access_levels = match tcx.sess.compile_status() {
Ok(..) => tcx.privacy_access_levels(LOCAL_CRATE),
Err(..) => tcx.arena.alloc(AccessLevels::default()),
};
// Privacy checking requires and is done after type checking; use a
// fallback in case the access levels couldn't have been correctly computed.
let access_levels = match tcx.sess.compile_status() {
Ok(..) => tcx.privacy_access_levels(LOCAL_CRATE),
Err(..) => tcx.arena.alloc(AccessLevels::default()),
};
let save_ctxt = SaveContext {
tcx,
maybe_typeck_results: None,
access_levels: &access_levels,
span_utils: SpanUtils::new(&tcx.sess),
config: find_config(config),
impl_counter: Cell::new(0),
};
let save_ctxt = SaveContext {
tcx,
maybe_typeck_results: None,
access_levels: &access_levels,
span_utils: SpanUtils::new(&tcx.sess),
config: find_config(config),
impl_counter: Cell::new(0),
};
let mut visitor = DumpVisitor::new(save_ctxt);
let mut visitor = DumpVisitor::new(save_ctxt);
visitor.dump_crate_info(cratename, tcx.hir().krate());
visitor.dump_compilation_options(input, cratename);
visitor.process_crate(tcx.hir().krate());
visitor.dump_crate_info(cratename, tcx.hir().krate());
visitor.dump_compilation_options(input, cratename);
visitor.process_crate(tcx.hir().krate());
handler.save(&visitor.save_ctxt, &visitor.analysis())
handler.save(&visitor.save_ctxt, &visitor.analysis())
})
})
}