Skip to content

Commit 7e5a88a

Browse files
committed
Combine individual limit queries into single limits query
1 parent ff15b5e commit 7e5a88a

File tree

20 files changed

+75
-55
lines changed

20 files changed

+75
-55
lines changed

Diff for: compiler/rustc_interface/src/passes.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -872,13 +872,11 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
872872
});
873873
},
874874
{
875-
// Mark the attributes as used, and ensure that
876-
// they're not ill-formed. We force these queries
877-
// to run, since they might not otherwise get called.
878-
tcx.ensure().recursion_limit(());
879-
tcx.ensure().move_size_limit(());
880-
tcx.ensure().type_length_limit(());
881-
tcx.ensure().const_eval_limit(());
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(());
882880
}
883881
);
884882
});

Diff for: compiler/rustc_middle/src/middle/limits.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,29 @@
1212
use crate::bug;
1313
use crate::ty;
1414
use rustc_ast::Attribute;
15-
use rustc_session::Limit;
1615
use rustc_session::Session;
16+
use rustc_session::{Limit, Limits};
1717
use rustc_span::symbol::{sym, Symbol};
1818

1919
use std::num::IntErrorKind;
2020

2121
pub fn provide(providers: &mut ty::query::Providers) {
22-
providers.recursion_limit = |tcx, ()| get_recursion_limit(tcx.hir().krate_attrs(), tcx.sess);
23-
providers.move_size_limit =
24-
|tcx, ()| get_limit(tcx.hir().krate_attrs(), tcx.sess, sym::move_size_limit, 0).0;
25-
providers.type_length_limit =
26-
|tcx, ()| get_limit(tcx.hir().krate_attrs(), tcx.sess, sym::type_length_limit, 1048576);
27-
providers.const_eval_limit =
28-
|tcx, ()| get_limit(tcx.hir().krate_attrs(), tcx.sess, sym::const_eval_limit, 1_000_000);
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+
}
2938
}
3039

3140
pub fn get_recursion_limit(krate_attrs: &[Attribute], sess: &Session) -> Limit {

Diff for: compiler/rustc_middle/src/query/mod.rs

+2-20
Original file line numberDiff line numberDiff line change
@@ -1713,25 +1713,7 @@ rustc_queries! {
17131713
desc { "conservatively checking if {:?} is privately uninhabited", key }
17141714
}
17151715

1716-
/// The maximum recursion limit for potentially infinitely recursive
1717-
/// operations such as auto-dereference and monomorphization.
1718-
query recursion_limit(key: ()) -> Limit {
1719-
desc { "looking up recursion limit" }
1720-
}
1721-
1722-
/// The size at which the `large_assignments` lint starts
1723-
/// being emitted.
1724-
query move_size_limit(key: ()) -> usize {
1725-
desc { "looking up move size limit" }
1726-
}
1727-
1728-
/// The maximum length of types during monomorphization.
1729-
query type_length_limit(key: ()) -> Limit {
1730-
desc { "looking up type length limit" }
1731-
}
1732-
1733-
/// The maximum blocks a const expression can evaluate.
1734-
query const_eval_limit(key: ()) -> Limit {
1735-
desc { "looking up const eval limit" }
1716+
query limits(key: ()) -> Limits {
1717+
desc { "looking up limits" }
17361718
}
17371719
}

Diff for: compiler/rustc_middle/src/ty/context.rs

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

Diff for: compiler/rustc_middle/src/ty/layout.rs

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

Diff for: compiler/rustc_middle/src/ty/print/pretty.rs

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

Diff for: compiler/rustc_middle/src/ty/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +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::Limit;
52+
use rustc_session::Limits;
5353
use rustc_target::spec::PanicStrategy;
5454

5555
use rustc_ast as ast;

Diff for: compiler/rustc_middle/src/ty/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ 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(());
209+
let recursion_limit = self.recursion_limit();
210210
for iteration in 0.. {
211211
if !recursion_limit.value_within_limit(iteration) {
212212
return self.ty_error_with_message(

Diff for: compiler/rustc_mir/src/const_eval/eval_queries.rs

+2-2
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.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.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 },

Diff for: compiler/rustc_mir/src/interpret/eval_context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
392392
tcx: tcx.at(root_span),
393393
param_env,
394394
memory: Memory::new(tcx, memory_extra),
395-
recursion_limit: tcx.recursion_limit(()),
395+
recursion_limit: tcx.recursion_limit(),
396396
}
397397
}
398398

Diff for: compiler/rustc_mir/src/monomorphize/collector.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ pub fn collect_crate_mono_items(
295295

296296
let mut visited = MTLock::new(FxHashSet::default());
297297
let mut inlining_map = MTLock::new(InliningMap::new());
298-
let recursion_limit = tcx.recursion_limit(());
298+
let recursion_limit = tcx.recursion_limit();
299299

300300
{
301301
let visited: MTRef<'_, _> = &mut visited;
@@ -587,7 +587,7 @@ fn check_type_length_limit<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) {
587587
// which means that rustc basically hangs.
588588
//
589589
// Bail out in these cases to avoid that bad user experience.
590-
if !tcx.type_length_limit(()).value_within_limit(type_length) {
590+
if !tcx.type_length_limit().value_within_limit(type_length) {
591591
let (shrunk, written_to_path) = shrunk_instance_name(tcx, &instance, 32, 32);
592592
let msg = format!("reached the type-length limit while instantiating `{}`", shrunk);
593593
let mut diag = tcx.sess.struct_span_fatal(tcx.def_span(instance.def_id()), &msg);
@@ -824,7 +824,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
824824

825825
fn visit_operand(&mut self, operand: &mir::Operand<'tcx>, location: Location) {
826826
self.super_operand(operand, location);
827-
let limit = self.tcx.move_size_limit(());
827+
let limit = self.tcx.move_size_limit().0;
828828
if limit == 0 {
829829
return;
830830
}

Diff for: compiler/rustc_mir/src/transform/inline/cycle.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ crate fn mir_callgraph_reachable(
133133
&mut Vec::new(),
134134
&mut FxHashSet::default(),
135135
&mut FxHashMap::default(),
136-
tcx.recursion_limit(()),
136+
tcx.recursion_limit(),
137137
)
138138
}
139139

Diff for: compiler/rustc_session/src/session.rs

+14
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ impl Mul<usize> for Limit {
112112
}
113113
}
114114

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+
115129
/// Represents the data associated with a compilation
116130
/// session for a single crate.
117131
pub struct Session {

Diff for: compiler/rustc_trait_selection/src/autoderef.rs

+2-2
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.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.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);

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+1-1
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.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 (`{}`)",

Diff for: compiler/rustc_trait_selection/src/traits/project.rs

+2-2
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().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().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.

Diff for: compiler/rustc_trait_selection/src/traits/query/normalize.rs

+1-1
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().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(),

Diff for: compiler/rustc_trait_selection/src/traits/select/mod.rs

+1-1
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.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);

Diff for: compiler/rustc_traits/src/dropck_outlives.rs

+1-1
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.recursion_limit(()).value_within_limit(depth) {
166+
if !tcx.recursion_limit().value_within_limit(depth) {
167167
constraints.overflows.push(ty);
168168
return Ok(());
169169
}

Diff for: compiler/rustc_ty_utils/src/needs_drop.rs

+1-1
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.recursion_limit(()),
66+
recursion_limit: tcx.recursion_limit(),
6767
adt_components,
6868
}
6969
}

0 commit comments

Comments
 (0)