Skip to content

Commit a8239e1

Browse files
authored
Merge pull request #546 from ehuss/type_length_limit
Document `type_length_limit`.
2 parents 7acdcad + 17060fd commit a8239e1

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/attributes.md

+2
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ The following is an index of all built-in attributes.
228228
- Limits
229229
- [`recursion_limit`] — Sets the maximum recursion limit for certain
230230
compile-time operations.
231+
- [`type_length_limit`] — Sets the maximum size of a polymorphic type.
231232
- Runtime
232233
- [`panic_handler`] — Sets the function to handle panics.
233234
- [`global_allocator`] — Sets the global memory allocator.
@@ -285,6 +286,7 @@ The following is an index of all built-in attributes.
285286
[`should_panic`]: attributes/testing.html#the-should_panic-attribute
286287
[`target_feature`]: attributes/codegen.html#the-target_feature-attribute
287288
[`test`]: attributes/testing.html#the-test-attribute
289+
[`type_length_limit`]: attributes/limits.html#the-type_length_limit-attribute
288290
[`used`]: abi.html#the-used-attribute
289291
[`warn`]: attributes/diagnostics.html#lint-check-attributes
290292
[`windows_subsystem`]: runtime.html#the-windows_subsystem-attribute

src/attributes/limits.md

+49-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,55 @@ The following [attributes] affect compile-time limits.
44

55
## The `recursion_limit` attribute
66

7-
The *`recursion_limit` attribute* may be applied at the crate level to set the
7+
The *`recursion_limit` attribute* may be applied at the [crate] level to set the
88
maximum depth for potentially infinitely-recursive compile-time operations
9-
like auto-dereference or macro expansion. It uses the [_MetaNameValueStr_]
10-
syntax to specify the recursion depth. The default is
11-
`#![recursion_limit="64"]`.
9+
like macro expansion or auto-dereference. It uses the [_MetaNameValueStr_]
10+
syntax to specify the recursion depth.
11+
12+
> Note: The default in `rustc` is 64.
13+
14+
```rust,compile_fail
15+
#![recursion_limit = "4"]
16+
17+
macro_rules! a {
18+
() => { a!(1) };
19+
(1) => { a!(2) };
20+
(2) => { a!(3) };
21+
(3) => { a!(4) };
22+
(4) => { };
23+
}
24+
25+
// This fails to expand because it requires a recursion depth greater than 4.
26+
a!{}
27+
```
28+
29+
```rust,compile_fail
30+
#![recursion_limit = "1"]
31+
32+
// This fails because it requires two recursive steps to auto-derefence.
33+
(|_: &u8| {})(&&1);
34+
```
35+
36+
## The `type_length_limit` attribute
37+
38+
The *`type_length_limit` attribute* limits the maximum number of type
39+
substitutions made when constructing a concrete type during monomorphization.
40+
It is applied at the [crate] level, and uses the [_MetaNameValueStr_] syntax
41+
to set the limit based on the number of type substitutions.
42+
43+
> Note: The default in `rustc` is 1048576.
44+
45+
```rust,compile_fail
46+
#![type_length_limit = "8"]
47+
48+
fn f<T>(x: T) {}
49+
50+
// This fails to compile because monomorphizing to
51+
// `f::<(i32, i32, i32, i32, i32, i32, i32, i32, i32)>>` requires more
52+
// than 8 type elements.
53+
f(((1, 2, 3, 4, 5, 6, 7, 8, 9));
54+
```
1255

13-
[attributes]: attributes.html
1456
[_MetaNameValueStr_]: attributes.html#meta-item-attribute-syntax
57+
[attributes]: attributes.html
58+
[crate]: crates-and-source-files.html

0 commit comments

Comments
 (0)