Skip to content

Commit

Permalink
Rollup merge of rust-lang#89232 - rossmacarthur:fix-76424, r=wesleywiser
Browse files Browse the repository at this point in the history
Improve help for recursion limit errors

- Tweak help message and suggested limit (handle `0` case).
- Add test for rust-lang#75602 (it was already fixed, maybe can be resolved too).

Fixes rust-lang#76424
  • Loading branch information
GuillaumeGomez authored Sep 29, 2021
2 parents 905b9eb + d2613fb commit d21761a
Show file tree
Hide file tree
Showing 26 changed files with 75 additions and 35 deletions.
8 changes: 6 additions & 2 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,14 +633,18 @@ impl<'a, 'b> MacroExpander<'a, 'b> {

fn error_recursion_limit_reached(&mut self) {
let expn_data = self.cx.current_expansion.id.expn_data();
let suggested_limit = self.cx.ecfg.recursion_limit * 2;
let suggested_limit = match self.cx.ecfg.recursion_limit {
Limit(0) => Limit(2),
limit => limit * 2,
};
self.cx
.struct_span_err(
expn_data.call_site,
&format!("recursion limit reached while expanding `{}`", expn_data.kind.descr()),
)
.help(&format!(
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate (`{}`)",
"consider increasing the recursion limit by adding a \
`#![recursion_limit = \"{}\"]` attribute to your crate (`{}`)",
suggested_limit, self.cx.ecfg.crate_name,
))
.emit();
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_trait_selection/src/autoderef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_hir as hir;
use rustc_infer::infer::InferCtxt;
use rustc_middle::ty::{self, TraitRef, Ty, TyCtxt, WithConstness};
use rustc_middle::ty::{ToPredicate, TypeFoldable};
use rustc_session::DiagnosticMessageId;
use rustc_session::{DiagnosticMessageId, Limit};
use rustc_span::def_id::LOCAL_CRATE;
use rustc_span::Span;

Expand Down Expand Up @@ -217,7 +217,10 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {

pub fn report_autoderef_recursion_limit_error<'tcx>(tcx: TyCtxt<'tcx>, span: Span, ty: Ty<'tcx>) {
// We've reached the recursion limit, error gracefully.
let suggested_limit = tcx.recursion_limit() * 2;
let suggested_limit = match tcx.recursion_limit() {
Limit(0) => Limit(2),
limit => limit * 2,
};
let msg = format!("reached the recursion limit while auto-dereferencing `{:?}`", ty);
let error_id = (DiagnosticMessageId::ErrorId(55), Some(span), msg);
let fresh = tcx.sess.one_time_diagnostics.borrow_mut().insert(error_id);
Expand All @@ -231,7 +234,8 @@ pub fn report_autoderef_recursion_limit_error<'tcx>(tcx: TyCtxt<'tcx>, span: Spa
)
.span_label(span, "deref recursion limit reached")
.help(&format!(
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate (`{}`)",
"consider increasing the recursion limit by adding a \
`#![recursion_limit = \"{}\"]` attribute to your crate (`{}`)",
suggested_limit,
tcx.crate_name(LOCAL_CRATE),
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use rustc_middle::ty::{
Infer, InferTy, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness,
};
use rustc_middle::ty::{TypeAndMut, TypeckResults};
use rustc_session::Limit;
use rustc_span::def_id::LOCAL_CRATE;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{BytePos, DesugaringKind, ExpnKind, ForLoopLoc, MultiSpan, Span, DUMMY_SP};
Expand Down Expand Up @@ -2426,10 +2427,13 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
}

fn suggest_new_overflow_limit(&self, err: &mut DiagnosticBuilder<'_>) {
let current_limit = self.tcx.recursion_limit();
let suggested_limit = current_limit * 2;
let suggested_limit = match self.tcx.recursion_limit() {
Limit(0) => Limit(2),
limit => limit * 2,
};
err.help(&format!(
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate (`{}`)",
"consider increasing the recursion limit by adding a \
`#![recursion_limit = \"{}\"]` attribute to your crate (`{}`)",
suggested_limit,
self.tcx.crate_name(LOCAL_CRATE),
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LL | | type U = str;
LL | | }
| |_^
|
= help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`hr_associated_type_bound_2`)
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`hr_associated_type_bound_2`)
note: required because of the requirements on the impl of `for<'b> X<'b>` for `u32`
--> $DIR/hr-associated-type-bound-2.rs:11:6
|
Expand All @@ -24,7 +24,7 @@ error[E0275]: overflow evaluating the requirement `for<'b> u32: X<'b>`
LL | type U = str;
| ^^^^^^^^^^^^^
|
= help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`hr_associated_type_bound_2`)
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`hr_associated_type_bound_2`)
note: required because of the requirements on the impl of `for<'b> X<'b>` for `u32`
--> $DIR/hr-associated-type-bound-2.rs:11:6
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/autoref-autoderef/issue-38940.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0055]: reached the recursion limit while auto-dereferencing `J`
LL | let x: &Bottom = &t;
| ^^ deref recursion limit reached
|
= help: consider adding a `#![recursion_limit="20"]` attribute to your crate (`issue_38940`)
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "20"]` attribute to your crate (`issue_38940`)

error[E0308]: mismatched types
--> $DIR/issue-38940.rs:43:22
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/did_you_mean/recursion_limit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0275]: overflow evaluating the requirement `K: Send`
LL | is_send::<A>();
| ^^^^^^^^^^^^
|
= help: consider adding a `#![recursion_limit="20"]` attribute to your crate (`recursion_limit`)
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "20"]` attribute to your crate (`recursion_limit`)
note: required because it appears within the type `J`
--> $DIR/recursion_limit.rs:24:9
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/did_you_mean/recursion_limit_deref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0055]: reached the recursion limit while auto-dereferencing `J`
LL | let x: &Bottom = &t;
| ^^ deref recursion limit reached
|
= help: consider adding a `#![recursion_limit="20"]` attribute to your crate (`recursion_limit_deref`)
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "20"]` attribute to your crate (`recursion_limit_deref`)

error[E0308]: mismatched types
--> $DIR/recursion_limit_deref.rs:50:22
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/did_you_mean/recursion_limit_macro.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | ($t:tt $($tail:tt)*) => { recurse!($($tail)*) };
LL | recurse!(0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9);
| -------------------------------------------------- in this macro invocation
|
= help: consider adding a `#![recursion_limit="20"]` attribute to your crate (`recursion_limit_macro`)
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "20"]` attribute to your crate (`recursion_limit_macro`)
= note: this error originates in the macro `recurse` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0055.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0055]: reached the recursion limit while auto-dereferencing `Foo`
LL | ref_foo.foo();
| ^^^ deref recursion limit reached
|
= help: consider adding a `#![recursion_limit="8"]` attribute to your crate (`E0055`)
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "8"]` attribute to your crate (`E0055`)

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0275.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0275]: overflow evaluating the requirement `Bar<Bar<Bar<Bar<Bar<Bar<Bar<B
LL | impl<T> Foo for T where Bar<T>: Foo {}
| ^^^
|
= help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`E0275`)
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`E0275`)
note: required because of the requirements on the impl of `Foo` for `Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<Bar<T>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>`
--> $DIR/E0275.rs:5:9
|
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/infinite/infinite-autoderef.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ error[E0055]: reached the recursion limit while auto-dereferencing `Foo`
LL | Foo.foo;
| ^^^^^^^ deref recursion limit reached
|
= help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`infinite_autoderef`)
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`infinite_autoderef`)

