Skip to content

Commit b2cdca0

Browse files
committed
respect the tcx's recursion limit when peeling
1 parent 120cf4c commit b2cdca0

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

compiler/rustc_hir_analysis/src/autoderef.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use rustc_infer::infer::InferCtxt;
22
use rustc_infer::traits::PredicateObligations;
33
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
44
use rustc_session::Limit;
5-
use rustc_span::Span;
65
use rustc_span::def_id::{LOCAL_CRATE, LocalDefId};
6+
use rustc_span::{ErrorGuaranteed, Span};
77
use rustc_trait_selection::traits::ObligationCtxt;
88
use tracing::{debug, instrument};
99

@@ -259,7 +259,11 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
259259
}
260260
}
261261

262-
pub fn report_autoderef_recursion_limit_error<'tcx>(tcx: TyCtxt<'tcx>, span: Span, ty: Ty<'tcx>) {
262+
pub fn report_autoderef_recursion_limit_error<'tcx>(
263+
tcx: TyCtxt<'tcx>,
264+
span: Span,
265+
ty: Ty<'tcx>,
266+
) -> ErrorGuaranteed {
263267
// We've reached the recursion limit, error gracefully.
264268
let suggested_limit = match tcx.recursion_limit() {
265269
Limit(0) => Limit(2),
@@ -270,5 +274,5 @@ pub fn report_autoderef_recursion_limit_error<'tcx>(tcx: TyCtxt<'tcx>, span: Spa
270274
ty,
271275
suggested_limit,
272276
crate_name: tcx.crate_name(LOCAL_CRATE),
273-
});
277+
})
274278
}

compiler/rustc_hir_typeck/src/pat.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_hir::{
1414
self as hir, BindingMode, ByRef, ExprKind, HirId, LangItem, Mutability, Pat, PatExpr,
1515
PatExprKind, PatKind, expr_needs_parens,
1616
};
17+
use rustc_hir_analysis::autoderef::report_autoderef_recursion_limit_error;
1718
use rustc_infer::infer;
1819
use rustc_middle::traits::PatternOriginExpr;
1920
use rustc_middle::ty::{self, AdtDef, Ty, TypeVisitableExt};
@@ -551,17 +552,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
551552
debug!("scrutinee ty {expected:?} is a smart pointer, inserting overloaded deref");
552553
// The scrutinee is a smart pointer; implicitly dereference it. This adds a
553554
// requirement that `expected: DerefPure`.
554-
let inner_ty = self.deref_pat_target(pat.span, expected);
555+
let mut inner_ty = self.deref_pat_target(pat.span, expected);
555556
// Once we've checked `pat`, we'll add a `DerefMut` bound if it contains any
556557
// `ref mut` bindings. See `Self::register_deref_mut_bounds_if_needed`.
557558

558-
// Preserve the smart pointer type for THIR lowering and upvar analysis.
559-
self.typeck_results
560-
.borrow_mut()
561-
.pat_adjustments_mut()
562-
.entry(pat.hir_id)
563-
.or_default()
564-
.push(PatAdjustment { kind: PatAdjust::OverloadedDeref, source: expected });
559+
let mut typeck_results = self.typeck_results.borrow_mut();
560+
let mut pat_adjustments_table = typeck_results.pat_adjustments_mut();
561+
let pat_adjustments = pat_adjustments_table.entry(pat.hir_id).or_default();
562+
// We may reach the recursion limit if a user matches on a type `T` satisfying
563+
// `T: Deref<Target = T>`; error gracefully in this case.
564+
// FIXME(deref_patterns): If `deref_patterns` stabilizes, it may make sense to move
565+
// this check out of this branch. Alternatively, this loop could be implemented with
566+
// autoderef and this check removed. For now though, don't break code compiling on
567+
// stable with lots of `&`s and a low recursion limit, if anyone's done that.
568+
if self.tcx.recursion_limit().value_within_limit(pat_adjustments.len()) {
569+
// Preserve the smart pointer type for THIR lowering and closure upvar analysis.
570+
pat_adjustments
571+
.push(PatAdjustment { kind: PatAdjust::OverloadedDeref, source: expected });
572+
} else {
573+
let guar = report_autoderef_recursion_limit_error(self.tcx, pat.span, expected);
574+
inner_ty = Ty::new_error(self.tcx, guar);
575+
}
576+
drop(typeck_results);
565577

566578
// Recurse, using the old pat info to keep `current_depth` to its old value.
567579
// Peeling smart pointers does not update the default binding mode.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//! Test that implicit deref patterns respect the recursion limit
2+
#![feature(deref_patterns)]
3+
#![allow(incomplete_features)]
4+
#![recursion_limit = "8"]
5+
6+
use std::ops::Deref;
7+
8+
struct Cyclic;
9+
impl Deref for Cyclic {
10+
type Target = Cyclic;
11+
fn deref(&self) -> &Cyclic {
12+
&Cyclic
13+
}
14+
}
15+
16+
fn main() {
17+
match &Box::new(Cyclic) {
18+
() => {}
19+
//~^ ERROR: reached the recursion limit while auto-dereferencing `Cyclic`
20+
//~| ERROR: the trait bound `Cyclic: DerefPure` is not satisfied
21+
_ => {}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0055]: reached the recursion limit while auto-dereferencing `Cyclic`
2+
--> $DIR/recursion-limit.rs:18:9
3+
|
4+
LL | () => {}
5+
| ^^ deref recursion limit reached
6+
|
7+
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "16"]` attribute to your crate (`recursion_limit`)
8+
9+
error[E0277]: the trait bound `Cyclic: DerefPure` is not satisfied
10+
--> $DIR/recursion-limit.rs:18:9
11+
|
12+
LL | () => {}
13+
| ^^ the trait `DerefPure` is not implemented for `Cyclic`
14+
15+
error: aborting due to 2 previous errors
16+
17+
Some errors have detailed explanations: E0055, E0277.
18+
For more information about an error, try `rustc --explain E0055`.

0 commit comments

Comments
 (0)