限制
以下属性影响编译时限制。
recursion_limit 属性
recursion_limit 属性可应用于 crate 级别,以设置潜在无限递归编译时操作(如宏展开或自动解引用)的最大深度。
它使用 MetaNameValueStr 语法指定递归深度。
Note
rustc中的默认值为 128。
#![allow(unused)]
#![recursion_limit = "4"]
fn main() {
macro_rules! a {
() => { a!(1); };
(1) => { a!(2); };
(2) => { a!(3); };
(3) => { a!(4); };
(4) => { };
}
// This fails to expand because it requires a recursion depth greater than 4.
a!{}
}
#![allow(unused)]
#![recursion_limit = "1"]
fn main() {
// This fails because it requires two recursive steps to auto-dereference.
(|_: &u8| {})(&&&1);
}
type_length_limit 属性
type_length_limit 属性 设置在单态化期间构造具体类型时允许的最大类型替换数量。
Note
rustc仅在 nightly-Zenforce-type-length-limit标志激活时才强制执行此限制。有关更多信息,请参阅 Rust PR #127670。
Example
#![type_length_limit = "4"] fn f<T>(x: T) {} // This fails to compile because monomorphizing to // `f::<((((i32,), i32), i32), i32)>` requires more // than 4 type elements. f(((((1,), 2), 3), 4));
Note
rustc中的默认值为1048576。
type_length_limit 属性使用 MetaNameValueStr 语法。字符串中的值必须是非负数。
type_length_limit 属性只能应用于 crate 根。
Note
rustc会忽略在其他位置的使用,但会发出 lint 警告。这可能在未来成为错误。
只有项上的第一次使用 type_length_limit 才有效果。
Note
rustc会对第一次之后的任何使用发出 lint 警告。这可能在未来成为错误。