Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 969a6c2

Browse files
committedJul 5, 2021
Auto merge of #86674 - Aaron1011:new-querify-limits, r=michaelwoerister
Query-ify global limit attribute handling Currently, we read various 'global limits' from inner attributes the crate root (`recursion_limit`, `move_size_limit`, `type_length_limit`, `const_eval_limit`). These limits are then stored in `Sessions`, allowing them to be access from a `TyCtxt` without registering a dependency on the crate root attributes. This PR moves the calculation of these global limits behind queries, so that we properly track dependencies on crate root attributes. During the setup of macro expansion (before we've created a `TyCtxt`), we need to access the recursion limit, which is now done by directly calling into the code shared by the normal query implementations.
2 parents 6e9b369 + 7e5a88a commit 969a6c2

File tree

31 files changed

+173
-91
lines changed

31 files changed

+173
-91
lines changed
 

‎compiler/rustc_interface/src/passes.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,6 @@ pub fn register_plugins<'a>(
211211
});
212212
}
213213

214-
sess.time("recursion_limit", || {
215-
middle::limits::update_limits(sess, &krate);
216-
});
217-
218214
let mut lint_store = rustc_lint::new_lint_store(
219215
sess.opts.debugging_opts.no_interleave_lints,
220216
sess.unstable_options(),
@@ -311,9 +307,11 @@ pub fn configure_and_expand(
311307

312308
// Create the config for macro expansion
313309
let features = sess.features_untracked();
310+
let recursion_limit =
311+
rustc_middle::middle::limits::get_recursion_limit(&krate.attrs, &sess);
314312
let cfg = rustc_expand::expand::ExpansionConfig {
315313
features: Some(&features),
316-
recursion_limit: sess.recursion_limit(),
314+
recursion_limit,
317315
trace_mac: sess.opts.debugging_opts.trace_macros,
318316
should_test: sess.opts.test,
319317
span_debug: sess.opts.debugging_opts.span_debug,
@@ -872,6 +870,13 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
872870
tcx.ensure().check_mod_unstable_api_usage(module);
873871
tcx.ensure().check_mod_const_bodies(module);
874872
});
873+
},
874+
{
875+
// We force these querie to run,
876+
// since they might not otherwise get called.
877+
// This marks the corresponding crate-level attributes
878+
// as used, and ensures that their values are valid.
879+
tcx.ensure().limits(());
875880
}
876881
);
877882
});

‎compiler/rustc_middle/src/ich/hcx.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ impl<'a> rustc_span::HashStableContext for StableHashingContext<'a> {
245245
}
246246
}
247247

248+
impl rustc_session::HashStableContext for StableHashingContext<'a> {}
249+
248250
pub fn hash_stable_trait_impls<'a>(
249251
hcx: &mut StableHashingContext<'a>,
250252
hasher: &mut StableHasher,

‎compiler/rustc_middle/src/middle/limits.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,46 @@
1010
//! just peeks and looks for that attribute.
1111
1212
use crate::bug;
13-
use rustc_ast as ast;
14-
use rustc_data_structures::sync::OnceCell;
13+
use crate::ty;
14+
use rustc_ast::Attribute;
1515
use rustc_session::Session;
16+
use rustc_session::{Limit, Limits};
1617
use rustc_span::symbol::{sym, Symbol};
1718

1819
use std::num::IntErrorKind;
1920

