Skip to content

Commit 0433fdf

Browse files
committed
Auto merge of rust-lang#76843 - kornelski:longtypetofile, r=ecstatic-morse
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 rust-lang#76777
2 parents a409a23 + 34d3c7d commit 0433fdf

17 files changed

+71
-40
lines changed

compiler/rustc_mir/src/monomorphize/collector.rs

+40-26
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ use rustc_session::config::EntryFnType;
197197
use rustc_span::source_map::{dummy_spanned, respan, Span, Spanned, DUMMY_SP};
198198
use smallvec::SmallVec;
199199
use std::iter;
200+
use std::path::PathBuf;
200201

201202
#[derive(PartialEq)]
202203
pub enum MonoItemCollectionMode {
@@ -420,27 +421,38 @@ fn record_accesses<'a, 'tcx: 'a>(
420421
inlining_map.lock_mut().record_accesses(caller, &accesses);
421422
}
422423

423-
// Shrinks string by keeping prefix and suffix of given sizes.
424-
fn shrink(s: String, before: usize, after: usize) -> String {
425-
// An iterator of all byte positions including the end of the string.
426-
let positions = || s.char_indices().map(|(i, _)| i).chain(iter::once(s.len()));
427-
428-
let shrunk = format!(
429-
"{before}...{after}",
430-
before = &s[..positions().nth(before).unwrap_or(s.len())],
431-
after = &s[positions().rev().nth(after).unwrap_or(0)..],
432-
);
424+
/// Format instance name that is already known to be too long for rustc.
425+
/// Show only the first and last 32 characters to avoid blasting
426+
/// the user's terminal with thousands of lines of type-name.
427+
///
428+
/// If the type name is longer than before+after, it will be written to a file.
429+
fn shrunk_instance_name(
430+
tcx: TyCtxt<'tcx>,
431+
instance: &Instance<'tcx>,
432+
before: usize,
433+
after: usize,
434+
) -> (String, Option<PathBuf>) {
435+
let s = instance.to_string();
433436

434437
// Only use the shrunk version if it's really shorter.
435438
// This also avoids the case where before and after slices overlap.
436-
if shrunk.len() < s.len() { shrunk } else { s }
437-
}
439+
if s.chars().nth(before + after + 1).is_some() {
440+
// An iterator of all byte positions including the end of the string.
441+
let positions = || s.char_indices().map(|(i, _)| i).chain(iter::once(s.len()));
442+
443+
let shrunk = format!(
444+
"{before}...{after}",
445+
before = &s[..positions().nth(before).unwrap_or(s.len())],
446+
after = &s[positions().rev().nth(after).unwrap_or(0)..],
447+
);
448+
449+
let path = tcx.output_filenames(LOCAL_CRATE).temp_path_ext("long-type.txt", None);
450+
let written_to_path = std::fs::write(&path, s).ok().map(|_| path);
438451

439-
// Format instance name that is already known to be too long for rustc.
440-
// Show only the first and last 32 characters to avoid blasting
441-
// the user's terminal with thousands of lines of type-name.
442-
fn shrunk_instance_name(instance: &Instance<'tcx>) -> String {
443-
shrink(instance.to_string(), 32, 32)
452+
(shrunk, written_to_path)
453+
} else {
454+
(s, None)
455+
}
444456
}
445457

446458
fn check_recursion_limit<'tcx>(
@@ -465,15 +477,16 @@ fn check_recursion_limit<'tcx>(
465477
// more than the recursion limit is assumed to be causing an
466478
// infinite expansion.
467479
if !tcx.sess.recursion_limit().value_within_limit(adjusted_recursion_depth) {
468-
let error = format!(
469-
"reached the recursion limit while instantiating `{}`",
470-
shrunk_instance_name(&instance),
471-
);
480+
let (shrunk, written_to_path) = shrunk_instance_name(tcx, &instance, 32, 32);
481+
let error = format!("reached the recursion limit while instantiating `{}`", shrunk);
472482
let mut err = tcx.sess.struct_span_fatal(span, &error);
473483
err.span_note(
474484
tcx.def_span(def_id),
475485
&format!("`{}` defined here", tcx.def_path_str(def_id)),
476486
);
487+
if let Some(path) = written_to_path {
488+
err.note(&format!("the full type name has been written to '{}'", path.display()));
489+
}
477490
err.emit();
478491
FatalError.raise();
479492
}
@@ -502,12 +515,13 @@ fn check_type_length_limit<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) {
502515
//
503516
// Bail out in these cases to avoid that bad user experience.
504517
if !tcx.sess.type_length_limit().value_within_limit(type_length) {
505-
let msg = format!(
506-
"reached the type-length limit while instantiating `{}`",
507-
shrunk_instance_name(&instance),
508-
);
518+
let (shrunk, written_to_path) = shrunk_instance_name(tcx, &instance, 32, 32);
519+
let msg = format!("reached the type-length limit while instantiating `{}`", shrunk);
509520
let mut diag = tcx.sess.struct_span_fatal(tcx.def_span(instance.def_id()), &msg);
510-
diag.note(&format!(
521+
if let Some(path) = written_to_path {
522+
diag.note(&format!("the full type name has been written to '{}'", path.display()));
523+
}
524+
diag.help(&format!(
511525
"consider adding a `#![type_length_limit=\"{}\"]` attribute to your crate",
512526
type_length
513527
));

src/test/ui/infinite/infinite-instantiation.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// build-fail
2+
// normalize-stderr-test: ".nll/" -> "/"
23

34
trait ToOpt: Sized {
45
fn to_option(&self) -> Option<Self>;
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
error: reached the recursion limit while instantiating `function::<Option<Option<Option<...>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>`
2-
--> $DIR/infinite-instantiation.rs:21:9
2+
--> $DIR/infinite-instantiation.rs:22:9
33
|
44
LL | function(counter - 1, t.to_option());
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: `function` defined here
8-
--> $DIR/infinite-instantiation.rs:19:1
8+
--> $DIR/infinite-instantiation.rs:20:1
99
|
1010
LL | fn function<T:ToOpt + Clone>(counter: usize, t: T) {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
= note: the full type name has been written to '$TEST_BUILD_DIR/infinite/infinite-instantiation/infinite-instantiation.long-type.txt'
1213

1314
error: aborting due to previous error
1415

src/test/ui/issues/issue-22638.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// build-fail
22
// normalize-stderr-test: "<\[closure@.+`" -> "$$CLOSURE`"
3+
// normalize-stderr-test: ".nll/" -> "/"
34

45
#![allow(unused)]
56

src/test/ui/issues/issue-22638.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
error: reached the recursion limit while instantiating `A::matches::$CLOSURE`
2-
--> $DIR/issue-22638.rs:55:9
2+
--> $DIR/issue-22638.rs:56:9
33
|
44
LL | a.matches(f)
55
| ^^^^^^^^^^^^
66
|
77
note: `A::matches` defined here
8-
--> $DIR/issue-22638.rs:14:5
8+
--> $DIR/issue-22638.rs:15:5
99
|
1010
LL | pub fn matches<F: Fn()>(&self, f: &F) {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
= note: the full type name has been written to '$TEST_BUILD_DIR/issues/issue-22638/issue-22638.long-type.txt'
1213

1314
error: aborting due to previous error
1415

src/test/ui/issues/issue-37311-type-length-limit/issue-37311.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// build-fail
2+
// normalize-stderr-test: ".nll/" -> "/"
23

34
trait Mirror {
45
type Image;
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
error: reached the recursion limit while instantiating `<(&(&(&(&(&(&(&(&(&(&(&(&(&(&(&(.....), ...), ...) as Foo>::recurse`
2-
--> $DIR/issue-37311.rs:16:9
2+
--> $DIR/issue-37311.rs:17:9
33
|
44
LL | (self, self).recurse();
55
| ^^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: `<T as Foo>::recurse` defined here
8-
--> $DIR/issue-37311.rs:15:5
8+
--> $DIR/issue-37311.rs:16:5
99
|
1010
LL | fn recurse(&self) {
1111
| ^^^^^^^^^^^^^^^^^
12+
= 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'
1213

1314
error: aborting due to previous error
1415

src/test/ui/issues/issue-67552.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// build-fail
2+
// normalize-stderr-test: ".nll/" -> "/"
23

34
fn main() {
45
rec(Empty);

src/test/ui/issues/issue-67552.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
error: reached the recursion limit while instantiating `rec::<&mut &mut &mut &mut &mut &... &mut &mut &mut &mut &mut Empty>`
2-
--> $DIR/issue-67552.rs:27:9
2+
--> $DIR/issue-67552.rs:28:9
33
|
44
LL | rec(identity(&mut it))
55
| ^^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: `rec` defined here
8-
--> $DIR/issue-67552.rs:20:1
8+
--> $DIR/issue-67552.rs:21:1
99
|
1010
LL | / fn rec<T>(mut it: T)
1111
LL | | where
1212
LL | | T: Iterator,
1313
| |________________^
14+
= note: the full type name has been written to '$TEST_BUILD_DIR/issues/issue-67552/issue-67552.long-type.txt'
1415

1516
error: aborting due to previous error
1617

src/test/ui/issues/issue-8727.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// recursions.
33

44
// build-fail
5+
// normalize-stderr-test: ".nll/" -> "/"
56

67
fn generic<T>() { //~ WARN function cannot return without recursing
78
generic::<Option<T>>();

src/test/ui/issues/issue-8727.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: function cannot return without recursing
2-
--> $DIR/issue-8727.rs:6:1
2+
--> $DIR/issue-8727.rs:7:1
33
|
44
LL | fn generic<T>() {
55
| ^^^^^^^^^^^^^^^ cannot return without recursing
@@ -10,16 +10,17 @@ LL | generic::<Option<T>>();
1010
= help: a `loop` may express intention better if this is on purpose
1111

1212
error: reached the recursion limit while instantiating `generic::<Option<Option<Option<O...>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>`
13-
--> $DIR/issue-8727.rs:7:5
13+
--> $DIR/issue-8727.rs:8:5
1414
|
1515
LL | generic::<Option<T>>();
1616
| ^^^^^^^^^^^^^^^^^^^^^^
1717
|
1818
note: `generic` defined here
19-
--> $DIR/issue-8727.rs:6:1
19+
--> $DIR/issue-8727.rs:7:1
2020
|
2121
LL | fn generic<T>() {
2222
| ^^^^^^^^^^^^^^^
23+
= note: the full type name has been written to '$TEST_BUILD_DIR/issues/issue-8727/issue-8727.long-type.txt'
2324

2425
error: aborting due to previous error; 1 warning emitted
2526

src/test/ui/recursion/issue-38591-non-regular-dropck-recursion.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// no free regions or type parameters.
33
// Codegen however, has to error for the infinitely many `drop_in_place`
44
// functions it has been asked to create.
5+
56
// build-fail
7+
// normalize-stderr-test: ".nll/" -> "/"
68

79
struct S<T> {
810
t: T,

src/test/ui/recursion/issue-38591-non-regular-dropck-recursion.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ LL | | // SAFETY: see comment above
2121
LL | | unsafe { drop_in_place(to_drop) }
2222
LL | | }
2323
| |_^
24+
= 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'
2425

2526
error: aborting due to previous error
2627

src/test/ui/recursion/recursion.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// build-fail
22
// compile-flags:-C overflow-checks=off
3+
// normalize-stderr-test: ".nll/" -> "/"
34

45
enum Nil {NilValue}
56
struct Cons<T> {head:isize, tail:T}
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
error: reached the recursion limit while instantiating `test::<Cons<Cons<Cons<Cons<Cons<...>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>`
2-
--> $DIR/recursion.rs:17:11
2+
--> $DIR/recursion.rs:18:11
33
|
44
LL | _ => {test (n-1, i+1, Cons {head:2*i+1, tail:first}, Cons{head:i*i, tail:second})}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: `test` defined here
8-
--> $DIR/recursion.rs:15:1
8+
--> $DIR/recursion.rs:16:1
99
|
1010
LL | fn test<T:Dot> (n:isize, i:isize, first:T, second:T) ->isize {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
= note: the full type name has been written to '$TEST_BUILD_DIR/recursion/recursion/recursion.long-type.txt'
1213

1314
error: aborting due to previous error
1415

src/test/ui/type_length_limit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// build-fail
22
// error-pattern: reached the type-length limit while instantiating
3+
// normalize-stderr-test: ".nll/" -> "/"
34

45
// Test that the type length limit can be changed.
56

src/test/ui/type_length_limit.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ error: reached the type-length limit while instantiating `std::mem::drop::<Optio
44
LL | pub fn drop<T>(_x: T) {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: consider adding a `#![type_length_limit="8"]` attribute to your crate
7+
= note: the full type name has been written to '$TEST_BUILD_DIR/type_length_limit/type_length_limit.long-type.txt'
8+
= help: consider adding a `#![type_length_limit="8"]` attribute to your crate
89

910
error: aborting due to previous error
1011

0 commit comments

Comments
 (0)