]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/recursion_limit.rs
handle errors based on parse_sess
[rust.git] / src / librustc / middle / recursion_limit.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Recursion limit.
12 //
13 // There are various parts of the compiler that must impose arbitrary limits
14 // on how deeply they recurse to prevent stack overflow. Users can override
15 // this via an attribute on the crate like `#![recursion_limit="22"]`. This pass
16 // just peeks and looks for that attribute.
17
18 use session::Session;
19 use syntax::ast;
20
21 use rustc_data_structures::sync::Once;
22
23 pub fn update_limits(sess: &Session, krate: &ast::Crate) {
24     update_limit(sess, krate, &sess.recursion_limit, "recursion_limit",
25                  "recursion limit", 64);
26     update_limit(sess, krate, &sess.type_length_limit, "type_length_limit",
27                  "type length limit", 1048576);
28 }
29
30 fn update_limit(sess: &Session, krate: &ast::Crate, limit: &Once<usize>,
31                 name: &str, description: &str, default: usize) {
32     for attr in &krate.attrs {
33         if !attr.check_name(name) {
34             continue;
35         }
36
37         if let Some(s) = attr.value_str() {
38             if let Some(n) = s.as_str().parse().ok() {
39                 limit.set(n);
40                 return;
41             }
42         }
43
44         span_err!(sess, attr.span, E0296,
45                   "malformed {} attribute, expected #![{}=\"N\"]",
46                   description, name);
47     }
48     limit.set(default);
49 }