Skip to content

Commit a5c5365

Browse files
nagisamati865
authored andcommitted
Move ensure_sufficient_stack to data_structures
We anticipate this to have uses in all sorts of crates and keeping it in `rustc_data_structures` enables access to it from more locations without necessarily pulling in the large `librustc` crate.
1 parent 968f442 commit a5c5365

File tree

16 files changed

+33
-33
lines changed

16 files changed

+33
-33
lines changed

Cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -3161,7 +3161,6 @@ checksum = "81dfcfbb0ddfd533abf8c076e3b49d1e5042d1962526a12ce2c66d514b24cca3"
31613161
dependencies = [
31623162
"rustc-ap-rustc_data_structures",
31633163
"smallvec 1.0.0",
3164-
"stacker",
31653164
]
31663165

31673166
[[package]]
@@ -3706,6 +3705,7 @@ dependencies = [
37063705
"serialize",
37073706
"smallvec 1.0.0",
37083707
"stable_deref_trait",
3708+
"stacker",
37093709
"winapi 0.3.8",
37103710
]
37113711

src/librustc_ast_lowering/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericAr
33
use rustc_ast::ast::*;
44
use rustc_ast::attr;
55
use rustc_ast::ptr::P as AstP;
6+
use rustc_data_structures::stack::ensure_sufficient_stack;
67
use rustc_data_structures::thin_vec::ThinVec;
78
use rustc_errors::struct_span_err;
89
use rustc_hir as hir;
910
use rustc_hir::def::Res;
10-
use rustc_middle::limits::ensure_sufficient_stack;
1111
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
1212
use rustc_span::symbol::{sym, Symbol};
1313

src/librustc_ast_lowering/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ 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;
7-
use rustc_middle::limits::ensure_sufficient_stack;
88
use rustc_span::{source_map::Spanned, Span};
99

1010
impl<'a, 'hir> LoweringContext<'a, 'hir> {

src/librustc_data_structures/Cargo.toml

+1
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

+1
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

+17
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_middle/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,3 @@ byteorder = { version = "1.3" }
3434
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
3535
measureme = "0.7.1"
3636
rustc_session = { path = "../librustc_session" }
37-
stacker = "0.1.6"

src/librustc_middle/middle/limits.rs

-18
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,6 @@ use rustc_span::symbol::{sym, Symbol};
1313

1414
use std::num::IntErrorKind;
1515

16-
// This is the amount of bytes that need to be left on the stack before increasing the size.
17-
// It must be at least as large as the stack required by any code that does not call
18-
// `ensure_sufficient_stack`.
19-
const RED_ZONE: usize = 100 * 1024; // 100k
20-
21-
// Ony the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then
22-
// on. This flag has performance relevant characteristics. Don't set it too high.
23-
const STACK_PER_RECURSION: usize = 1 * 1024 * 1024; // 1MB
24-
25-
/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations
26-
/// to "break up" recursive calls. E.g. almost any call to `visit_expr` or equivalent can benefit
27-
/// from this.
28-
///
29-
/// Should not be sprinkled around carelessly, as it causes a little bit of overhead.
30-
pub fn ensure_sufficient_stack<R, F: FnOnce() -> R>(f: F) -> R {
31-
stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)
32-
}
33-
3416
pub fn update_limits(sess: &Session, krate: &ast::Crate) {
3517
update_limit(sess, krate, &sess.recursion_limit, sym::recursion_limit, 128);
3618
update_limit(sess, krate, &sess.type_length_limit, sym::type_length_limit, 1048576);

src/librustc_middle/ty/inhabitedness/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
pub use self::def_id_forest::DefIdForest;
22

3-
use crate::middle::limits::ensure_sufficient_stack;
43
use crate::ty;
54
use crate::ty::context::TyCtxt;
65
use crate::ty::TyKind::*;
76
use crate::ty::{AdtDef, FieldDef, Ty, TyS, VariantDef};
87
use crate::ty::{AdtKind, Visibility};
98
use crate::ty::{DefId, SubstsRef};
9+
use rustc_data_structures::stack::ensure_sufficient_stack;
1010

1111
mod def_id_forest;
1212

src/librustc_middle/ty/query/plumbing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl QueryContext for TyCtxt<'tcx> {
6969

7070
// Use the `ImplicitCtxt` while we execute the query.
7171
tls::enter_context(&new_icx, |_| {
72-
crate::middle::limits::ensure_sufficient_stack(|| compute(*self))
72+
rustc_data_structures::stack::ensure_sufficient_stack(|| compute(*self))
7373
})
7474
})
7575
}

