Skip to content

Commit 07e7823

Browse files
committed
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.
1 parent 7b2deb5 commit 07e7823

File tree

1,325 files changed

+4808
-4548
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,325 files changed

+4808
-4548
lines changed

compiler/rustc_codegen_llvm/src/type_of.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::type_::Type;
44
use rustc_codegen_ssa::traits::*;
55
use rustc_middle::bug;
66
use rustc_middle::ty::layout::{FnAbiExt, TyAndLayout};
7+
use rustc_middle::ty::print::with_no_trimmed_paths;
78
use rustc_middle::ty::{self, Ty, TypeFoldable};
89
use rustc_target::abi::{Abi, AddressSpace, Align, FieldsShape};
910
use rustc_target::abi::{Int, Pointer, F32, F64};
@@ -57,7 +58,7 @@ fn uncached_llvm_type<'a, 'tcx>(
5758
ty::Adt(..) | ty::Closure(..) | ty::Foreign(..) | ty::Generator(..) | ty::Str
5859
if !cx.sess().fewer_names() =>
5960
{
60-
let mut name = layout.ty.to_string();
61+
let mut name = with_no_trimmed_paths(|| layout.ty.to_string());
6162
if let (&ty::Adt(def, _), &Variants::Single { index }) =
6263
(&layout.ty.kind, &layout.variants)
6364
{

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_middle::mir;
1616
use rustc_middle::mir::interpret::{AllocId, ConstValue, Pointer, Scalar};
1717
use rustc_middle::mir::AssertKind;
1818
use rustc_middle::ty::layout::{FnAbiExt, HasTyCtxt};
19+
use rustc_middle::ty::print::with_no_trimmed_paths;
1920
use rustc_middle::ty::{self, Instance, Ty, TypeFoldable};
2021
use rustc_span::source_map::Span;
2122
use rustc_span::{sym, Symbol};
@@ -479,14 +480,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
479480
UninitValid => !layout.might_permit_raw_init(bx, /*zero:*/ false).unwrap(),
480481
};
481482
if do_panic {
482-
let msg_str = if layout.abi.is_uninhabited() {
483-
// Use this error even for the other intrinsics as it is more precise.
484-
format!("attempted to instantiate uninhabited type `{}`", ty)
485-
} else if intrinsic == ZeroValid {
486-
format!("attempted to zero-initialize type `{}`, which is invalid", ty)
487-
} else {
488-
format!("attempted to leave type `{}` uninitialized, which is invalid", ty)
489-
};
483+
let msg_str = with_no_trimmed_paths(|| {
484+
if layout.abi.is_uninhabited() {
485+
// Use this error even for the other intrinsics as it is more precise.
486+
format!("attempted to instantiate uninhabited type `{}`", ty)
487+
} else if intrinsic == ZeroValid {
488+
format!("attempted to zero-initialize type `{}`, which is invalid", ty)
489+
} else {
490+
format!("attempted to leave type `{}` uninitialized, which is invalid", ty)
491+
}
492+
});
490493
let msg = bx.const_str(Symbol::intern(&msg_str));
491494
let location = self.get_caller_location(bx, span).immediate();
492495

compiler/rustc_driver/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use rustc_save_analysis as save;
3434
use rustc_save_analysis::DumpHandler;
3535
use rustc_serialize::json::{self, ToJson};
3636
use rustc_session::config::nightly_options;
37-
use rustc_session::config::{ErrorOutputType, Input, OutputType, PrintRequest};
37+
use rustc_session::config::{ErrorOutputType, Input, OutputType, PrintRequest, TrimmedDefPaths};
3838
use rustc_session::getopts;
3939
use rustc_session::lint::{Lint, LintId};
4040
use rustc_session::{config, DiagnosticOutput, Session};
@@ -159,7 +159,10 @@ pub fn run_compiler(
159159
None => return Ok(()),
160160
};
161161

162-
let sopts = config::build_session_options(&matches);
162+
let sopts = config::Options {
163+
trimmed_def_paths: TrimmedDefPaths::GoodPath,
164+
..config::build_session_options(&matches)
165+
};
163166
let cfg = interface::parse_cfgspecs(matches.opt_strs("cfg"));
164167

165168
let mut dummy_config = |sopts, cfg, diagnostic_output| {

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ fn test_debugging_options_tracking_hash() {
518518
untracked!(time_llvm_passes, true);
519519
untracked!(time_passes, true);
520520
untracked!(trace_macros, true);
521+
untracked!(trim_diagnostic_paths, false);
521522
untracked!(ui_testing, true);
522523
untracked!(unpretty, Some("expanded".to_string()));
523524
untracked!(unstable_options, true);

compiler/rustc_lint/src/context.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use rustc_middle::lint::LintDiagnosticBuilder;
3131
use rustc_middle::middle::privacy::AccessLevels;
3232
use rustc_middle::middle::stability;
3333
use rustc_middle::ty::layout::{LayoutError, TyAndLayout};
34+
use rustc_middle::ty::print::with_no_trimmed_paths;
3435
use rustc_middle::ty::{self, print::Printer, subst::GenericArg, Ty, TyCtxt};
3536
use rustc_session::lint::{add_elided_lifetime_in_path_suggestion, BuiltinLintDiagnostics};
3637
use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintId};
@@ -795,10 +796,12 @@ impl<'tcx> LateContext<'tcx> {
795796
}
796797

797798
// This shouldn't ever be needed, but just in case:
798-
Ok(vec![match trait_ref {
799-
Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)),
800-
None => Symbol::intern(&format!("<{}>", self_ty)),
801-
}])
799+
with_no_trimmed_paths(|| {
800+
Ok(vec![match trait_ref {
801+
Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)),
802+
None => Symbol::intern(&format!("<{}>", self_ty)),
803+
}])
804+
})
802805
}
803806

