]> git.lizzy.rs Git - rust.git/commitdiff
rustc_mir: adjust the type_length_limit diagnostic to be more useful.
authorEduard-Mihai Burtescu <edy.burt@gmail.com>
Wed, 19 Dec 2018 04:39:33 +0000 (06:39 +0200)
committerEduard-Mihai Burtescu <edy.burt@gmail.com>
Fri, 15 Mar 2019 11:25:10 +0000 (13:25 +0200)
src/librustc/ty/item_path.rs
src/librustc_mir/monomorphize/collector.rs
src/test/ui/issues/issue-22638.stderr
src/test/ui/issues/issue-37311-type-length-limit/issue-37311.stderr
src/test/ui/type_length_limit.rs
src/test/ui/type_length_limit.stderr

index d9a8deb80e46ac1a798937926e5121c571b3c547..288894f235b718f060537daa8f2bf8dcd7f8b7f6 100644 (file)
@@ -3,7 +3,7 @@
 use crate::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
 use crate::ty::{self, DefIdTree, Ty, TyCtxt};
 use crate::ty::print::PrintCx;
-use crate::ty::subst::{Subst, Substs};
+use crate::ty::subst::{Subst, SubstsRef};
 use crate::middle::cstore::{ExternCrate, ExternCrateSource};
 use syntax::ast;
 use syntax::symbol::{keywords, Symbol};