error[E0055]: reached the recursion limit while auto-dereferencing `Foo`
--> $DIR/infinite-autoderef.rs:25:9
|
LL | Foo.foo;
| ^^^ deref recursion limit reached
|
= help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`infinite_autoderef`)
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`infinite_autoderef`)

error[E0609]: no field `foo` on type `Foo`
--> $DIR/infinite-autoderef.rs:25:9
Expand All @@ -34,7 +34,7 @@ error[E0055]: reached the recursion limit while auto-dereferencing `Foo`
LL | Foo.bar();
| ^^^ deref recursion limit reached
|
= help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`infinite_autoderef`)
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`infinite_autoderef`)

error[E0599]: no method named `bar` found for struct `Foo` in the current scope
--> $DIR/infinite-autoderef.rs:26:9
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/infinite/infinite-macro-expansion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | () => (recursive!())
LL | recursive!()
| ------------ in this macro invocation
|
= help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`infinite_macro_expansion`)
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`infinite_macro_expansion`)
= note: this error originates in the macro `recursive` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-16098.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | $n + prob1!($n - 1);
LL | println!("Problem 1: {}", prob1!(1000));
| ------------ in this macro invocation
|
= help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`issue_16098`)
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`issue_16098`)
= note: this error originates in the macro `prob1` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-18400.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0275]: overflow evaluating the requirement `_: Sized`
LL | 0.contains(bits);
| ^^^^^^^^
|
= help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`issue_18400`)
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`issue_18400`)
note: required because of the requirements on the impl of `Set<&[_]>` for `{integer}`
--> $DIR/issue-18400.rs:6:16
|
Expand Down
Loading

0 comments on commit d21761a

Please sign in to comment.