Skip to content
Permalink
Browse files

Clean up E0072 long explanation

  • Loading branch information
GuillaumeGomez committed Nov 26, 2019
1 parent 4eee955 commit 77ecb6d44a48f079337c85962bd9f39e1c96594c
Showing with 12 additions and 9 deletions.
  1. +12 −9 src/librustc_error_codes/error_codes/E0072.md
@@ -1,20 +1,23 @@
When defining a recursive struct or enum, any use of the type being defined
from inside the definition must occur behind a pointer (like `Box` or `&`).
This is because structs and enums must have a well-defined size, and without
the pointer, the size of the type would need to be unbounded.
A recursive type has infinite size because it doesn't have an indirection.

Consider the following erroneous definition of a type for a list of bytes:
Erroneous code example:

```compile_fail,E0072
// error, invalid recursive struct type
struct ListNode {
head: u8,
tail: Option<ListNode>,
tail: Option<ListNode>, // error: no indirection here so impossible to
// compute the type's size
}
```

This type cannot have a well-defined size, because it needs to be arbitrarily
large (since we would be able to nest `ListNode`s to any depth). Specifically,
When defining a recursive struct or enum, any use of the type being defined
from inside the definition must occur behind a pointer (like `Box`, `&` or
`Rc`). This is because structs and enums must have a well-defined size, and
without the pointer, the size of the type would need to be unbounded.

In the example, the type cannot have a well-defined size, because it needs to be
arbitrarily large (since we would be able to nest `ListNode`s to any depth).
Specifically,

```plain
size of `ListNode` = 1 byte for `head`

0 comments on commit 77ecb6d

Please sign in to comment.
You can’t perform that action at this time.