@@ -79,7 +79,7 @@ fn guess_def_namespace(self, def_id: DefId) -> Namespace {
     pub fn item_path_str_with_substs_and_ns(
         self,
         def_id: DefId,
-        substs: Option<&Substs<'tcx>>,
+        substs: Option<SubstsRef<'tcx>>,
         ns: Namespace,
     ) -> String {
         debug!("item_path_str: def_id={:?}, substs={:?}, ns={:?}", def_id, substs, ns);
@@ -116,7 +116,7 @@ impl<P: ItemPathPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
     pub fn default_print_item_path(
         &mut self,
         def_id: DefId,
-        substs: Option<&Substs<'tcx>>,
+        substs: Option<SubstsRef<'tcx>>,
         ns: Namespace,
     ) -> P::Path {
         debug!("default_print_item_path: def_id={:?}, substs={:?}, ns={:?}", def_id, substs, ns);
@@ -169,7 +169,7 @@ pub fn default_print_item_path(
     fn default_print_impl_path(
         &mut self,
         impl_def_id: DefId,
-        substs: Option<&Substs<'tcx>>,
+        substs: Option<SubstsRef<'tcx>>,
         ns: Namespace,
     ) -> P::Path {
         debug!("default_print_impl_path: impl_def_id={:?}", impl_def_id);
@@ -314,7 +314,7 @@ pub trait ItemPathPrinter: Sized {
     fn print_item_path(
         self: &mut PrintCx<'_, '_, 'tcx, Self>,
         def_id: DefId,
-        substs: Option<&Substs<'tcx>>,
+        substs: Option<SubstsRef<'tcx>>,
         ns: Namespace,
     ) -> Self::Path {
         self.default_print_item_path(def_id, substs, ns)
@@ -322,7 +322,7 @@ fn print_item_path(
     fn print_impl_path(
         self: &mut PrintCx<'_, '_, 'tcx, Self>,
         impl_def_id: DefId,
-        substs: Option<&Substs<'tcx>>,
+        substs: Option<SubstsRef<'tcx>>,
         ns: Namespace,
     ) -> Self::Path {
         self.default_print_impl_path(impl_def_id, substs, ns)
@@ -506,7 +506,7 @@ impl ItemPathPrinter for LocalPathPrinter {
     fn print_item_path(
         self: &mut PrintCx<'_, '_, 'tcx, Self>,
         def_id: DefId,
-        substs: Option<&Substs<'tcx>>,
+        substs: Option<SubstsRef<'tcx>>,
         ns: Namespace,
     ) -> Self::Path {
         self.try_print_visible_item_path(def_id, ns)
@@ -515,7 +515,7 @@ fn print_item_path(
     fn print_impl_path(
         self: &mut PrintCx<'_, '_, 'tcx, Self>,
         impl_def_id: DefId,
-        substs: Option<&Substs<'tcx>>,
+        substs: Option<SubstsRef<'tcx>>,
         ns: Namespace,
     ) -> Self::Path {
         // Always use types for non-local impls, where types are always
index bfdf34f3e37e49a5c938bb184004a0e74992d568..307cee5d97217c1ae55e730bc5421574ad94a9ce 100644 (file)
 use rustc_data_structures::bit_set::GrowableBitSet;
 use rustc_data_structures::sync::{MTRef, MTLock, ParallelIterator, par_iter};
 
+use std::iter;
+
 #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
 pub enum MonoItemCollectionMode {
     Eager,
@@ -487,21 +489,33 @@ fn check_type_length_limit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
     // We include the const length in the type length, as it's better
     // to be overly conservative.
     if type_length + const_length > type_length_limit {
-        // The instance name is already known to be too long for rustc. Use
-        // `{:.64}` to avoid blasting the user's terminal with thousands of
-        // lines of type-name.
-        let instance_name = instance.to_string();
-        let msg = format!("reached the type-length limit while instantiating `{:.64}...`",
-                          instance_name);
-        let mut diag = if let Some(hir_id) = tcx.hir().as_local_hir_id(instance.def_id()) {
-            tcx.sess.struct_span_fatal(tcx.hir().span_by_hir_id(hir_id), &msg)
-        } else {
-            tcx.sess.struct_fatal(&msg)
+        // The instance name is already known to be too long for rustc.
+        // Show only the first and last 32 characters to avoid blasting
+        // the user's terminal with thousands of lines of type-name.
+        let shrink = |s: String, before: usize, after: usize| {
+            // An iterator of all byte positions including the end of the string.
+            let positions = || s.char_indices().map(|(i, _)| i).chain(iter::once(s.len()));
+
+            let shrunk = format!(
+                "{before}...{after}",
+                before = &s[..positions().nth(before).unwrap_or(s.len())],
+                after = &s[positions().rev().nth(after).unwrap_or(0)..],
+            );
+
+            // Only use the shrunk version if it's really shorter.
+            // This also avoids the case where before and after slices overlap.
+            if shrunk.len() < s.len() {
+                shrunk
+            } else {
+                s
+            }
         };
-
+        let msg = format!("reached the type-length limit while instantiating `{}`",
+                          shrink(instance.to_string(), 32, 32));
+        let mut diag = tcx.sess.struct_span_fatal(tcx.def_span(instance.def_id()), &msg);
         diag.note(&format!(
             "consider adding a `#![type_length_limit=\"{}\"]` attribute to your crate",
-            type_length_limit * 2));
+            type_length));
         diag.emit();
         tcx.sess.abort_if_errors();
     }
index aff968f3618c1edb8da02f3fdcbf0214d4d66e4c..b60e1c29ec0ee1eac4d1e1f88c9b3b9c0e0407db 100644 (file)
@@ -8,7 +8,7 @@ LL | |         a.matches(f)
 LL | |     }
    | |_____^
    |
-   = note: consider adding a `#![type_length_limit="40000000"]` attribute to your crate
+   = note: consider adding a `#![type_length_limit="26214380"]` attribute to your crate
 
 error: aborting due to previous error
 
index c5432190412297a55c372d3a50040289e750908e..aead415d23f84bf2875b33c1c0a6c0f85aacd289 100644 (file)
@@ -1,4 +1,4 @@
-error: reached the type-length limit while instantiating `<(&(&(&(&(&(&(&(&(&(&(&(&(&(&(&(&(&(&(&(), &()), &(&(), &())), &...`
+error: reached the type-length limit while instantiating `<(&(&(&(&(&(&(&(&(&(&(&(&(&(&(&(...))))))))))))))) as Foo>::recurse`
   --> $DIR/issue-37311.rs:13:5
    |
 LL | /     fn recurse(&self) {
@@ -6,7 +6,7 @@ LL | |         (self, self).recurse();
 LL | |     }
    | |_____^
    |
-   = note: consider adding a `#![type_length_limit="2097152"]` attribute to your crate
+   = note: consider adding a `#![type_length_limit="2097149"]` attribute to your crate
 
 error: aborting due to previous error
 
index bb54669d37df2b529aa60afd9a150d3d29a4caaf..cd15f81a615355dda6dae175ceded2755aa4177d 100644 (file)
@@ -1,3 +1,5 @@
+// ignore-musl
+// ignore-x86
 // error-pattern: reached the type-length limit while instantiating
 
 // Test that the type length limit can be changed.
index 910eca075948a8b732d2671a6f50193e87ccb8b5..9d07c86356b67ed80ad99176621926cfe57fd0f5 100644 (file)
@@ -1,6 +1,10 @@
-error: reached the type-length limit while instantiating `std::mem::drop::<std::option::Option<((((((G, G, G), (G, G, G), ...`
+error: reached the type-length limit while instantiating `std::mem::drop::<std::option::Op... G), (G, G, G), (G, G, G))))))>>`
+  --> $SRC_DIR/libcore/mem.rs:LL:COL
    |
-   = note: consider adding a `#![type_length_limit="512"]` attribute to your crate
+LL | pub fn drop<T>(_x: T) { }
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: consider adding a `#![type_length_limit="1094"]` attribute to your crate
 
 error: aborting due to previous error