Skip to content

Commit 97f3eee

Browse files
committedMay 7, 2020
Auto merge of #55617 - oli-obk:stacker, r=nagisa,oli-obk
Prevent compiler stack overflow for deeply recursive code I was unable to write a test that 1. runs in under 1s 2. overflows on my machine without this patch The following reproduces the issue, but I don't think it's sensible to include a test that takes 30s to compile. We can now easily squash newly appearing overflows by the strategic insertion of calls to `ensure_sufficient_stack`. ```rust // compile-pass #![recursion_limit="1000000"] macro_rules! chain { (EE $e:expr) => {$e.sin()}; (RECURSE $i:ident $e:expr) => {chain!($i chain!($i chain!($i chain!($i $e))))}; (Z $e:expr) => {chain!(RECURSE EE $e)}; (Y $e:expr) => {chain!(RECURSE Z $e)}; (X $e:expr) => {chain!(RECURSE Y $e)}; (A $e:expr) => {chain!(RECURSE X $e)}; (B $e:expr) => {chain!(RECURSE A $e)}; (C $e:expr) => {chain!(RECURSE B $e)}; // causes overflow on x86_64 linux // less than 1 second until overflow on test machine // after overflow has been fixed, takes 30s to compile :/ (D $e:expr) => {chain!(RECURSE C $e)}; (E $e:expr) => {chain!(RECURSE D $e)}; (F $e:expr) => {chain!(RECURSE E $e)}; // more than 10 seconds (G $e:expr) => {chain!(RECURSE F $e)}; (H $e:expr) => {chain!(RECURSE G $e)}; (I $e:expr) => {chain!(RECURSE H $e)}; (J $e:expr) => {chain!(RECURSE I $e)}; (K $e:expr) => {chain!(RECURSE J $e)}; (L $e:expr) => {chain!(RECURSE L $e)}; } fn main() { let x = chain!(D 42.0_f32); } ``` fixes #55471 fixes #41884 fixes #40161 fixes #34844 fixes #32594 cc @alexcrichton @rust-lang/compiler I looked at all code that checks the recursion limit and inserted stack growth calls where appropriate.
2 parents 29457dd + 935a05f commit 97f3eee

File tree

16 files changed

+447
-351
lines changed

16 files changed

+447
-351
lines changed
 

‎Cargo.lock

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,6 +2639,15 @@ dependencies = [
26392639
"core",
26402640
]
26412641

2642+
[[package]]
2643+
name = "psm"
2644+
version = "0.1.8"
2645+
source = "registry+https://github.com/rust-lang/crates.io-index"
2646+
checksum = "659ecfea2142a458893bb7673134bad50b752fea932349c213d6a23874ce3aa7"
2647+
dependencies = [
2648+
"cc",
2649+
]
2650+
26422651
[[package]]
26432652
name = "publicsuffix"
26442653
version = "1.5.3"
@@ -3708,6 +3717,7 @@ dependencies = [
37083717
"serialize",
37093718
"smallvec 1.4.0",
37103719
"stable_deref_trait",
3720+
"stacker",
37113721
"winapi 0.3.8",
37123722
]
37133723

@@ -4669,6 +4679,19 @@ version = "1.1.0"
46694679
source = "registry+https://github.com/rust-lang/crates.io-index"
46704680
checksum = "ffbc596e092fe5f598b12ef46cc03754085ac2f4d8c739ad61c4ae266cc3b3fa"
46714681

4682+
[[package]]
4683+
name = "stacker"
4684+
version = "0.1.8"
4685+
source = "registry+https://github.com/rust-lang/crates.io-index"
4686+
checksum = "32c2467b8abbb417e4e62fd62229719b9c9d77714a7fa989f1afad16ba9c9743"
4687+
dependencies = [
4688+
"cc",
4689+
"cfg-if",
4690+
"libc",
4691+
"psm",
4692+
"winapi 0.3.8",
4693+
]
4694+
46724695
[[package]]
46734696
name = "std"
46744697
version = "0.0.0"

