]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/middle/limits.rs
Rollup merge of #105837 - compiler-errors:issue-105728, r=estebank
[rust.git] / compiler / rustc_middle / src / middle / limits.rs
1 //! Registering limits:
2 //! * recursion_limit,
3 //! * move_size_limit,
4 //! * type_length_limit, and
5 //! * const_eval_limit
6 //!
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.
11
12 use crate::bug;
13 use crate::error::LimitInvalid;
14 use crate::ty;
15 use rustc_ast::Attribute;
16 use rustc_session::Session;
17 use rustc_session::{Limit, Limits};
18 use rustc_span::symbol::{sym, Symbol};
19
20 use std::num::IntErrorKind;
21
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(),
27             tcx.sess,
28             sym::move_size_limit,
29             tcx.sess.opts.unstable_opts.move_size_limit.unwrap_or(0),
30         ),
31         type_length_limit: get_limit(
32             tcx.hir().krate_attrs(),
33             tcx.sess,
34             sym::type_length_limit,
35             1048576,
36         ),
37         const_eval_limit: get_limit(
38             tcx.hir().krate_attrs(),
39             tcx.sess,
40             sym::const_eval_limit,
41             2_000_000,
42         ),
43     }
44 }
45
46 pub fn get_recursion_limit(krate_attrs: &[Attribute], sess: &Session) -> Limit {
47     get_limit(krate_attrs, sess, sym::recursion_limit, 128)
48 }
49
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) {
53             continue;
54         }
55
56         if let Some(s) = attr.value_str() {
57             match s.as_str().parse() {
58                 Ok(n) => return Limit::new(n),
59                 Err(e) => {
60                     let value_span = attr
61                         .meta()
62                         .and_then(|meta| meta.name_value_literal_span())
63                         .unwrap_or(attr.span);
64
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")
71                         }
72                         IntErrorKind::Zero => bug!("zero is a valid `limit`"),
73                         kind => bug!("unimplemented IntErrorKind variant: {:?}", kind),
74                     };
75                     sess.emit_err(LimitInvalid { span: attr.span, value_span, error_str });
76                 }
77             }
78         }
79     }
80     return Limit::new(default);
81 }