Skip to content

Commit ff15b5e

Browse files
committed
Query-ify global limit attribute handling
1 parent 9044245 commit ff15b5e

File tree

30 files changed

+153
-91
lines changed

30 files changed

+153
-91
lines changed

compiler/rustc_interface/src/passes.rs

Lines changed: 12 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,15 @@ 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+
// 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(());
875882
}
876883
);
877884
});

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: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,37 @@
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;
15+
use rustc_session::Limit;
1516
use rustc_session::Session;
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.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);
2529
}
2630

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 {
31+
pub fn get_recursion_limit(krate_attrs: &[Attribute], sess: &Session) -> Limit {
32+
get_limit(krate_attrs, sess, sym::recursion_limit, 128)
33+
}
34+
35+
fn get_limit(krate_attrs: &[Attribute], sess: &Session, name: Symbol, default: usize) -> Limit {
36+
for attr in krate_attrs {
3537
if !sess.check_name(attr, name) {
3638
continue;
3739
}
3840

3941
if let Some(s) = attr.value_str() {
4042
match s.as_str().parse() {
41-
Ok(n) => {
42-
limit.set(From::from(n)).unwrap();
43-
return;
44-
}
43+
Ok(n) => return Limit::new(n),
4544
Err(e) => {
4645
let mut err =
4746
sess.struct_span_err(attr.span, "`limit` must be a non-negative integer");
@@ -68,5 +67,5 @@ fn update_limit(
6867
}
6968
}
7069
}
71-
limit.set(From::from(default)).unwrap();
70+
return Limit::new(default);
7271
}

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,4 +1712,26 @@ rustc_queries! {
17121712
query conservative_is_privately_uninhabited(key: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
17131713
desc { "conservatively checking if {:?} is privately uninhabited", key }
17141714
}
1715+
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" }
1736+
}
17151737
}

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::Limit;
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),

0 commit comments

Comments
 (0)