Skip to content

Commit fa8636d

Browse files
author
Nick Hamann
committed
Add error explanations for E0072, E0073, E0121, E0178, E0371, E0372.
1 parent af52207 commit fa8636d

File tree

2 files changed

+123
-8
lines changed

2 files changed

+123
-8
lines changed

src/librustc_typeck/diagnostics.rs

Lines changed: 121 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,63 @@ LinkedList::new() += 1;
108108
```
109109
"##,
110110

111+
E0072: r##"
112+
When defining a recursive struct or enum, any use of the type being defined
113+
from inside the definition must occur behind a pointer (like `Box` or `&`).
114+
This is because structs and enums must have a well-defined size, and without
115+
the pointer the size of the type would need to be unbounded.
116+
117+
Consider the following erroneous definition of a type for a list of bytes:
118+
119+
```
120+
// error, illegal recursive struct type
121+
struct ListNode {
122+
head: u8,
123+
tail: Option<ListNode>,
124+
}
125+
```
126+
127+
This type cannot have a well-defined size, because it needs to be arbitrarily
128+
large (since we would be able to nest `ListNode`s to any depth). Specifically,
129+
130+
```
131+
size of ListNode = 1 byte for head
132+
+ 1 byte for the discriminant of the Option
133+
+ size of ListNode
134+
```
135+
136+
One way to fix this is by wrapping `ListNode` in a `Box`, like so:
137+
138+
```
139+
struct ListNode {
140+
head: u8,
141+
tail: Option<Box<ListNode>>,
142+
}
143+
```
144+
145+
This works because `Box` is a pointer, so its size is well-known.
146+
"##,
147+
148+
E0073: r##"
149+
You cannot define a struct (or enum) `Foo` that requires an instance of `Foo`
150+
in order to make a new `Foo` value. This is because there would be no way a
151+
first instance of `Foo` could be made to initialize another instance!
152+
153+
Here's an example of a struct that has this problem:
154+
155+
```
156+
struct Foo { x: Box<Foo> } // error
157+
```
158+
159+
One fix is to use `Option`, like so:
160+
161+
```
162+
struct Foo { x: Option<Box<Foo>> }
163+
```
164+
165+
Now it's possible to create at least one instance of `Foo`: `Foo { x: None }`.
166+
"##,
167+
111168
E0081: r##"
112169
Enum discriminants are used to differentiate enum variants stored in memory.
113170
This error indicates that the same value was used for two or more variants,
@@ -265,6 +322,19 @@ RFC. It is, however, [currently unimplemented][iss15872].
265322
[iss15872]: https://github.com/rust-lang/rust/issues/15872
266323
"##,
267324

325+
E0121: r##"
326+
In order to be consistent with Rust's lack of global type inference, type
327+
placeholders are disallowed by design in item signatures.
328+
329+
Examples of this error include:
330+
331+
```
332+
fn foo() -> _ { 5 } // error, explicitly write out the return type instead
333+
334+
static BAR: _ = "test"; // error, explicitly write out the type instead
335+
```
336+
"##,
337+
268338
E0131: r##"
269339
It is not possible to define `main` with type parameters, or even with function
270340
parameters. When `main` is present, it must take no arguments and return `()`.
@@ -293,6 +363,28 @@ return, for example with a `loop` that never breaks or a call to another
293363
diverging function (such as `panic!()`).
294364
"##,
295365

366+
E0178: r##"
367+
In types, the `+` type operator has low precedence, so it is often necessary
368+
to use parentheses.
369+
370+
For example:
371+
372+
```
373+
trait Foo {}
374+
375+
struct Bar<'a> {
376+
w: &'a Foo + Copy, // error, use &'a (Foo + Copy)
377+
x: &'a Foo + 'a, // error, use &'a (Foo + 'a)
378+
y: &'a mut Foo + 'a, // error, use &'a mut (Foo + 'a)
379+
z: fn() -> Foo + 'a, // error, use fn() -> (Foo + 'a)
380+
}
381+
```
382+
383+
More details can be found in [RFC 438].
384+
385+
[RFC 438]: https://github.com/rust-lang/rfcs/pull/438
386+
"##,
387+
296388
E0184: r##"
297389
Explicitly implementing both Drop and Copy for a type is currently disallowed.
298390
This feature can make some sense in theory, but the current implementation is
@@ -458,6 +550,35 @@ The `Sized` trait is a special trait built-in to the compiler for types with a
458550
constant size known at compile-time. This trait is automatically implemented
459551
for types as needed by the compiler, and it is currently disallowed to
460552
explicitly implement it for a type.
553+
"##,
554+
555+
E0371: r##"
556+
When `Trait2` is a subtrait of `Trait1` (for example, when `Trait2` has a
557+
definition like `trait Trait2: Trait1 { ... }`), it is not allowed to implement
558+
`Trait1` for `Trait2`. This is because `Trait2` already implements `Trait1` by
559+
definition, so it is not useful to do this.
560+
561+
Example:
562+
563+
```
564+
trait Foo { fn foo(&self) { } }
565+
trait Bar: Foo { }
566+
trait Baz: Bar { }
567+
568+
impl Bar for Baz { } // error, `Baz` implements `Bar` by definition
569+
impl Foo for Baz { } // error, `Baz` implements `Bar` which implements `Foo`
570+
impl Baz for Baz { } // error, `Baz` (trivially) implements `Baz`
571+
impl Baz for Bar { } // Note: This is OK
572+
```
573+
"##,
574+
575+
E0372: r##"
576+
Trying to implement a trait for a trait object (as in `impl Trait1 for
577+
Trait2 { ... }`) does not work if the trait is not object-safe. Please see the
578+
[RFC 255] for more details on object safety rules.
579+
580+
[RFC 255]:https://github.com/rust-lang/rfcs/blob/master/text/0255-object-\
581+
safety.md
461582
"##
462583

463584
}
@@ -489,8 +610,6 @@ register_diagnostics! {
489610
E0069,
490611
E0070,
491612
E0071,
492-
E0072,
493-
E0073,
494613
E0074,
495614
E0075,
496615
E0076,
@@ -514,7 +633,6 @@ register_diagnostics! {
514633
E0118,
515634
E0119,
516635
E0120,
517-
E0121,
518636
E0122,
519637
E0123,
520638
E0124,
@@ -531,7 +649,6 @@ register_diagnostics! {
531649
E0172,
532650
E0173, // manual implementations of unboxed closure traits are experimental
533651
E0174, // explicit use of unboxed closure methods are experimental
534-
E0178,
535652
E0182,
536653
E0183,
537654
E0185,
@@ -608,8 +725,6 @@ register_diagnostics! {
608725
E0367, // dropck forbid specialization to predicate not in struct/enum
609726
E0368, // binary operation `<op>=` cannot be applied to types
610727
E0369, // binary operation `<op>` cannot be applied to types
611-
E0371, // impl Trait for Trait is illegal
612-
E0372, // impl Trait for Trait where Trait is not object safe
613728
E0374, // the trait `CoerceUnsized` may only be implemented for a coercion
614729
// between structures with one field being coerced, none found
615730
E0375, // the trait `CoerceUnsized` may only be implemented for a coercion

src/test/compile-fail/coherence-impl-trait-for-trait.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ trait Foo { fn dummy(&self) { } }
1515
trait Bar: Foo { }
1616
trait Baz: Bar { }
1717

18-
// Subtraits of Baz are not legal:
18+
// Supertraits of Baz are not legal:
1919
impl Foo for Baz { } //~ ERROR E0371
2020
impl Bar for Baz { } //~ ERROR E0371
2121
impl Baz for Baz { } //~ ERROR E0371
2222

2323
// But other random traits are:
2424
trait Other { }
25-
impl Other for Baz { } // OK, Bar not a subtrait of Baz
25+
impl Other for Baz { } // OK, Other not a supertrait of Baz
2626

2727
// If the trait is not object-safe, we give a more tailored message
2828
// because we're such schnuckels:

0 commit comments

Comments
 (0)