Skip to content

Commit

Permalink
Rollup merge of #76843 - kornelski:longtypetofile, r=ecstatic-morse
Browse files Browse the repository at this point in the history
Let user see the full type of type-length limit error

Seeing the full type of the error is sometimes essential to diagnosing the problem, but the type itself is too long to be displayed in the terminal in a useful fashion. This change solves this dilemma by writing the full offending type name to a file, and displays this filename as a note.

> note: the full type name been written to '$TEST_BUILD_DIR/issues/issue-22638/issue-22638.long-type.txt'

Closes #76777
  • Loading branch information
RalfJung authored Sep 20, 2020
2 parents 0342f8a + c5968e8 commit 484e207
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 27 deletions.
66 changes: 40 additions & 26 deletions compiler/rustc_mir/src/monomorphize/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ use rustc_session::config::EntryFnType;
use rustc_span::source_map::{dummy_spanned, respan, Span, Spanned, DUMMY_SP};
use smallvec::SmallVec;
use std::iter;
use std::path::PathBuf;

#[derive(PartialEq)]
pub enum MonoItemCollectionMode {
Expand Down Expand Up @@ -418,27 +419,38 @@ fn record_accesses<'a, 'tcx: 'a>(
inlining_map.lock_mut().record_accesses(caller, &accesses);
}

// Shrinks string by keeping prefix and suffix of given sizes.
fn shrink(s: String, before: usize, after: usize) -> String {
// 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)..],
);
/// Format instance name that 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.
///
/// If the type name is longer than before+after, it will be written to a file.
fn shrunk_instance_name(
tcx: TyCtxt<'tcx>,
instance: &Instance<'tcx>,
before: usize,
after: usize,
) -> (String, Option<PathBuf>) {
let s = instance.to_string();

// 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 }
}
if s.chars().nth(before + after + 1).is_some() {
// 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)..],
);

let path = tcx.output_filenames(LOCAL_CRATE).temp_path_ext("long-type.txt", None);
let written_to_path = std::fs::write(&path, s).ok().map(|_| path);

// Format instance name that 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.
fn shrunk_instance_name(instance: &Instance<'tcx>) -> String {
shrink(instance.to_string(), 32, 32)
(shrunk, written_to_path)
} else {
(s, None)
}
}

fn check_recursion_limit<'tcx>(
Expand All @@ -463,15 +475,16 @@ fn check_recursion_limit<'tcx>(
// more than the recursion limit is assumed to be causing an
// infinite expansion.
if !tcx.sess.recursion_limit().value_within_limit(adjusted_recursion_depth) {
let error = format!(
"reached the recursion limit while instantiating `{}`",
shrunk_instance_name(&instance),
);
let (shrunk, written_to_path) = shrunk_instance_name(tcx, &instance, 32, 32);
let error = format!("reached the recursion limit while instantiating `{}`", shrunk);
let mut err = tcx.sess.struct_span_fatal(span, &error);
err.span_note(
tcx.def_span(def_id),
&format!("`{}` defined here", tcx.def_path_str(def_id)),
);
if let Some(path) = written_to_path {
err.note(&format!("the full type name has been written to '{}'", path.display()));
}
err.emit();
FatalError.raise();
}
Expand Down Expand Up @@ -500,12 +513,13 @@ fn check_type_length_limit<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) {
//
// Bail out in these cases to avoid that bad user experience.
if !tcx.sess.type_length_limit().value_within_limit(type_length) {
let msg = format!(
"reached the type-length limit while instantiating `{}`",
shrunk_instance_name(&instance),
);
let (shrunk, written_to_path) = shrunk_instance_name(tcx, &instance, 32, 32);
let msg = format!("reached the type-length limit while instantiating `{}`", shrunk);
let mut diag = tcx.sess.struct_span_fatal(tcx.def_span(instance.def_id()), &msg);
diag.note(&format!(
if let Some(path) = written_to_path {
diag.note(&format!("the full type name has been written to '{}'", path.display()));
}
diag.help(&format!(
"consider adding a `#![type_length_limit=\"{}\"]` attribute to your crate",
type_length
));
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/infinite/infinite-instantiation.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ note: `function` defined here
|
LL | fn function<T:ToOpt + Clone>(counter: usize, t: T) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: the full type name has been written to '$TEST_BUILD_DIR/infinite/infinite-instantiation/infinite-instantiation.long-type.txt'

error: aborting due to previous error

1 change: 1 addition & 0 deletions src/test/ui/issues/issue-22638.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ note: `A::matches` defined here
|
LL | pub fn matches<F: Fn()>(&self, f: &F) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: the full type name has been written to '$TEST_BUILD_DIR/issues/issue-22638/issue-22638.long-type.txt'

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ note: `<T as Foo>::recurse` defined here
|
LL | fn recurse(&self) {
| ^^^^^^^^^^^^^^^^^
= note: the full type name has been written to '$TEST_BUILD_DIR/issues/issue-37311-type-length-limit/issue-37311/issue-37311.long-type.txt'

error: aborting due to previous error

1 change: 1 addition & 0 deletions src/test/ui/issues/issue-67552.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ LL | / fn rec<T>(mut it: T)
LL | | where
LL | | T: Iterator,
| |________________^
= note: the full type name has been written to '$TEST_BUILD_DIR/issues/issue-67552/issue-67552.long-type.txt'

error: aborting due to previous error

1 change: 1 addition & 0 deletions src/test/ui/issues/issue-8727.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ note: `generic` defined here
|
LL | fn generic<T>() {
| ^^^^^^^^^^^^^^^
= note: the full type name has been written to '$TEST_BUILD_DIR/issues/issue-8727/issue-8727.long-type.txt'

error: aborting due to previous error; 1 warning emitted

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ LL | | // SAFETY: see comment above
LL | | unsafe { drop_in_place(to_drop) }
LL | | }
| |_^
= note: the full type name has been written to '$TEST_BUILD_DIR/recursion/issue-38591-non-regular-dropck-recursion/issue-38591-non-regular-dropck-recursion.long-type.txt'

error: aborting due to previous error

1 change: 1 addition & 0 deletions src/test/ui/recursion/recursion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ note: `test` defined here
|
LL | fn test<T:Dot> (n:isize, i:isize, first:T, second:T) ->isize {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: the full type name has been written to '$TEST_BUILD_DIR/recursion/recursion/recursion.long-type.txt'

error: aborting due to previous error

3 changes: 2 additions & 1 deletion src/test/ui/type_length_limit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ error: reached the type-length limit while instantiating `std::mem::drop::<Optio
LL | pub fn drop<T>(_x: T) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: consider adding a `#![type_length_limit="8"]` attribute to your crate
= note: the full type name has been written to '$TEST_BUILD_DIR/type_length_limit/type_length_limit.long-type.txt'
= help: consider adding a `#![type_length_limit="8"]` attribute to your crate

error: aborting due to previous error

0 comments on commit 484e207

Please sign in to comment.