about summary refs log tree commit diff
path: root/compiler/rustc_session/src
diff options
context:
space:
mode:
authorDan Aloni <alonid@gmail.com>2020-09-02 10:40:56 +0300
committerDan Aloni <alonid@gmail.com>2020-09-02 22:26:37 +0300
commit07e7823c01be1733df2480de19fbbe6b8e9384cf (patch)
tree9e4ff1075680201f9c72b58bf780638ef1fcede3 /compiler/rustc_session/src
parent7b2deb562822112cc30d23958e7459564e2c6ef9 (diff)
downloadrust-07e7823c01be1733df2480de19fbbe6b8e9384cf.tar.gz
rust-07e7823c01be1733df2480de19fbbe6b8e9384cf.zip
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.
Diffstat (limited to 'compiler/rustc_session/src')
-rw-r--r--compiler/rustc_session/src/config.rs22
-rw-r--r--compiler/rustc_session/src/options.rs5
2 files changed, 26 insertions, 1 deletions
diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs
index 6861314a88f..65377b08820 100644
--- a/compiler/rustc_session/src/config.rs
+++ b/compiler/rustc_session/src/config.rs
@@ -328,6 +328,23 @@ impl Default for ErrorOutputType {
     }
 }
 
+/// Parameter to control path trimming.
+#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
+pub enum TrimmedDefPaths {
+    /// `try_print_trimmed_def_path` never prints a trimmed path and never calls the expensive query
+    Never,
+    /// `try_print_trimmed_def_path` calls the expensive query, the query doesn't call `delay_good_path_bug`
+    Always,
+    /// `try_print_trimmed_def_path` calls the expensive query, the query calls `delay_good_path_bug`
+    GoodPath,
+}
+
+impl Default for TrimmedDefPaths {
+    fn default() -> Self {
+        Self::Never
+    }
+}
+
 /// Use tree-based collections to cheaply get a deterministic `Hash` implementation.
 /// *Do not* switch `BTreeMap` out for an unsorted container type! That would break
 /// dependency tracking for command-line arguments.
@@ -621,6 +638,7 @@ impl Default for Options {
             unstable_features: UnstableFeatures::Disallow,
             debug_assertions: true,
             actually_rustdoc: false,
+            trimmed_def_paths: TrimmedDefPaths::default(),
             cli_forced_codegen_units: None,
             cli_forced_thinlto_off: false,
             remap_path_prefix: Vec::new(),
@@ -1811,6 +1829,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
         unstable_features: UnstableFeatures::from_environment(),
         debug_assertions,
         actually_rustdoc: false,
+        trimmed_def_paths: TrimmedDefPaths::default(),
         cli_forced_codegen_units: codegen_units,
         cli_forced_thinlto_off: disable_thinlto,
         remap_path_prefix,
@@ -2057,7 +2076,7 @@ crate mod dep_tracking {
     use super::{
         CFGuard, CrateType, DebugInfo, ErrorOutputType, LinkerPluginLto, LtoCli, OptLevel,
         OutputTypes, Passes, SanitizerSet, SourceFileHashAlgorithm, SwitchWithOptPath,
-        SymbolManglingVersion,
+        SymbolManglingVersion, TrimmedDefPaths,
     };
     use crate::lint;
     use crate::utils::NativeLibKind;
@@ -2138,6 +2157,7 @@ crate mod dep_tracking {
     impl_dep_tracking_hash_via_hash!(SwitchWithOptPath);
     impl_dep_tracking_hash_via_hash!(SymbolManglingVersion);
     impl_dep_tracking_hash_via_hash!(Option<SourceFileHashAlgorithm>);
+    impl_dep_tracking_hash_via_hash!(TrimmedDefPaths);
 
     impl_dep_tracking_hash_for_sortable_vec_of!(String);
     impl_dep_tracking_hash_for_sortable_vec_of!(PathBuf);
diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs
index a28c17917df..f502d43a0e0 100644
--- a/compiler/rustc_session/src/options.rs
+++ b/compiler/rustc_session/src/options.rs
@@ -125,6 +125,9 @@ top_level_options!(
         // try to not rely on this too much.
         actually_rustdoc: bool [TRACKED],
 
+        // Control path trimming.
+        trimmed_def_paths: TrimmedDefPaths [TRACKED],
+
         // Specifications of codegen units / ThinLTO which are forced as a
         // result of parsing command line options. These are not necessarily
         // what rustc was invoked with, but massaged a bit to agree with
@@ -1084,6 +1087,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
         "for every macro invocation, print its name and arguments (default: no)"),
     treat_err_as_bug: Option<usize> = (None, parse_treat_err_as_bug, [TRACKED],
         "treat error number `val` that occurs as bug"),
+    trim_diagnostic_paths: bool = (true, parse_bool, [UNTRACKED],
+        "in diagnostics, use heuristics to shorten paths referring to items"),
     ui_testing: bool = (false, parse_bool, [UNTRACKED],
         "emit compiler diagnostics in a form suitable for UI testing (default: no)"),
     unleash_the_miri_inside_of_you: bool = (false, parse_bool, [TRACKED],