2019-12-14 18:51:56 +01:00
|
|
|
//! Registering limits, recursion_limit, type_length_limit and const_eval_limit
|
2019-12-13 09:38:07 +01:00
|
|
|
//!
|
|
|
|
|
//! There are various parts of the compiler that must impose arbitrary limits
|
|
|
|
|
//! on how deeply they recurse to prevent stack overflow. Users can override
|
|
|
|
|
//! this via an attribute on the crate like `#![recursion_limit="22"]`. This pass
|
|
|
|
|
//! just peeks and looks for that attribute.
|
2014-12-02 12:57:38 -05:00
|
|
|
|
2020-03-29 16:41:09 +02:00
|
|
|
use crate::bug;
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast as ast;
|
2020-05-15 21:44:28 -07:00
|
|
|
use rustc_data_structures::sync::OnceCell;
|
2020-05-26 19:48:08 +01:00
|
|
|
use rustc_session::{Limit, Session};
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
2014-12-02 12:57:38 -05:00
|
|
|
|
2020-03-11 12:49:08 +01:00
|
|
|
use std::num::IntErrorKind;
|
2016-11-15 23:25:59 +02:00
|
|
|
|
|
|
|
|
pub fn update_limits(sess: &Session, krate: &ast::Crate) {
|
2019-12-12 21:18:21 -07:00
|
|
|
update_limit(sess, krate, &sess.recursion_limit, sym::recursion_limit, 128);
|
|
|
|
|
update_limit(sess, krate, &sess.type_length_limit, sym::type_length_limit, 1048576);
|
2019-12-14 18:51:56 +01:00
|
|
|
update_limit(sess, krate, &sess.const_eval_limit, sym::const_eval_limit, 1_000_000);
|
2016-11-15 23:25:59 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-12 21:18:21 -07:00
|
|
|
fn update_limit(
|
|
|
|
|
sess: &Session,
|
|
|
|
|
krate: &ast::Crate,
|
2020-05-26 19:48:08 +01:00
|
|
|
limit: &OnceCell<Limit>,
|
2019-12-12 21:18:21 -07:00
|
|
|
name: Symbol,
|
|
|
|
|
default: usize,
|
|
|
|
|
) {
|
2015-01-31 12:20:46 -05:00
|
|
|
for attr in &krate.attrs {
|
2020-07-30 11:27:50 +10:00
|
|
|
if !sess.check_name(attr, name) {
|
2014-12-02 12:57:38 -05:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(s) = attr.value_str() {
|
2019-12-12 21:18:21 -07:00
|
|
|
match s.as_str().parse() {
|
|
|
|
|
Ok(n) => {
|
2020-05-26 19:48:08 +01:00
|
|
|
limit.set(Limit::new(n)).unwrap();
|
2019-12-12 21:18:21 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
2020-02-19 10:24:16 +01:00
|
|
|
let mut err =
|
|
|
|
|
sess.struct_span_err(attr.span, "`limit` must be a non-negative integer");
|
2019-12-12 21:18:21 -07:00
|
|
|
|
|
|
|
|
let value_span = attr
|
|
|
|
|
.meta()
|
|
|
|
|
.and_then(|meta| meta.name_value_literal().cloned())
|
|
|
|
|
.map(|lit| lit.span)
|
|
|
|
|
.unwrap_or(attr.span);
|
|
|
|
|
|
|
|
|
|
let error_str = match e.kind() {
|
2020-10-06 14:06:25 +01:00
|
|
|
IntErrorKind::PosOverflow => "`limit` is too large",
|
2020-10-06 22:42:33 +01:00
|
|
|
IntErrorKind::Empty => "`limit` must be a non-negative integer",
|
2020-10-26 18:14:12 +00:00
|
|
|
IntErrorKind::InvalidDigit => "not a valid integer",
|
|
|
|
|
IntErrorKind::NegOverflow => {
|
|
|
|
|
bug!("`limit` should never negatively underflow")
|
|
|
|
|
}
|
2020-02-19 10:24:16 +01:00
|
|
|
IntErrorKind::Zero => bug!("zero is a valid `limit`"),
|
2019-12-12 21:18:21 -07:00
|
|
|
kind => bug!("unimplemented IntErrorKind variant: {:?}", kind),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
err.span_label(value_span, error_str);
|
|
|
|
|
err.emit();
|
|
|
|
|
}
|
2014-12-02 12:57:38 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-26 19:48:08 +01:00
|
|
|
limit.set(Limit::new(default)).unwrap();
|
2014-12-02 12:57:38 -05:00
|
|
|
}
|