‎src/librustc_ast_lowering/expr.rs

Lines changed: 191 additions & 176 deletions
Large diffs are not rendered by default.

‎src/librustc_ast_lowering/pat.rs

Lines changed: 75 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,89 @@ use super::{ImplTraitContext, LoweringContext, ParamMode};
22

33
use rustc_ast::ast::*;
44
use rustc_ast::ptr::P;
5+
use rustc_data_structures::stack::ensure_sufficient_stack;
56
use rustc_hir as hir;
67
use rustc_hir::def::Res;
78
use rustc_span::{source_map::Spanned, Span};
89

910
impl<'a, 'hir> LoweringContext<'a, 'hir> {
1011
crate fn lower_pat(&mut self, p: &Pat) -> &'hir hir::Pat<'hir> {
11-
let node = match p.kind {
12-
PatKind::Wild => hir::PatKind::Wild,
13-
PatKind::Ident(ref binding_mode, ident, ref sub) => {
14-
let lower_sub = |this: &mut Self| sub.as_ref().map(|s| this.lower_pat(&*s));
15-
let node = self.lower_pat_ident(p, binding_mode, ident, lower_sub);
16-
node
17-
}
18-
PatKind::Lit(ref e) => hir::PatKind::Lit(self.lower_expr(e)),
19-
PatKind::TupleStruct(ref path, ref pats) => {
20-
let qpath = self.lower_qpath(
21-
p.id,
22-
&None,
23-
path,
24-
ParamMode::Optional,
25-
ImplTraitContext::disallowed(),
26-
);
27-
let (pats, ddpos) = self.lower_pat_tuple(pats, "tuple struct");
28-
hir::PatKind::TupleStruct(qpath, pats, ddpos)
29-
}
30-
PatKind::Or(ref pats) => {
31-
hir::PatKind::Or(self.arena.alloc_from_iter(pats.iter().map(|x| self.lower_pat(x))))
32-
}
33-
PatKind::Path(ref qself, ref path) => {
34-
let qpath = self.lower_qpath(
35-
p.id,
36-
qself,
37-
path,
38-
ParamMode::Optional,
39-
ImplTraitContext::disallowed(),
40-
);
41-
hir::PatKind::Path(qpath)
42-
}
43-
PatKind::Struct(ref path, ref fields, etc) => {
44-
let qpath = self.lower_qpath(
45-
p.id,
46-
&None,
47-
path,
48-
ParamMode::Optional,
49-
ImplTraitContext::disallowed(),
50-
);
12+
ensure_sufficient_stack(|| {
13+
let node = match p.kind {
14+
PatKind::Wild => hir::PatKind::Wild,
15+
PatKind::Ident(ref binding_mode, ident, ref sub) => {
16+
let lower_sub = |this: &mut Self| sub.as_ref().map(|s| this.lower_pat(&*s));
17+
let node = self.lower_pat_ident(p, binding_mode, ident, lower_sub);
18+
node
19+
}
20+
PatKind::Lit(ref e) => hir::PatKind::Lit(self.lower_expr(e)),
21+
PatKind::TupleStruct(ref path, ref pats) => {
22+
let qpath = self.lower_qpath(
23+
p.id,
24+
&None,
25+
path,
26+
ParamMode::Optional,
27+
ImplTraitContext::disallowed(),
28+
);
29+
let (pats, ddpos) = self.lower_pat_tuple(pats, "tuple struct");
30+
hir::PatKind::TupleStruct(qpath, pats, ddpos)
31+
}
32+
PatKind::Or(ref pats) => hir::PatKind::Or(
33+
self.arena.alloc_from_iter(pats.iter().map(|x| self.lower_pat(x))),
34+
),
35+
PatKind::Path(ref qself, ref path) => {
36+
let qpath = self.lower_qpath(
37+
p.id,
38+
qself,
39+
path,
40+
ParamMode::Optional,
41+
ImplTraitContext::disallowed(),
42+
);
43+
hir::PatKind::Path(qpath)
44+
}
45+
PatKind::Struct(ref path, ref fields, etc) => {
46+
let qpath = self.lower_qpath(
47+
p.id,
48+
&None,
49+
path,
50+
ParamMode::Optional,
51+
ImplTraitContext::disallowed(),
52+
);
5153

52-
let fs = self.arena.alloc_from_iter(fields.iter().map(|f| hir::FieldPat {
53-
hir_id: self.next_id(),
54-
ident: f.ident,
55-
pat: self.lower_pat(&f.pat),
56-
is_shorthand: f.is_shorthand,
57-
span: f.span,
58-
}));
59-
hir::PatKind::Struct(qpath, fs, etc)
60-
}
61-
PatKind::Tuple(ref pats) => {
62-
let (pats, ddpos) = self.lower_pat_tuple(pats, "tuple");
63-
hir::PatKind::Tuple(pats, ddpos)
64-
}
65-
PatKind::Box(ref inner) => hir::PatKind::Box(self.lower_pat(inner)),
66-
PatKind::Ref(ref inner, mutbl) => hir::PatKind::Ref(self.lower_pat(inner), mutbl),
67-
PatKind::Range(ref e1, ref e2, Spanned { node: ref end, .. }) => hir::PatKind::Range(
68-
e1.as_deref().map(|e| self.lower_expr(e)),
69-
e2.as_deref().map(|e| self.lower_expr(e)),
70-
self.lower_range_end(end, e2.is_some()),
71-
),
72-
PatKind::Slice(ref pats) => self.lower_pat_slice(pats),
73-
PatKind::Rest => {
74-
// If we reach here the `..` pattern is not semantically allowed.
75-
self.ban_illegal_rest_pat(p.span)
76-
}
77-
PatKind::Paren(ref inner) => return self.lower_pat(inner),
78-
PatKind::MacCall(_) => panic!("Shouldn't exist here"),
79-
};
54+
let fs = self.arena.alloc_from_iter(fields.iter().map(|f| hir::FieldPat {
55+
hir_id: self.next_id(),
56+
ident: f.ident,
57+
pat: self.lower_pat(&f.pat),
58+
is_shorthand: f.is_shorthand,
59+
span: f.span,
60+
}));
61+
hir::PatKind::Struct(qpath, fs, etc)
62+
}
63+
PatKind::Tuple(ref pats) => {
64+
let (pats, ddpos) = self.lower_pat_tuple(pats, "tuple");
65+
hir::PatKind::Tuple(pats, ddpos)
66+
}
67+
PatKind::Box(ref inner) => hir::PatKind::Box(self.lower_pat(inner)),
68+
PatKind::Ref(ref inner, mutbl) => hir::PatKind::Ref(self.lower_pat(inner), mutbl),
69+
PatKind::Range(ref e1, ref e2, Spanned { node: ref end, .. }) => {
70+
hir::PatKind::Range(
71+
e1.as_deref().map(|e| self.lower_expr(e)),
72+
e2.as_deref().map(|e| self.lower_expr(e)),
73+
self.lower_range_end(end, e2.is_some()),
74+
)
75+
}
76+
PatKind::Slice(ref pats) => self.lower_pat_slice(pats),
77+
PatKind::Rest => {
78+
// If we reach here the `..` pattern is not semantically allowed.
79+
self.ban_illegal_rest_pat(p.span)
80+
}
81+
// FIXME: consider not using recursion to lower this.
82+
PatKind::Paren(ref inner) => return self.lower_pat(inner),
83+
PatKind::MacCall(_) => panic!("{:?} shouldn't exist here", p.span),
84+
};
8085

81-
self.pat_with_node_id_of(p, node)
86+
self.pat_with_node_id_of(p, node)
87+
})
8288
}
8389

8490
fn lower_pat_tuple(

‎src/librustc_data_structures/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ rustc_index = { path = "../librustc_index", package = "rustc_index" }
2828
bitflags = "1.2.1"
2929
measureme = "0.7.1"
3030
libc = "0.2"
31+
stacker = "0.1.6"
3132

3233
[dependencies.parking_lot]
3334
version = "0.10"

‎src/librustc_data_structures/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub mod stable_set;
8080
#[macro_use]
8181
pub mod stable_hasher;
8282
pub mod sharded;
83+
pub mod stack;
8384
pub mod sync;
8485
pub mod thin_vec;
8586
pub mod tiny_list;

‎src/librustc_data_structures/stack.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This is the amount of bytes that need to be left on the stack before increasing the size.
2+
// It must be at least as large as the stack required by any code that does not call
3+
// `ensure_sufficient_stack`.
4+
const RED_ZONE: usize = 100 * 1024; // 100k
5+
6+
// Only the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then
7+
// on. This flag has performance relevant characteristics. Don't set it too high.
8+
const STACK_PER_RECURSION: usize = 1 * 1024 * 1024; // 1MB
9+
10+
/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations
11+
/// to "break up" recursive calls. E.g. almost any call to `visit_expr` or equivalent can benefit
12+
/// from this.
13+
///
14+
/// Should not be sprinkled around carelessly, as it causes a little bit of overhead.
15+
pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {
16+
stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)
17+
}

‎src/librustc_interface/util.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,7 @@ pub fn create_session(
8181
(Lrc::new(sess), Lrc::new(codegen_backend), source_map)
8282
}
8383

84-
// Temporarily have stack size set to 32MB to deal with various crates with long method
85-
// chains or deep syntax trees, except when on Haiku.
86-
// FIXME(oli-obk): get https://github.com/rust-lang/rust/pull/55617 the finish line
87-
#[cfg(not(target_os = "haiku"))]
88-
const STACK_SIZE: usize = 32 * 1024 * 1024;
89-
90-
#[cfg(target_os = "haiku")]
91-
const STACK_SIZE: usize = 16 * 1024 * 1024;
84+
const STACK_SIZE: usize = 8 * 1024 * 1024;
9285

9386
fn get_stack_size() -> Option<usize> {
9487
// FIXME: Hacks on hacks. If the env is trying to override the stack size

‎src/librustc_middle/ty/inhabitedness/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::ty::TyKind::*;
66
use crate::ty::{AdtDef, FieldDef, Ty, TyS, VariantDef};
77
use crate::ty::{AdtKind, Visibility};
88
use crate::ty::{DefId, SubstsRef};
9+
use rustc_data_structures::stack::ensure_sufficient_stack;
910

1011
mod def_id_forest;
1112

@@ -196,7 +197,9 @@ impl<'tcx> TyS<'tcx> {
196197
/// Calculates the forest of `DefId`s from which this type is visibly uninhabited.
197198
fn uninhabited_from(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> DefIdForest {
198199
match self.kind {
199-
Adt(def, substs) => def.uninhabited_from(tcx, substs, param_env),
200+
Adt(def, substs) => {
201+
ensure_sufficient_stack(|| def.uninhabited_from(tcx, substs, param_env))
202+
}
200203

201204
Never => DefIdForest::full(tcx),
202205

‎src/librustc_middle/ty/query/plumbing.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ impl QueryContext for TyCtxt<'tcx> {
6868
};
6969

7070
// Use the `ImplicitCtxt` while we execute the query.
71-
tls::enter_context(&new_icx, |_| compute(*self))
71+
tls::enter_context(&new_icx, |_| {
72+
rustc_data_structures::stack::ensure_sufficient_stack(|| compute(*self))
73+
})
7274
})
7375
}
7476
}

‎src/librustc_mir/monomorphize/collector.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,9 @@ fn collect_items_rec<'tcx>(
369369
recursion_depth_reset = Some(check_recursion_limit(tcx, instance, recursion_depths));
370370
check_type_length_limit(tcx, instance);
371371

372-
collect_neighbours(tcx, instance, &mut neighbors);
372+
rustc_data_structures::stack::ensure_sufficient_stack(|| {
373+
collect_neighbours(tcx, instance, &mut neighbors);
374+
});
373375
}
374376
MonoItem::GlobalAsm(..) => {
375377
recursion_depth_reset = None;
@@ -1146,7 +1148,9 @@ fn collect_miri<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: AllocId, output: &mut Vec<Mon
11461148
Some(GlobalAlloc::Memory(alloc)) => {
11471149
trace!("collecting {:?} with {:#?}", alloc_id, alloc);
11481150
for &((), inner) in alloc.relocations().values() {
1149-
collect_miri(tcx, inner, output);
1151+
rustc_data_structures::stack::ensure_sufficient_stack(|| {
1152+
collect_miri(tcx, inner, output);
1153+
});
11501154
}
11511155
}
11521156
Some(GlobalAlloc::Function(fn_instance)) => {

‎src/librustc_mir_build/build/expr/as_temp.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::build::scope::DropKind;
44
use crate::build::{BlockAnd, BlockAndExtension, Builder};
55
use crate::hair::*;
6+
use rustc_data_structures::stack::ensure_sufficient_stack;
67
use rustc_hir as hir;
78
use rustc_middle::middle::region;
89
use rustc_middle::mir::*;
@@ -21,7 +22,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2122
M: Mirror<'tcx, Output = Expr<'tcx>>,
2223
{
2324
let expr = self.hir.mirror(expr);
24-
self.expr_as_temp(block, temp_lifetime, expr, mutability)
25+
//
26+
// this is the only place in mir building that we need to truly need to worry about
27+
// infinite recursion. Everything else does recurse, too, but it always gets broken up
28+
// at some point by inserting an intermediate temporary
29+
ensure_sufficient_stack(|| self.expr_as_temp(block, temp_lifetime, expr, mutability))
2530
}
2631

2732
fn expr_as_temp(

‎src/librustc_trait_selection/traits/project.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1818
use crate::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
1919
use crate::traits::error_reporting::InferCtxtExt;
2020
use rustc_ast::ast::Ident;
21+
use rustc_data_structures::stack::ensure_sufficient_stack;
2122
use rustc_errors::ErrorReported;
2223
use rustc_hir::def_id::DefId;
2324
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
@@ -261,7 +262,7 @@ where
261262
{
262263
debug!("normalize_with_depth(depth={}, value={:?})", depth, value);
263264
let mut normalizer = AssocTypeNormalizer::new(selcx, param_env, cause, depth, obligations);
264-
let result = normalizer.fold(value);
265+
let result = ensure_sufficient_stack(|| normalizer.fold(value));
265266
debug!(
266267
"normalize_with_depth: depth={} result={:?} with {} obligations",
267268
depth,

‎src/librustc_trait_selection/traits/query/normalize.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::infer::canonical::OriginalQueryValues;
77
use crate::infer::{InferCtxt, InferOk};
88
use crate::traits::error_reporting::InferCtxtExt;
99
use crate::traits::{Obligation, ObligationCause, PredicateObligation, Reveal};
10+
use rustc_data_structures::stack::ensure_sufficient_stack;
1011
use rustc_infer::traits::Normalized;
1112
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
1213
use rustc_middle::ty::subst::Subst;
@@ -131,7 +132,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
131132
ty
132133
);
133134
}
134-
let folded_ty = self.fold_ty(concrete_ty);
135+
let folded_ty = ensure_sufficient_stack(|| self.fold_ty(concrete_ty));
135136
self.anon_depth -= 1;
136137
folded_ty
137138
}

‎src/librustc_trait_selection/traits/select.rs

Lines changed: 104 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use crate::traits::error_reporting::InferCtxtExt;
3737
use crate::traits::project::ProjectionCacheKeyExt;
3838
use rustc_ast::attr;
3939
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
40+
use rustc_data_structures::stack::ensure_sufficient_stack;
4041
use rustc_hir as hir;
4142
use rustc_hir::def_id::DefId;
4243
use rustc_hir::lang_items;
@@ -2365,13 +2366,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
23652366
self.infcx.commit_unconditionally(|_| {
23662367
let (skol_ty, _) = self.infcx.replace_bound_vars_with_placeholders(&ty);
23672368
let Normalized { value: normalized_ty, mut obligations } =
2368-
project::normalize_with_depth(
2369-
self,
2370-
param_env,
2371-
cause.clone(),
2372-
recursion_depth,
2373-
&skol_ty,
2374-
);
2369+
ensure_sufficient_stack(|| {
2370+
project::normalize_with_depth(
2371+
self,
2372+
param_env,
2373+
cause.clone(),
2374+
recursion_depth,
2375+
&skol_ty,
2376+
)
2377+
});
23752378
let skol_obligation = predicate_for_trait_def(
23762379
self.tcx(),
23772380
param_env,
@@ -2525,13 +2528,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
25252528
};
25262529

25272530
let cause = obligation.derived_cause(BuiltinDerivedObligation);
2528-
self.collect_predicates_for_types(
2529-
obligation.param_env,
2530-
cause,
2531-
obligation.recursion_depth + 1,
2532-
trait_def,
2533-
nested,
2534-
)
2531+
ensure_sufficient_stack(|| {
2532+
self.collect_predicates_for_types(
2533+
obligation.param_env,
2534+
cause,
2535+
obligation.recursion_depth + 1,
2536+
trait_def,
2537+
nested,
2538+
)
2539+
})
25352540
} else {
25362541
vec![]
25372542
};
@@ -2568,38 +2573,39 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
25682573
nested: ty::Binder<Vec<Ty<'tcx>>>,
25692574
) -> VtableAutoImplData<PredicateObligation<'tcx>> {
25702575
debug!("vtable_auto_impl: nested={:?}", nested);
2576+
ensure_sufficient_stack(|| {
2577+
let cause = obligation.derived_cause(BuiltinDerivedObligation);
2578+
let mut obligations = self.collect_predicates_for_types(
2579+
obligation.param_env,
2580+
cause,
2581+
obligation.recursion_depth + 1,
2582+
trait_def_id,
2583+
nested,
2584+
);
25712585

2572-
let cause = obligation.derived_cause(BuiltinDerivedObligation);
2573-
let mut obligations = self.collect_predicates_for_types(
2574-
obligation.param_env,
2575-
cause,
2576-
obligation.recursion_depth + 1,
2577-
trait_def_id,
2578-
nested,
2579-
);
2580-
2581-
let trait_obligations: Vec<PredicateObligation<'_>> =
2582-
self.infcx.commit_unconditionally(|_| {
2583-
let poly_trait_ref = obligation.predicate.to_poly_trait_ref();
2584-
let (trait_ref, _) =
2585-
self.infcx.replace_bound_vars_with_placeholders(&poly_trait_ref);
2586-
let cause = obligation.derived_cause(ImplDerivedObligation);
2587-
self.impl_or_trait_obligations(
2588-
cause,
2589-
obligation.recursion_depth + 1,
2590-
obligation.param_env,
2591-
trait_def_id,
2592-
&trait_ref.substs,
2593-
)
2594-
});
2586+
let trait_obligations: Vec<PredicateObligation<'_>> =
2587+
self.infcx.commit_unconditionally(|_| {
2588+
let poly_trait_ref = obligation.predicate.to_poly_trait_ref();
2589+
let (trait_ref, _) =
2590+
self.infcx.replace_bound_vars_with_placeholders(&poly_trait_ref);
2591+
let cause = obligation.derived_cause(ImplDerivedObligation);
2592+
self.impl_or_trait_obligations(
2593+
cause,
2594+
obligation.recursion_depth + 1,
2595+
obligation.param_env,
2596+
trait_def_id,
2597+
&trait_ref.substs,
2598+
)
2599+
});
25952600

2596-
// Adds the predicates from the trait. Note that this contains a `Self: Trait`
2597-
// predicate as usual. It won't have any effect since auto traits are coinductive.
2598-
obligations.extend(trait_obligations);
2601+
// Adds the predicates from the trait. Note that this contains a `Self: Trait`
2602+
// predicate as usual. It won't have any effect since auto traits are coinductive.
2603+
obligations.extend(trait_obligations);
25992604

2600-
debug!("vtable_auto_impl: obligations={:?}", obligations);
2605+
debug!("vtable_auto_impl: obligations={:?}", obligations);
26012606

2602-
VtableAutoImplData { trait_def_id, nested: obligations }
2607+
VtableAutoImplData { trait_def_id, nested: obligations }
2608+
})
26032609
}
26042610

26052611
fn confirm_impl_candidate(
@@ -2615,13 +2621,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
26152621
let substs = self.rematch_impl(impl_def_id, obligation, snapshot);
26162622
debug!("confirm_impl_candidate: substs={:?}", substs);
26172623
let cause = obligation.derived_cause(ImplDerivedObligation);
2618-
self.vtable_impl(
2619-
impl_def_id,
2620-
substs,
2621-
cause,
2622-
obligation.recursion_depth + 1,
2623-
obligation.param_env,
2624-
)
2624+
ensure_sufficient_stack(|| {
2625+
self.vtable_impl(
2626+
impl_def_id,
2627+
substs,
2628+
cause,
2629+
obligation.recursion_depth + 1,
2630+
obligation.param_env,
2631+
)
2632+
})
26252633
})
26262634
}
26272635

