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.
14 use rustc_ast::Attribute;
15 use rustc_session::Session;
16 use rustc_session::{Limit, Limits};
17 use rustc_span::symbol::{sym, Symbol};
19 use std::num::IntErrorKind;
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(
25 tcx.hir().krate_attrs(),
28 tcx.sess.opts.debugging_opts.move_size_limit.unwrap_or(0),
30 type_length_limit: get_limit(
31 tcx.hir().krate_attrs(),
33 sym::type_length_limit,
36 const_eval_limit: get_limit(
37 tcx.hir().krate_attrs(),
39 sym::const_eval_limit,
45 pub fn get_recursion_limit(krate_attrs: &[Attribute], sess: &Session) -> Limit {
46 get_limit(krate_attrs, sess, sym::recursion_limit, 128)
49 fn get_limit(krate_attrs: &[Attribute], sess: &Session, name: Symbol, default: usize) -> Limit {
50 for attr in krate_attrs {
51 if !attr.has_name(name) {
55 if let Some(s) = attr.value_str() {
56 match s.as_str().parse() {
57 Ok(n) => return Limit::new(n),
60 sess.struct_span_err(attr.span, "`limit` must be a non-negative integer");
64 .and_then(|meta| meta.name_value_literal_span())
65 .unwrap_or(attr.span);
67 let error_str = match e.kind() {
68 IntErrorKind::PosOverflow => "`limit` is too large",
69 IntErrorKind::Empty => "`limit` must be a non-negative integer",
70 IntErrorKind::InvalidDigit => "not a valid integer",
71 IntErrorKind::NegOverflow => {
72 bug!("`limit` should never negatively overflow")
74 IntErrorKind::Zero => bug!("zero is a valid `limit`"),
75 kind => bug!("unimplemented IntErrorKind variant: {:?}", kind),
78 err.span_label(value_span, error_str);
84 return Limit::new(default);