20-
pub fn update_limits(sess: &Session, krate: &ast::Crate) {
21-
update_limit(sess, krate, &sess.recursion_limit, sym::recursion_limit, 128);
22-
update_limit(sess, krate, &sess.move_size_limit, sym::move_size_limit, 0);
23-
update_limit(sess, krate, &sess.type_length_limit, sym::type_length_limit, 1048576);
24-
update_limit(sess, krate, &sess.const_eval_limit, sym::const_eval_limit, 1_000_000);
21+
pub fn provide(providers: &mut ty::query::Providers) {
22+
providers.limits = |tcx, ()| Limits {
23+
recursion_limit: get_recursion_limit(tcx.hir().krate_attrs(), tcx.sess),
24+
move_size_limit: get_limit(tcx.hir().krate_attrs(), tcx.sess, sym::move_size_limit, 0),
25+
type_length_limit: get_limit(
26+
tcx.hir().krate_attrs(),
27+
tcx.sess,
28+
sym::type_length_limit,
29+
1048576,
30+
),
31+
const_eval_limit: get_limit(
32+
tcx.hir().krate_attrs(),
33+
tcx.sess,
34+
sym::const_eval_limit,
35+
1_000_000,
36+
),
37+
}
38+
}
39+
40+
pub fn get_recursion_limit(krate_attrs: &[Attribute], sess: &Session) -> Limit {
41+
get_limit(krate_attrs, sess, sym::recursion_limit, 128)
2542
}
2643

27-
fn update_limit(
28-
sess: &Session,
29-
krate: &ast::Crate,
30-
limit: &OnceCell<impl From<usize> + std::fmt::Debug>,
31-
name: Symbol,
32-
default: usize,
33-
) {
34-
for attr in &krate.attrs {
44+
fn get_limit(krate_attrs: &[Attribute], sess: &Session, name: Symbol, default: usize) -> Limit {
45+
for attr in krate_attrs {
3546
if !sess.check_name(attr, name) {
3647
continue;
3748
}
3849

3950
if let Some(s) = attr.value_str() {
4051
match s.as_str().parse() {
41-
Ok(n) => {
42-
limit.set(From::from(n)).unwrap();
43-
return;
44-
}
52+
Ok(n) => return Limit::new(n),
4553
Err(e) => {
4654
let mut err =
4755
sess.struct_span_err(attr.span, "`limit` must be a non-negative integer");
@@ -68,5 +76,5 @@ fn update_limit(
6876
}
6977
}
7078
}
71-
limit.set(From::from(default)).unwrap();
79+
return Limit::new(default);
7280
}

‎compiler/rustc_middle/src/middle/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ pub mod privacy;
3232
pub mod region;
3333
pub mod resolve_lifetime;
3434
pub mod stability;
35+
36+
pub fn provide(providers: &mut crate::ty::query::Providers) {
37+
limits::provide(providers);
38+
}

‎compiler/rustc_middle/src/query/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,4 +1725,8 @@ rustc_queries! {
17251725
query conservative_is_privately_uninhabited(key: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
17261726
desc { "conservatively checking if {:?} is privately uninhabited", key }
17271727
}
1728+
1729+
query limits(key: ()) -> Limits {
1730+
desc { "looking up limits" }
1731+
}
17281732
}

‎compiler/rustc_middle/src/ty/context.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ use rustc_middle::ty::OpaqueTypeKey;
5353
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
5454
use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames};
5555
use rustc_session::lint::{Level, Lint};
56+
use rustc_session::Limit;
5657
use rustc_session::Session;
5758
use rustc_span::def_id::StableCrateId;
5859
use rustc_span::source_map::MultiSpan;
@@ -1569,6 +1570,22 @@ impl<'tcx> TyCtxt<'tcx> {
15691570
def_kind => (def_kind.article(), def_kind.descr(def_id)),
15701571
}
15711572
}
1573+
1574+
pub fn type_length_limit(self) -> Limit {
1575+
self.limits(()).type_length_limit
1576+
}
1577+
1578+
pub fn recursion_limit(self) -> Limit {
1579+
self.limits(()).recursion_limit
1580+
}
1581+
1582+
pub fn move_size_limit(self) -> Limit {
1583+
self.limits(()).move_size_limit
1584+
}
1585+
1586+
pub fn const_eval_limit(self) -> Limit {
1587+
self.limits(()).const_eval_limit
1588+
}
15721589
}
15731590

15741591
/// A trait implemented for all `X<'a>` types that can be safely and

‎compiler/rustc_middle/src/ty/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ fn layout_raw<'tcx>(
221221
ty::tls::with_related_context(tcx, move |icx| {
222222
let (param_env, ty) = query.into_parts();
223223

224-
if !tcx.sess.recursion_limit().value_within_limit(icx.layout_depth) {
224+
if !tcx.recursion_limit().value_within_limit(icx.layout_depth) {
225225
tcx.sess.fatal(&format!("overflow representing the type `{}`", ty));
226226
}
227227

‎compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,7 @@ pub fn provide(providers: &mut ty::query::Providers) {
19871987
util::provide(providers);
19881988
print::provide(providers);
19891989
super::util::bug::provide(providers);
1990+
super::middle::provide(providers);
19901991
*providers = ty::query::Providers {
19911992
trait_impls_of: trait_def::trait_impls_of_provider,
19921993
type_uninhabited_from: inhabitedness::type_uninhabited_from,

‎compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ impl<F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
14371437
}
14381438

14391439
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
1440-
let type_length_limit = self.tcx.sess.type_length_limit();
1440+
let type_length_limit = self.tcx.type_length_limit();
14411441
if type_length_limit.value_within_limit(self.printed_type_count) {
14421442
self.printed_type_count += 1;
14431443
self.pretty_print_type(ty)

‎compiler/rustc_middle/src/ty/query/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use rustc_serialize::opaque;
4949
use rustc_session::config::{EntryFnType, OptLevel, OutputFilenames, SymbolManglingVersion};
5050
use rustc_session::utils::NativeLibKind;
5151
use rustc_session::CrateDisambiguator;
52+
use rustc_session::Limits;
5253
use rustc_target::spec::PanicStrategy;
5354

5455
use rustc_ast as ast;

‎compiler/rustc_middle/src/ty/util.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,9 @@ impl<'tcx> TyCtxt<'tcx> {
206206
mut ty: Ty<'tcx>,
207207
normalize: impl Fn(Ty<'tcx>) -> Ty<'tcx>,
208208
) -> Ty<'tcx> {
209+
let recursion_limit = self.recursion_limit();
209210
for iteration in 0.. {
210-
if !self.sess.recursion_limit().value_within_limit(iteration) {
211+
if !recursion_limit.value_within_limit(iteration) {
211212
return self.ty_error_with_message(
212213
DUMMY_SP,
213214
&format!("reached the recursion limit finding the struct tail for {}", ty),

‎compiler/rustc_mir/src/const_eval/eval_queries.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub(super) fn mk_eval_cx<'mir, 'tcx>(
9898
tcx,
9999
root_span,
100100
param_env,
101-
CompileTimeInterpreter::new(tcx.sess.const_eval_limit()),
101+
CompileTimeInterpreter::new(tcx.const_eval_limit()),
102102
MemoryExtra { can_access_statics },
103103
)
104104
}
@@ -300,7 +300,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
300300
tcx,
301301
tcx.def_span(def.did),
302302
key.param_env,
303-
CompileTimeInterpreter::new(tcx.sess.const_eval_limit()),
303+
CompileTimeInterpreter::new(tcx.const_eval_limit()),
304304
// Statics (and promoteds inside statics) may access other statics, because unlike consts
305305
// they do not have to behave "as if" they were evaluated at runtime.
306306
MemoryExtra { can_access_statics: is_static },

‎compiler/rustc_mir/src/const_eval/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
393393
frame: Frame<'mir, 'tcx>,
394394
) -> InterpResult<'tcx, Frame<'mir, 'tcx>> {
395395
// Enforce stack size limit. Add 1 because this is run before the new frame is pushed.
396-
if !ecx.tcx.sess.recursion_limit().value_within_limit(ecx.stack().len() + 1) {
396+
if !ecx.recursion_limit.value_within_limit(ecx.stack().len() + 1) {
397397
throw_exhaust!(StackFrameLimitReached)
398398
} else {
399399
Ok(frame)

‎compiler/rustc_mir/src/interpret/eval_context.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_middle::ty::layout::{self, TyAndLayout};
1313
use rustc_middle::ty::{
1414
self, query::TyCtxtAt, subst::SubstsRef, ParamEnv, Ty, TyCtxt, TypeFoldable,
1515
};
16+
use rustc_session::Limit;
1617
use rustc_span::{Pos, Span};
1718
use rustc_target::abi::{Align, HasDataLayout, LayoutOf, Size, TargetDataLayout};
1819

@@ -39,6 +40,9 @@ pub struct InterpCx<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
3940

4041
/// The virtual memory system.
4142
pub memory: Memory<'mir, 'tcx, M>,
43+
44+
/// The recursion limit (cached from `tcx.recursion_limit(())`)
45+
pub recursion_limit: Limit,
4246
}
4347

4448
// The Phantomdata exists to prevent this type from being `Send`. If it were sent across a thread
@@ -388,6 +392,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
388392
tcx: tcx.at(root_span),
389393
param_env,
390394
memory: Memory::new(tcx, memory_extra),
395+
recursion_limit: tcx.recursion_limit(),
391396
}
392397
}
393398

‎compiler/rustc_mir/src/monomorphize/collector.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ use rustc_middle::ty::{self, GenericParamDefKind, Instance, Ty, TyCtxt, TypeFold
200200
use rustc_middle::{middle::codegen_fn_attrs::CodegenFnAttrFlags, mir::visit::TyContext};
201201
use rustc_session::config::EntryFnType;
202202
use rustc_session::lint::builtin::LARGE_ASSIGNMENTS;
203+
use rustc_session::Limit;
203204
use rustc_span::source_map::{dummy_spanned, respan, Span, Spanned, DUMMY_SP};
204205
use rustc_target::abi::Size;
205206
use smallvec::SmallVec;
@@ -294,6 +295,7 @@ pub fn collect_crate_mono_items(
294295

295296
let mut visited = MTLock::new(FxHashSet::default());
296297
let mut inlining_map = MTLock::new(InliningMap::new());
298+
let recursion_limit = tcx.recursion_limit();
297299

298300
{
299301
let visited: MTRef<'_, _> = &mut visited;
@@ -307,6 +309,7 @@ pub fn collect_crate_mono_items(
307309
dummy_spanned(root),
308310
visited,
309311
&mut recursion_depths,
312+
recursion_limit,
310313
inlining_map,
311314
);
312315
});
@@ -350,6 +353,7 @@ fn collect_items_rec<'tcx>(
350353
starting_point: Spanned<MonoItem<'tcx>>,
351354
visited: MTRef<'_, MTLock<FxHashSet<MonoItem<'tcx>>>>,
352355
recursion_depths: &mut DefIdMap<usize>,
356+
recursion_limit: Limit,
353357
inlining_map: MTRef<'_, MTLock<InliningMap<'tcx>>>,
354358
) {
355359
if !visited.lock_mut().insert(starting_point.node) {
@@ -409,8 +413,13 @@ fn collect_items_rec<'tcx>(
409413
debug_assert!(should_codegen_locally(tcx, &instance));
410414

411415
// Keep track of the monomorphization recursion depth
412-
recursion_depth_reset =
413-
Some(check_recursion_limit(tcx, instance, starting_point.span, recursion_depths));
416+
recursion_depth_reset = Some(check_recursion_limit(
417+
tcx,
418+
instance,
419+
starting_point.span,
420+
recursion_depths,
421+
recursion_limit,
422+
));
414423
check_type_length_limit(tcx, instance);
415424

416425
rustc_data_structures::stack::ensure_sufficient_stack(|| {
@@ -455,7 +464,7 @@ fn collect_items_rec<'tcx>(
455464
record_accesses(tcx, starting_point.node, neighbors.iter().map(|i| &i.node), inlining_map);
456465

457466
for neighbour in neighbors {
458-
collect_items_rec(tcx, neighbour, visited, recursion_depths, inlining_map);
467+
collect_items_rec(tcx, neighbour, visited, recursion_depths, recursion_limit, inlining_map);
459468
}
460469

461470
if let Some((def_id, depth)) = recursion_depth_reset {
@@ -523,6 +532,7 @@ fn check_recursion_limit<'tcx>(
523532
instance: Instance<'tcx>,
524533
span: Span,
525534
recursion_depths: &mut DefIdMap<usize>,
535+
recursion_limit: Limit,
526536
) -> (DefId, usize) {
527537
let def_id = instance.def_id();
528538
let recursion_depth = recursion_depths.get(&def_id).cloned().unwrap_or(0);
@@ -539,7 +549,7 @@ fn check_recursion_limit<'tcx>(
539549
// Code that needs to instantiate the same function recursively
540550
// more than the recursion limit is assumed to be causing an
541551
// infinite expansion.
542-
if !tcx.sess.recursion_limit().value_within_limit(adjusted_recursion_depth) {
552+
if !recursion_limit.value_within_limit(adjusted_recursion_depth) {
543553
let (shrunk, written_to_path) = shrunk_instance_name(tcx, &instance, 32, 32);
544554
let error = format!("reached the recursion limit while instantiating `{}`", shrunk);
545555
let mut err = tcx.sess.struct_span_fatal(span, &error);
@@ -577,7 +587,7 @@ fn check_type_length_limit<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) {
577587
// which means that rustc basically hangs.
578588
//
579589
// Bail out in these cases to avoid that bad user experience.
580-
if !tcx.sess.type_length_limit().value_within_limit(type_length) {
590+
if !tcx.type_length_limit().value_within_limit(type_length) {
581591
let (shrunk, written_to_path) = shrunk_instance_name(tcx, &instance, 32, 32);
582592
let msg = format!("reached the type-length limit while instantiating `{}`", shrunk);
583593
let mut diag = tcx.sess.struct_span_fatal(tcx.def_span(instance.def_id()), &msg);
@@ -814,7 +824,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
814824

815825
fn visit_operand(&mut self, operand: &mir::Operand<'tcx>, location: Location) {
816826
self.super_operand(operand, location);
817-
let limit = self.tcx.sess.move_size_limit();
827+
let limit = self.tcx.move_size_limit().0;
818828
if limit == 0 {
819829
return;
820830
}

‎compiler/rustc_mir/src/transform/inline/cycle.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
55
use rustc_middle::mir::TerminatorKind;
66
use rustc_middle::ty::TypeFoldable;
77
use rustc_middle::ty::{self, subst::SubstsRef, InstanceDef, TyCtxt};
8+
use rustc_session::Limit;
89

910
// FIXME: check whether it is cheaper to precompute the entire call graph instead of invoking
1011
// this query riddiculously often.
@@ -30,7 +31,7 @@ crate fn mir_callgraph_reachable(
3031
);
3132
#[instrument(
3233
level = "debug",
33-
skip(tcx, param_env, target, stack, seen, recursion_limiter, caller)
34+
skip(tcx, param_env, target, stack, seen, recursion_limiter, caller, recursion_limit)
3435
)]
3536
fn process(
3637
tcx: TyCtxt<'tcx>,
@@ -40,6 +41,7 @@ crate fn mir_callgraph_reachable(
4041
stack: &mut Vec<ty::Instance<'tcx>>,
4142
seen: &mut FxHashSet<ty::Instance<'tcx>>,
4243
recursion_limiter: &mut FxHashMap<DefId, usize>,
44+
recursion_limit: Limit,
4345
) -> bool {
4446
trace!(%caller);
4547
for &(callee, substs) in tcx.mir_inliner_callees(caller.def) {
@@ -96,11 +98,20 @@ crate fn mir_callgraph_reachable(
9698
if seen.insert(callee) {
9799
let recursion = recursion_limiter.entry(callee.def_id()).or_default();
98100
trace!(?callee, recursion = *recursion);
99-
if tcx.sess.recursion_limit().value_within_limit(*recursion) {
101+
if recursion_limit.value_within_limit(*recursion) {
100102
*recursion += 1;
101103
stack.push(callee);
102104
let found_recursion = ensure_sufficient_stack(|| {
103-
process(tcx, param_env, callee, target, stack, seen, recursion_limiter)
105+
process(
106+
tcx,
107+
param_env,
108+
callee,
109+
target,
110+
stack,
111+
seen,
112+
recursion_limiter,
113+
recursion_limit,
114+
)
104115
});
105116
if found_recursion {
106117
return true;
@@ -122,6 +133,7 @@ crate fn mir_callgraph_reachable(
122133
&mut Vec::new(),
123134
&mut FxHashSet::default(),
124135
&mut FxHashMap::default(),
136+
tcx.recursion_limit(),
125137
)
126138
}
127139

‎compiler/rustc_session/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ pub use session::*;
2424
pub mod output;
2525

2626
pub use getopts;
27+
28+
/// Requirements for a `StableHashingContext` to be used in this crate.
29+
/// This is a hack to allow using the `HashStable_Generic` derive macro
30+
/// instead of implementing everything in `rustc_middle`.
31+
pub trait HashStableContext {}

‎compiler/rustc_session/src/session.rs

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc_errors::json::JsonEmitter;
2222
use rustc_errors::registry::Registry;
2323
use rustc_errors::{Diagnostic, DiagnosticBuilder, DiagnosticId, ErrorReported};
2424
use rustc_lint_defs::FutureBreakage;
25+
use rustc_macros::HashStable_Generic;
2526
pub use rustc_span::crate_disambiguator::CrateDisambiguator;
2627
use rustc_span::source_map::{FileLoader, MultiSpan, RealFileLoader, SourceMap, Span};
2728
use rustc_span::{edition::Edition, RealFileName};
@@ -66,7 +67,7 @@ pub enum CtfeBacktrace {
6667

6768
/// New-type wrapper around `usize` for representing limits. Ensures that comparisons against
6869
/// limits are consistent throughout the compiler.
69-
#[derive(Clone, Copy, Debug)]
70+
#[derive(Clone, Copy, Debug, HashStable_Generic)]
7071
pub struct Limit(pub usize);
7172

7273
impl Limit {
@@ -111,6 +112,20 @@ impl Mul<usize> for Limit {
111112
}
112113
}
113114

115+
#[derive(Clone, Copy, Debug, HashStable_Generic)]
116+
pub struct Limits {
117+
/// The maximum recursion limit for potentially infinitely recursive
118+
/// operations such as auto-dereference and monomorphization.
119+
pub recursion_limit: Limit,
120+
/// The size at which the `large_assignments` lint starts
121+
/// being emitted.
122+
pub move_size_limit: Limit,
123+
/// The maximum length of types during monomorphization.
124+
pub type_length_limit: Limit,
125+
/// The maximum blocks a const expression can evaluate.
126+
pub const_eval_limit: Limit,
127+
}
128+
114129
/// Represents the data associated with a compilation
115130
/// session for a single crate.
116131
pub struct Session {
@@ -144,20 +159,6 @@ pub struct Session {
144159

145160
lint_store: OnceCell<Lrc<dyn SessionLintStore>>,
146161

147-
/// The maximum recursion limit for potentially infinitely recursive
148-
/// operations such as auto-dereference and monomorphization.
149-
pub recursion_limit: OnceCell<Limit>,
150-
151-
/// The size at which the `large_assignments` lint starts
152-
/// being emitted.
153-
pub move_size_limit: OnceCell<usize>,
154-
155-
/// The maximum length of types during monomorphization.
156-
pub type_length_limit: OnceCell<Limit>,
157-
158-
/// The maximum blocks a const expression can evaluate.
159-
pub const_eval_limit: OnceCell<Limit>,
160-
161162
incr_comp_session: OneThread<RefCell<IncrCompSession>>,
162163
/// Used for incremental compilation tests. Will only be populated if
163164
/// `-Zquery-dep-graph` is specified.
@@ -347,25 +348,6 @@ impl Session {
347348
self.crate_types.set(crate_types).expect("`crate_types` was initialized twice")
348349
}
349350

350-
#[inline]
351-
pub fn recursion_limit(&self) -> Limit {
352-
self.recursion_limit.get().copied().unwrap()
353-
}
354-
355-
#[inline]
356-
pub fn move_size_limit(&self) -> usize {
357-
self.move_size_limit.get().copied().unwrap()
358-
}
359-
360-
#[inline]
361-
pub fn type_length_limit(&self) -> Limit {
362-
self.type_length_limit.get().copied().unwrap()
363-
}
364-
365-
pub fn const_eval_limit(&self) -> Limit {
366-
self.const_eval_limit.get().copied().unwrap()
367-
}
368-
369351
pub fn struct_span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> DiagnosticBuilder<'_> {
370352
self.diagnostic().struct_span_warn(sp, msg)
371353
}
@@ -1391,10 +1373,6 @@ pub fn build_session(
13911373
crate_disambiguator: OnceCell::new(),
13921374
features: OnceCell::new(),
13931375
lint_store: OnceCell::new(),
1394-
recursion_limit: OnceCell::new(),
1395-
move_size_limit: OnceCell::new(),
1396-
type_length_limit: OnceCell::new(),
1397-
const_eval_limit: OnceCell::new(),
13981376
incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),
13991377
cgu_reuse_tracker,
14001378
prof,

‎compiler/rustc_trait_selection/src/autoderef.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'a, 'tcx> Iterator for Autoderef<'a, 'tcx> {
5353
}
5454

5555
// If we have reached the recursion limit, error gracefully.
56-
if !tcx.sess.recursion_limit().value_within_limit(self.state.steps.len()) {
56+
if !tcx.recursion_limit().value_within_limit(self.state.steps.len()) {
5757
if !self.silence_errors {
5858
report_autoderef_recursion_limit_error(tcx, self.span, self.state.cur_ty);
5959
}
@@ -217,7 +217,7 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
217217

218218
pub fn report_autoderef_recursion_limit_error<'tcx>(tcx: TyCtxt<'tcx>, span: Span, ty: Ty<'tcx>) {
219219
// We've reached the recursion limit, error gracefully.
220-
let suggested_limit = tcx.sess.recursion_limit() * 2;
220+
let suggested_limit = tcx.recursion_limit() * 2;
221221
let msg = format!("reached the recursion limit while auto-dereferencing `{:?}`", ty);
222222
let error_id = (DiagnosticMessageId::ErrorId(55), Some(span), msg);
223223
let fresh = tcx.sess.one_time_diagnostics.borrow_mut().insert(error_id);

‎compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2310,7 +2310,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
23102310
}
23112311

23122312
fn suggest_new_overflow_limit(&self, err: &mut DiagnosticBuilder<'_>) {
2313-
let current_limit = self.tcx.sess.recursion_limit();
2313+
let current_limit = self.tcx.recursion_limit();
23142314
let suggested_limit = current_limit * 2;
23152315
err.help(&format!(
23162316
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate (`{}`)",

‎compiler/rustc_trait_selection/src/traits/project.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> {
344344
Reveal::UserFacing => ty,
345345

346346
Reveal::All => {
347-
let recursion_limit = self.tcx().sess.recursion_limit();
347+
let recursion_limit = self.tcx().recursion_limit();
348348
if !recursion_limit.value_within_limit(self.depth) {
349349
let obligation = Obligation::with_depth(
350350
self.cause.clone(),
@@ -726,7 +726,7 @@ fn project_type<'cx, 'tcx>(
726726
) -> Result<ProjectedTy<'tcx>, ProjectionTyError<'tcx>> {
727727
debug!(?obligation, "project_type");
728728

729-
if !selcx.tcx().sess.recursion_limit().value_within_limit(obligation.recursion_depth) {
729+
if !selcx.tcx().recursion_limit().value_within_limit(obligation.recursion_depth) {
730730
debug!("project: overflow!");
731731
// This should really be an immediate error, but some existing code
732732
// relies on being able to recover from this.

‎compiler/rustc_trait_selection/src/traits/query/normalize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
116116
Reveal::UserFacing => ty,
117117

118118
Reveal::All => {
119-
let recursion_limit = self.tcx().sess.recursion_limit();
119+
let recursion_limit = self.tcx().recursion_limit();
120120
if !recursion_limit.value_within_limit(self.anon_depth) {
121121
let obligation = Obligation::with_depth(
122122
self.cause.clone(),

‎compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
993993
obligation: &Obligation<'tcx, T>,
994994
error_obligation: &Obligation<'tcx, V>,
995995
) -> Result<(), OverflowError> {
996-
if !self.infcx.tcx.sess.recursion_limit().value_within_limit(obligation.recursion_depth) {
996+
if !self.infcx.tcx.recursion_limit().value_within_limit(obligation.recursion_depth) {
997997
match self.query_mode {
998998
TraitQueryMode::Standard => {
999999
self.infcx().report_overflow_error(error_obligation, true);

‎compiler/rustc_traits/src/dropck_outlives.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ fn dtorck_constraint_for_ty<'tcx>(
163163
) -> Result<(), NoSolution> {
164164
debug!("dtorck_constraint_for_ty({:?}, {:?}, {:?}, {:?})", span, for_ty, depth, ty);
165165

166-
if !tcx.sess.recursion_limit().value_within_limit(depth) {
166+
if !tcx.recursion_limit().value_within_limit(depth) {
167167
constraints.overflows.push(ty);
168168
return Ok(());
169169
}

‎compiler/rustc_ty_utils/src/needs_drop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<'tcx, F> NeedsDropTypes<'tcx, F> {
6363
seen_tys,
6464
query_ty: ty,
6565
unchecked_tys: vec![(ty, 0)],
66-
recursion_limit: tcx.sess.recursion_limit(),
66+
recursion_limit: tcx.recursion_limit(),
6767
adt_components,
6868
}
6969
}

‎src/test/ui/recursion_limit/empty.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22

33
#![recursion_limit = ""] //~ ERROR `limit` must be a non-negative integer
44
//~| `limit` must be a non-negative integer
5+
//~| ERROR `limit` must be a non-negative integer
6+
//~| `limit` must be a non-negative integer
57

68
fn main() {}

‎src/test/ui/recursion_limit/empty.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,13 @@ LL | #![recursion_limit = ""]
66
| |
77
| `limit` must be a non-negative integer
88

9-
error: aborting due to previous error
9+
error: `limit` must be a non-negative integer
10+
--> $DIR/empty.rs:3:1
11+
|
12+
LL | #![recursion_limit = ""]
13+
| ^^^^^^^^^^^^^^^^^^^^^--^
14+
| |
15+
| `limit` must be a non-negative integer
16+
17+
error: aborting due to 2 previous errors
1018

‎src/test/ui/recursion_limit/invalid_digit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
#![recursion_limit = "-100"] //~ ERROR `limit` must be a non-negative integer
44
//~| not a valid integer
5-
5+
//~| ERROR `limit` must be a non-negative integer
6+
//~| not a valid integer
67
fn main() {}

‎src/test/ui/recursion_limit/invalid_digit.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,13 @@ LL | #![recursion_limit = "-100"]
66
| |
77
| not a valid integer
88

9-
error: aborting due to previous error
9+
error: `limit` must be a non-negative integer
10+
--> $DIR/invalid_digit.rs:3:1
11+
|
12+
LL | #![recursion_limit = "-100"]
13+
| ^^^^^^^^^^^^^^^^^^^^^------^
14+
| |
15+
| not a valid integer
16+
17+
error: aborting due to 2 previous errors
1018

‎src/test/ui/recursion_limit/overflow.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
#![recursion_limit = "999999999999999999999999"]
44
//~^ ERROR `limit` must be a non-negative integer
55
//~| `limit` is too large
6+
//~| ERROR `limit` must be a non-negative integer
7+
//~| `limit` is too large
68

79
fn main() {}

‎src/test/ui/recursion_limit/overflow.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,13 @@ LL | #![recursion_limit = "999999999999999999999999"]
66
| |
77
| `limit` is too large
88

9-
error: aborting due to previous error
9+
error: `limit` must be a non-negative integer
10+
--> $DIR/overflow.rs:3:1
11+
|
12+
LL | #![recursion_limit = "999999999999999999999999"]
13+
| ^^^^^^^^^^^^^^^^^^^^^--------------------------^
14+
| |
15+
| `limit` is too large
16+
17+
error: aborting due to 2 previous errors
1018

0 commit comments

Comments
 (0)
Please sign in to comment.