Skip to content

Commit 77ecb6d

Browse files
Clean up E0072 long explanation
1 parent 4eee955 commit 77ecb6d

File tree

1 file changed

+12
-9
lines changed
  • src/librustc_error_codes/error_codes

1 file changed

+12
-9
lines changed

src/librustc_error_codes/error_codes/E0072.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
When defining a recursive struct or enum, any use of the type being defined
2-
from inside the definition must occur behind a pointer (like `Box` or `&`).
3-
This is because structs and enums must have a well-defined size, and without
4-
the pointer, the size of the type would need to be unbounded.
1+
A recursive type has infinite size because it doesn't have an indirection.
52

6-
Consider the following erroneous definition of a type for a list of bytes:
3+
Erroneous code example:
74

85
```compile_fail,E0072
9-
// error, invalid recursive struct type
106
struct ListNode {
117
head: u8,
12-
tail: Option<ListNode>,
8+
tail: Option<ListNode>, // error: no indirection here so impossible to
9+
// compute the type's size
1310
}
1411
```
1512

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

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

0 commit comments

Comments
 (0)