Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions src/attributes/limits.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,49 @@ r[attributes.limits.recursion_limit]
## The `recursion_limit` attribute

r[attributes.limits.recursion_limit.intro]
The *`recursion_limit` attribute* may be applied at the [crate] level to set the
maximum depth for potentially infinitely-recursive compile-time operations
like macro expansion or auto-dereference.
The *`recursion_limit` [attribute][attributes]* sets the maximum depth for potentially infinitely-recursive compile-time operations like macro expansion or auto-dereference.

r[attributes.limits.recursion_limit.syntax]
It uses the [MetaNameValueStr]
syntax to specify the recursion depth.
> [!EXAMPLE]
> ```rust,compile_fail
> #![recursion_limit = "4"]
>
> 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!{}
> ```

> [!EXAMPLE]
> ```rust,compile_fail
> #![recursion_limit = "1"]
>
> // This fails because it requires two recursive steps to auto-dereference.
> (|_: &u8| {})(&&&1);
> ```

> [!NOTE]
> The default in `rustc` is 128.
> The default recursion limit in `rustc` is 128.

```rust,compile_fail
#![recursion_limit = "4"]
r[attributes.limits.recursion_limit.syntax]
The `recursion_limit` attribute uses the [MetaNameValueStr] syntax to specify the recursion depth. The value in the string must be a non-negative integer.

macro_rules! a {
() => { a!(1); };
(1) => { a!(2); };
(2) => { a!(3); };
(3) => { a!(4); };
(4) => { };
}
r[attributes.limits.recursion_limit.allowed-positions]
The `recursion_limit` attribute may only be applied to the crate root.

// This fails to expand because it requires a recursion depth greater than 4.
a!{}
```
> [!NOTE]
> `rustc` currently warns in other positions, but this may be rejected in the future.

```rust,compile_fail
#![recursion_limit = "1"]
r[attributes.limits.recursion_limit.duplicates]
Only the first instance of `recursion_limit` on an item is honored. Subsequent `recursion_limit` attributes are ignored.

// This fails because it requires two recursive steps to auto-dereference.
(|_: &u8| {})(&&&1);
```
> [!NOTE]
> `rustc` currently warns on following duplicate `recursion_limit` attributes. This may become an error in the future.

r[attributes.limits.type_length_limit]
## The `type_length_limit` attribute
Expand Down