Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document type_length_limit. #546

Merged
merged 3 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions src/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ The following is an index of all built-in attributes.
- Limits
- [`recursion_limit`] — Sets the maximum recursion limit for certain
compile-time operations.
- [`type_length_limit`] — Sets the maximum size of a polymorphic type.
- Runtime
- [`panic_handler`] — Sets the function to handle panics.
- [`global_allocator`] — Sets the global memory allocator.
Expand Down Expand Up @@ -283,6 +284,7 @@ The following is an index of all built-in attributes.
[`repr`]: type-layout.html#representations
[`should_panic`]: attributes/testing.html#the-should_panic-attribute
[`test`]: attributes/testing.html#the-test-attribute
[`type_length_limit`]: attributes/limits.html#the-type_length_limit-attribute
[`used`]: abi.html#the-used-attribute
[`warn`]: attributes/diagnostics.html#lint-check-attributes
[`windows_subsystem`]: runtime.html#the-windows_subsystem-attribute
Expand Down
38 changes: 36 additions & 2 deletions src/attributes/limits.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,42 @@ The following [attributes] affect compile-time limits.
The *`recursion_limit` attribute* may be applied at the crate level to set the
maximum depth for potentially infinitely-recursive compile-time operations
like auto-dereference or macro expansion. It uses the [_MetaNameValueStr_]
Centril marked this conversation as resolved.
Show resolved Hide resolved
Centril marked this conversation as resolved.
Show resolved Hide resolved
syntax to specify the recursion depth. The default is
`#![recursion_limit="64"]`.
syntax to specify the recursion depth. The default is 64.
Centril marked this conversation as resolved.
Show resolved Hide resolved

```rust,ignore
#![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!{}
```

## The `type_length_limit` attribute

The *`type_length_limit` attribute* limits the maximum size of a type
constructed during monomorphization. It is applied at the crate level, and
Centril marked this conversation as resolved.
Show resolved Hide resolved
uses the [_MetaNameValueStr_] syntax to set the limit based on the number of
type substitutions within the type. The default value is 1048576.
Centril marked this conversation as resolved.
Show resolved Hide resolved

```rust,ignore
#![type_length_limit = "8"]

type A = (B, B, B);
type B = (C, C, C);
struct C;
Centril marked this conversation as resolved.
Show resolved Hide resolved

// This fails to compile because monomorphizing to
// `drop::<Option<((C, C, C), (C, C, C), (C, C, C))>>` requires more than 8
// type elements.
drop::<Option<A>>(None);
Centril marked this conversation as resolved.
Show resolved Hide resolved
```

[attributes]: attributes.html
[_MetaNameValueStr_]: attributes.html#meta-item-attribute-syntax