限制

以下属性会影响编译时限制。

recursion_limit 属性

recursion_limit 属性可以应用于crate 级别,以设置潜在的无限递归编译时操作(如宏展开或自动解引用)的最大深度。

它使用MetaNameValueStr 语法来指定递归深度。

注意: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 属性

注意:此限制仅在 nightly -Zenforce-type-length-limit 标志激活时强制执行。

更多信息,请参阅 https://github.com/rust-lang/rust/pull/127670

type_length_limit 属性限制了在单态化期间构造具体类型时进行的最大类型替换次数。

它应用于 crate 级别,并使用 MetaNameValueStr 语法来设置基于类型替换次数的限制。

注意:rustc 中的默认值为 1048576。

#![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));