]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/recursion_limit.rs
Auto merge of #61203 - memoryruins:bare_trait_objects, r=Centril
[rust.git] / src / librustc / middle / recursion_limit.rs
1 // Recursion limit.
2 //
3 // There are various parts of the compiler that must impose arbitrary limits
4 // on how deeply they recurse to prevent stack overflow. Users can override
5 // this via an attribute on the crate like `#![recursion_limit="22"]`. This pass
6 // just peeks and looks for that attribute.
7
8 use crate::session::Session;
9 use syntax::ast;
10 use syntax::symbol::{Symbol, sym};
11
12 use rustc_data_structures::sync::Once;
13
14 pub fn update_limits(sess: &Session, krate: &ast::Crate) {
15     update_limit(krate, &sess.recursion_limit, sym::recursion_limit, 64);
16     update_limit(krate, &sess.type_length_limit, sym::type_length_limit, 1048576);
17 }
18
19 fn update_limit(krate: &ast::Crate, limit: &Once<usize>, name: Symbol, default: usize) {
20     for attr in &krate.attrs {
21         if !attr.check_name(name) {
22             continue;
23         }
24
25         if let Some(s) = attr.value_str() {
26             if let Some(n) = s.as_str().parse().ok() {
27                 limit.set(n);
28                 return;
29             }
30         }
31     }
32     limit.set(default);
33 }