804807
fn path_append_impl(
@@ -812,12 +815,16 @@ impl<'tcx> LateContext<'tcx> {
812815

813816
// This shouldn't ever be needed, but just in case:
814817
path.push(match trait_ref {
815-
Some(trait_ref) => Symbol::intern(&format!(
816-
"<impl {} for {}>",
817-
trait_ref.print_only_trait_path(),
818-
self_ty
819-
)),
820-
None => Symbol::intern(&format!("<impl {}>", self_ty)),
818+
Some(trait_ref) => with_no_trimmed_paths(|| {
819+
Symbol::intern(&format!(
820+
"<impl {} for {}>",
821+
trait_ref.print_only_trait_path(),
822+
self_ty
823+
))
824+
}),
825+
None => {
826+
with_no_trimmed_paths(|| Symbol::intern(&format!("<impl {}>", self_ty)))
827+
}
821828
});
822829

823830
Ok(path)

compiler/rustc_macros/src/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ fn add_query_description_impl(
392392
#tcx: TyCtxt<'tcx>,
393393
#key: #arg,
394394
) -> Cow<'static, str> {
395-
format!(#desc).into()
395+
::rustc_middle::ty::print::with_no_trimmed_paths(|| format!(#desc).into())
396396
}
397397
};
398398

compiler/rustc_middle/src/middle/stability.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_hir as hir;
1313
use rustc_hir::def::DefKind;
1414
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX};
1515
use rustc_hir::{self, HirId};
16+
use rustc_middle::ty::print::with_no_trimmed_paths;
1617
use rustc_session::lint::builtin::{DEPRECATED, DEPRECATED_IN_FUTURE, SOFT_UNSTABLE};
1718
use rustc_session::lint::{BuiltinLintDiagnostics, Lint, LintBuffer};
1819
use rustc_session::parse::feature_err_issue;
@@ -308,7 +309,7 @@ impl<'tcx> TyCtxt<'tcx> {
308309
// #[rustc_deprecated] however wants to emit down the whole
309310
// hierarchy.
310311
if !skip || depr_entry.attr.is_since_rustc_version {
311-
let path = &self.def_path_str(def_id);
312+
let path = &with_no_trimmed_paths(|| self.def_path_str(def_id));
312313
let kind = self.def_kind(def_id).descr(def_id);
313314
let (message, lint) = deprecation_message(&depr_entry.attr, kind, path);
314315
late_report_deprecation(

compiler/rustc_middle/src/mir/interpret/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ use rustc_data_structures::sync::{HashMapExt, Lock};
108108
use rustc_data_structures::tiny_list::TinyList;
109109
use rustc_hir::def_id::DefId;
110110
use rustc_macros::HashStable;
111+
use rustc_middle::ty::print::with_no_trimmed_paths;
111112
use rustc_serialize::{Decodable, Encodable};
112113
use rustc_target::abi::{Endian, Size};
113114

@@ -145,7 +146,7 @@ pub struct GlobalId<'tcx> {
145146

146147
impl GlobalId<'tcx> {
147148
pub fn display(self, tcx: TyCtxt<'tcx>) -> String {
148-
let instance_name = tcx.def_path_str(self.instance.def.def_id());
149+
let instance_name = with_no_trimmed_paths(|| tcx.def_path_str(self.instance.def.def_id()));
149150
if let Some(promoted) = self.promoted {
150151
format!("{}::{:?}", instance_name, promoted)
151152
} else {

compiler/rustc_middle/src/query/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,11 @@ rustc_queries! {
12551255
storage(ArenaCacheSelector<'tcx>)
12561256
desc { "calculating the visible parent map" }
12571257
}
1258+
query trimmed_def_paths(_: CrateNum)
1259+
-> FxHashMap<DefId, Symbol> {
1260+
storage(ArenaCacheSelector<'tcx>)
1261+
desc { "calculating trimmed def paths" }
1262+
}
12581263
query missing_extern_crate_item(_: CrateNum) -> bool {
12591264
eval_always
12601265
desc { "seeing if we're missing an `extern crate` item for this crate" }

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ pub struct GlobalCtxt<'tcx> {
943943
maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
944944
/// A map of glob use to a set of names it actually imports. Currently only
945945
/// used in save-analysis.
946-
glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
946+
pub(crate) glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
947947
/// Extern prelude entries. The value is `true` if the entry was introduced
948948
/// via `extern crate` item and not `--extern` option or compiler built-in.
949949
pub extern_prelude: FxHashMap<Symbol, bool>,

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3101,6 +3101,7 @@ pub fn provide(providers: &mut ty::query::Providers) {
31013101
erase_regions::provide(providers);
31023102
layout::provide(providers);
31033103
util::provide(providers);
3104+
print::provide(providers);
31043105
super::util::bug::provide(providers);
31053106
*providers = ty::query::Providers {
31063107
trait_impls_of: trait_def::trait_impls_of_provider,

0 commit comments

Comments
 (0)