1 //! Registering limits:
4 //! * type_length_limit, and
7 //! There are various parts of the compiler that must impose arbitrary limits
8 //! on how deeply they recurse to prevent stack overflow. Users can override
9 //! this via an attribute on the crate like `#![recursion_limit="22"]`. This pass
10 //! just peeks and looks for that attribute.
13 use crate::error::LimitInvalid;
15 use rustc_ast::Attribute;
16 use rustc_session::Session;
17 use rustc_session::{Limit, Limits};
18 use rustc_span::symbol::{sym, Symbol};
20 use std::num::IntErrorKind;
22 pub fn provide(providers: &mut ty::query::Providers) {
23 providers.limits = |tcx, ()| Limits {
24 recursion_limit: get_recursion_limit(tcx.hir().krate_attrs(), tcx.sess),
25 move_size_limit: get_limit(
26 tcx.hir().krate_attrs(),
29 tcx.sess.opts.unstable_opts.move_size_limit.unwrap_or(0),
31 type_length_limit: get_limit(
32 tcx.hir().krate_attrs(),
34 sym::type_length_limit,
37 const_eval_limit: get_limit(
38 tcx.hir().krate_attrs(),
40 sym::const_eval_limit,
46 pub fn get_recursion_limit(krate_attrs: &[Attribute], sess: &Session) -> Limit {
47 get_limit(krate_attrs, sess, sym::recursion_limit, 128)
50 fn get_limit(krate_attrs: &[Attribute], sess: &Session, name: Symbol, default: usize) -> Limit {
51 for attr in krate_attrs {
52 if !attr.has_name(name) {
56 if let Some(s) = attr.value_str() {
57 match s.as_str().parse() {
58 Ok(n) => return Limit::new(n),
62 .and_then(|meta| meta.name_value_literal_span())
63 .unwrap_or(attr.span);
65 let error_str = match e.kind() {
66 IntErrorKind::PosOverflow => "`limit` is too large",
67 IntErrorKind::Empty => "`limit` must be a non-negative integer",
68 IntErrorKind::InvalidDigit => "not a valid integer",
69 IntErrorKind::NegOverflow => {
70 bug!("`limit` should never negatively overflow")
72 IntErrorKind::Zero => bug!("zero is a valid `limit`"),
73 kind => bug!("unimplemented IntErrorKind variant: {:?}", kind),
75 sess.emit_err(LimitInvalid { span: attr.span, value_span, error_str });
80 return Limit::new(default);