@@ -2734,13 +2742,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
27342742
)
27352743
.map_bound(|(trait_ref, _)| trait_ref);
27362744

2737-
let Normalized { value: trait_ref, obligations } = project::normalize_with_depth(
2738-
self,
2739-
obligation.param_env,
2740-
obligation.cause.clone(),
2741-
obligation.recursion_depth + 1,
2742-
&trait_ref,
2743-
);
2745+
let Normalized { value: trait_ref, obligations } = ensure_sufficient_stack(|| {
2746+
project::normalize_with_depth(
2747+
self,
2748+
obligation.param_env,
2749+
obligation.cause.clone(),
2750+
obligation.recursion_depth + 1,
2751+
&trait_ref,
2752+
)
2753+
});
27442754

27452755
self.confirm_poly_trait_refs(
27462756
obligation.cause.clone(),
@@ -2798,13 +2808,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
27982808
debug!("confirm_generator_candidate({:?},{:?},{:?})", obligation, generator_def_id, substs);
27992809

28002810
let trait_ref = self.generator_trait_ref_unnormalized(obligation, substs);
2801-
let Normalized { value: trait_ref, mut obligations } = normalize_with_depth(
2802-
self,
2803-
obligation.param_env,
2804-
obligation.cause.clone(),
2805-
obligation.recursion_depth + 1,
2806-
&trait_ref,
2807-
);
2811+
let Normalized { value: trait_ref, mut obligations } = ensure_sufficient_stack(|| {
2812+
normalize_with_depth(
2813+
self,
2814+
obligation.param_env,
2815+
obligation.cause.clone(),
2816+
obligation.recursion_depth + 1,
2817+
&trait_ref,
2818+
)
2819+
});
28082820

28092821
debug!(
28102822
"confirm_generator_candidate(generator_def_id={:?}, \
@@ -2843,13 +2855,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
28432855
};
28442856

28452857
let trait_ref = self.closure_trait_ref_unnormalized(obligation, substs);
2846-
let Normalized { value: trait_ref, mut obligations } = normalize_with_depth(
2847-
self,
2848-
obligation.param_env,
2849-
obligation.cause.clone(),
2850-
obligation.recursion_depth + 1,
2851-
&trait_ref,
2852-
);
2858+
let Normalized { value: trait_ref, mut obligations } = ensure_sufficient_stack(|| {
2859+
normalize_with_depth(
2860+
self,
2861+
obligation.param_env,
2862+
obligation.cause.clone(),
2863+
obligation.recursion_depth + 1,
2864+
&trait_ref,
2865+
)
2866+
});
28532867

28542868
debug!(
28552869
"confirm_closure_candidate(closure_def_id={:?}, trait_ref={:?}, obligations={:?})",
@@ -3139,15 +3153,17 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
31393153
nested.extend(obligations);
31403154

31413155
// Construct the nested `T: Unsize<U>` predicate.
3142-
nested.push(predicate_for_trait_def(
3143-
tcx,
3144-
obligation.param_env,
3145-
obligation.cause.clone(),
3146-
obligation.predicate.def_id(),
3147-
obligation.recursion_depth + 1,
3148-
a_last.expect_ty(),
3149-
&[b_last],
3150-
));
3156+
nested.push(ensure_sufficient_stack(|| {
3157+
predicate_for_trait_def(
3158+
tcx,
3159+
obligation.param_env,
3160+
obligation.cause.clone(),
3161+
obligation.predicate.def_id(),
3162+
obligation.recursion_depth + 1,
3163+
a_last.expect_ty(),
3164+
&[b_last],
3165+
)
3166+
}));
31513167
}
31523168

31533169
_ => bug!(),
@@ -3208,13 +3224,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
32083224
let impl_trait_ref = impl_trait_ref.subst(self.tcx(), impl_substs);
32093225

32103226
let Normalized { value: impl_trait_ref, obligations: mut nested_obligations } =
3211-
project::normalize_with_depth(
3212-
self,
3213-
obligation.param_env,
3214-
obligation.cause.clone(),
3215-
obligation.recursion_depth + 1,
3216-
&impl_trait_ref,
3217-
);
3227+
ensure_sufficient_stack(|| {
3228+
project::normalize_with_depth(
3229+
self,
3230+
obligation.param_env,
3231+
obligation.cause.clone(),
3232+
obligation.recursion_depth + 1,
3233+
&impl_trait_ref,
3234+
)
3235+
});
32183236

32193237
debug!(
32203238
"match_impl(impl_def_id={:?}, obligation={:?}, \

‎src/librustc_traits/dropck_outlives.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,12 @@ fn dtorck_constraint_for_ty<'tcx>(
191191

192192
ty::Array(ety, _) | ty::Slice(ety) => {
193193
// single-element containers, behave like their element
194-
dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ety, constraints)?;
194+
rustc_data_structures::stack::ensure_sufficient_stack(|| {
195+
dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ety, constraints)
196+
})?;
195197
}
196198

