1
Fork 0

rustc_mir: adjust the type_length_limit diagnostic to be more useful.

This commit is contained in:
Eduard-Mihai Burtescu 2018-12-19 06:39:33 +02:00
parent 3e1cef7007
commit 6ca6c1a6cc
6 changed files with 45 additions and 25 deletions

View file

@ -3,7 +3,7 @@ use crate::hir::map::DefPathData;
use crate::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; use crate::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use crate::ty::{self, DefIdTree, Ty, TyCtxt}; use crate::ty::{self, DefIdTree, Ty, TyCtxt};
use crate::ty::print::PrintCx; use crate::ty::print::PrintCx;
use crate::ty::subst::{Subst, Substs}; use crate::ty::subst::{Subst, SubstsRef};
use crate::middle::cstore::{ExternCrate, ExternCrateSource}; use crate::middle::cstore::{ExternCrate, ExternCrateSource};
use syntax::ast; use syntax::ast;
use syntax::symbol::{keywords, Symbol}; use syntax::symbol::{keywords, Symbol};
@ -79,7 +79,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub fn item_path_str_with_substs_and_ns( pub fn item_path_str_with_substs_and_ns(
self, self,
def_id: DefId, def_id: DefId,
substs: Option<&Substs<'tcx>>, substs: Option<SubstsRef<'tcx>>,
ns: Namespace, ns: Namespace,
) -> String { ) -> String {
debug!("item_path_str: def_id={:?}, substs={:?}, ns={:?}", def_id, substs, ns); 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( pub fn default_print_item_path(
&mut self, &mut self,
def_id: DefId, def_id: DefId,
substs: Option<&Substs<'tcx>>, substs: Option<SubstsRef<'tcx>>,
ns: Namespace, ns: Namespace,
) -> P::Path { ) -> P::Path {
debug!("default_print_item_path: def_id={:?}, substs={:?}, ns={:?}", def_id, substs, ns); debug!("default_print_item_path: def_id={:?}, substs={:?}, ns={:?}", def_id, substs, ns);
@ -169,7 +169,7 @@ impl<P: ItemPathPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
fn default_print_impl_path( fn default_print_impl_path(
&mut self, &mut self,
impl_def_id: DefId, impl_def_id: DefId,
substs: Option<&Substs<'tcx>>, substs: Option<SubstsRef<'tcx>>,
ns: Namespace, ns: Namespace,
) -> P::Path { ) -> P::Path {
debug!("default_print_impl_path: impl_def_id={:?}", impl_def_id); debug!("default_print_impl_path: impl_def_id={:?}", impl_def_id);
@ -314,7 +314,7 @@ pub trait ItemPathPrinter: Sized {
fn print_item_path( fn print_item_path(
self: &mut PrintCx<'_, '_, 'tcx, Self>, self: &mut PrintCx<'_, '_, 'tcx, Self>,
def_id: DefId, def_id: DefId,
substs: Option<&Substs<'tcx>>, substs: Option<SubstsRef<'tcx>>,
ns: Namespace, ns: Namespace,
) -> Self::Path { ) -> Self::Path {
self.default_print_item_path(def_id, substs, ns) self.default_print_item_path(def_id, substs, ns)
@ -322,7 +322,7 @@ pub trait ItemPathPrinter: Sized {
fn print_impl_path( fn print_impl_path(
self: &mut PrintCx<'_, '_, 'tcx, Self>, self: &mut PrintCx<'_, '_, 'tcx, Self>,
impl_def_id: DefId, impl_def_id: DefId,
substs: Option<&Substs<'tcx>>, substs: Option<SubstsRef<'tcx>>,
ns: Namespace, ns: Namespace,
) -> Self::Path { ) -> Self::Path {
self.default_print_impl_path(impl_def_id, substs, ns) self.default_print_impl_path(impl_def_id, substs, ns)
@ -506,7 +506,7 @@ impl ItemPathPrinter for LocalPathPrinter {
fn print_item_path( fn print_item_path(
self: &mut PrintCx<'_, '_, 'tcx, Self>, self: &mut PrintCx<'_, '_, 'tcx, Self>,
def_id: DefId, def_id: DefId,
substs: Option<&Substs<'tcx>>, substs: Option<SubstsRef<'tcx>>,
ns: Namespace, ns: Namespace,
) -> Self::Path { ) -> Self::Path {
self.try_print_visible_item_path(def_id, ns) self.try_print_visible_item_path(def_id, ns)
@ -515,7 +515,7 @@ impl ItemPathPrinter for LocalPathPrinter {
fn print_impl_path( fn print_impl_path(
self: &mut PrintCx<'_, '_, 'tcx, Self>, self: &mut PrintCx<'_, '_, 'tcx, Self>,
impl_def_id: DefId, impl_def_id: DefId,
substs: Option<&Substs<'tcx>>, substs: Option<SubstsRef<'tcx>>,
ns: Namespace, ns: Namespace,
) -> Self::Path { ) -> Self::Path {
// Always use types for non-local impls, where types are always // Always use types for non-local impls, where types are always

View file

@ -198,6 +198,8 @@ use crate::monomorphize::item::{MonoItemExt, DefPathBasedNames, InstantiationMod
use rustc_data_structures::bit_set::GrowableBitSet; use rustc_data_structures::bit_set::GrowableBitSet;
use rustc_data_structures::sync::{MTRef, MTLock, ParallelIterator, par_iter}; use rustc_data_structures::sync::{MTRef, MTLock, ParallelIterator, par_iter};
use std::iter;
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)] #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub enum MonoItemCollectionMode { pub enum MonoItemCollectionMode {
Eager, 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 // We include the const length in the type length, as it's better
// to be overly conservative. // to be overly conservative.
if type_length + const_length > type_length_limit { if type_length + const_length > type_length_limit {
// The instance name is already known to be too long for rustc. Use // The instance name is already known to be too long for rustc.
// `{:.64}` to avoid blasting the user's terminal with thousands of // Show only the first and last 32 characters to avoid blasting
// lines of type-name. // the user's terminal with thousands of lines of type-name.
let instance_name = instance.to_string(); let shrink = |s: String, before: usize, after: usize| {
let msg = format!("reached the type-length limit while instantiating `{:.64}...`", // An iterator of all byte positions including the end of the string.
instance_name); let positions = || s.char_indices().map(|(i, _)| i).chain(iter::once(s.len()));
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)
};
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!( diag.note(&format!(
"consider adding a `#![type_length_limit=\"{}\"]` attribute to your crate", "consider adding a `#![type_length_limit=\"{}\"]` attribute to your crate",
type_length_limit * 2)); type_length));
diag.emit(); diag.emit();
tcx.sess.abort_if_errors(); tcx.sess.abort_if_errors();
} }

View file

@ -8,7 +8,7 @@ LL | | a.matches(f)
LL | | } 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 error: aborting due to previous error

View 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 --> $DIR/issue-37311.rs:13:5
| |
LL | / fn recurse(&self) { LL | / fn recurse(&self) {
@ -6,7 +6,7 @@ LL | | (self, self).recurse();
LL | | } 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 error: aborting due to previous error

View file

@ -1,3 +1,5 @@
// ignore-musl
// ignore-x86
// error-pattern: reached the type-length limit while instantiating // error-pattern: reached the type-length limit while instantiating
// Test that the type length limit can be changed. // Test that the type length limit can be changed.

View 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 error: aborting due to previous error