src/librustc_mir/monomorphize/collector.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ 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-
rustc::middle::limits::ensure_sufficient_stack(|| {
372+
rustc_data_structures::stack::ensure_sufficient_stack(|| {
373373
collect_neighbours(tcx, instance, &mut neighbors);
374374
});
375375
}
@@ -1148,7 +1148,7 @@ fn collect_miri<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: AllocId, output: &mut Vec<Mon
11481148
Some(GlobalAlloc::Memory(alloc)) => {
11491149
trace!("collecting {:?} with {:#?}", alloc_id, alloc);
11501150
for &((), inner) in alloc.relocations().values() {
1151-
rustc_middle::limits::ensure_sufficient_stack(|| {
1151+
rustc_data_structures::stack::ensure_sufficient_stack(|| {
11521152
collect_miri(tcx, inner, output);
11531153
});
11541154
}

src/librustc_mir_build/build/expr/as_temp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
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;
7-
use rustc_middle::limits::ensure_sufficient_stack;
88
use rustc_middle::middle::region;
99
use rustc_middle::mir::*;
1010

src/librustc_trait_selection/traits/project.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ 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;
23-
use rustc_middle::limits::ensure_sufficient_stack;
2424
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
2525
use rustc_middle::ty::subst::{InternalSubsts, Subst};
2626
use rustc_middle::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, WithConstness};

src/librustc_trait_selection/traits/query/normalize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ 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;
11-
use rustc_middle::limits::ensure_sufficient_stack;
1212
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
1313
use rustc_middle::ty::subst::Subst;
1414
use rustc_middle::ty::{self, Ty, TyCtxt};

src/librustc_trait_selection/traits/select.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ 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;
4344
use rustc_index::bit_set::GrowableBitSet;
4445
use rustc_middle::dep_graph::{DepKind, DepNodeIndex};
45-
use rustc_middle::limits::ensure_sufficient_stack;
4646
use rustc_middle::ty::fast_reject;
4747
use rustc_middle::ty::relate::TypeRelation;
4848
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst, SubstsRef};

src/librustc_traits/dropck_outlives.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -191,12 +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-
rustc_middle::limits::ensure_sufficient_stack(|| {
194+
rustc_data_structures::stack::ensure_sufficient_stack(|| {
195195
dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ety, constraints)
196196
})?;
197197
}
198198

199-
ty::Tuple(tys) => rustc_middle::limits::ensure_sufficient_stack(|| {
199+
ty::Tuple(tys) => rustc_data_structures::stack::ensure_sufficient_stack(|| {
200200
for ty in tys.iter() {
201201
dtorck_constraint_for_ty(
202202
tcx,
@@ -210,7 +210,7 @@ fn dtorck_constraint_for_ty<'tcx>(
210210
Ok::<_, NoSolution>(())
211211
})?,
212212

213-
ty::Closure(_, substs) => rustc_middle::limits::ensure_sufficient_stack(|| {
213+
ty::Closure(_, substs) => rustc_data_structures::stack::ensure_sufficient_stack(|| {
214214
for ty in substs.as_closure().upvar_tys() {
215215
dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ty, constraints)?;
216216
}

0 commit comments

Comments
 (0)