197-
ty::Tuple(tys) => {
199+
ty::Tuple(tys) => rustc_data_structures::stack::ensure_sufficient_stack(|| {
198200
for ty in tys.iter() {
199201
dtorck_constraint_for_ty(
200202
tcx,
@@ -205,13 +207,15 @@ fn dtorck_constraint_for_ty<'tcx>(
205207
constraints,
206208
)?;
207209
}
208-
}
210+
Ok::<_, NoSolution>(())
211+
})?,
209212

210-
ty::Closure(_, substs) => {
213+
ty::Closure(_, substs) => rustc_data_structures::stack::ensure_sufficient_stack(|| {
211214
for ty in substs.as_closure().upvar_tys() {
212215
dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ty, constraints)?;
213216
}
214-
}
217+
Ok::<_, NoSolution>(())
218+
})?,
215219

216220
ty::Generator(_, substs, _movability) => {
217221
// rust-lang/rust#49918: types can be constructed, stored

‎src/tools/tidy/src/deps.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ const WHITELIST: &[&str] = &[
131131
"polonius-engine",
132132
"ppv-lite86",
133133
"proc-macro2",
134+
"psm",
134135
"punycode",
135136
"quick-error",
136137
"quote",
@@ -160,6 +161,7 @@ const WHITELIST: &[&str] = &[
160161
"sha-1",
161162
"smallvec",
162163
"stable_deref_trait",
164+
"stacker",
163165
"syn",
164166
"synstructure",
165167
"tempfile",

0 commit comments

Comments
 (0)
Please sign in to comment.