Skip to content

Commit 2955f44

Browse files
committed
Rollup merge of rust-lang#25363 - nham:E0106_E0107_fix, r=alexcrichton
cc rust-lang#25328
2 parents 729c518 + 5ac262f commit 2955f44

File tree

1 file changed

+167
-8
lines changed

1 file changed

+167
-8
lines changed

src/librustc_typeck/diagnostics.rs

Lines changed: 167 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,51 @@ methods that do not have default implementations), as well as any required
1919
trait items like associated types or constants.
2020
"##,
2121

22+
E0049: r##"
23+
This error indicates that an attempted implementation of a trait method
24+
has the wrong number of type parameters.
25+
26+
For example, the trait below has a method `foo` with a type parameter `T`,
27+
but the implementation of `foo` for the type `Bar` is missing this parameter:
28+
29+
```
30+
trait Foo {
31+
fn foo<T: Default>(x: T) -> Self;
32+
}
33+
34+
struct Bar;
35+
36+
// error: method `foo` has 0 type parameters but its trait declaration has 1
37+
// type parameter
38+
impl Foo for Bar {
39+
fn foo(x: bool) -> Self { Bar }
40+
}
41+
```
42+
"##,
43+
44+
E0050: r##"
45+
This error indicates that an attempted implementation of a trait method
46+
has the wrong number of function parameters.
47+
48+
For example, the trait below has a method `foo` with two function parameters
49+
(`&self` and `u8`), but the implementation of `foo` for the type `Bar` omits
50+
the `u8` parameter:
51+
52+
```
53+
trait Foo {
54+
fn foo(&self, x: u8) -> bool;
55+
}
56+
57+
struct Bar;
58+
59+
// error: method `foo` has 1 parameter but the declaration in trait `Foo::foo`
60+
// has 2
61+
impl Foo for Bar {
62+
fn foo(&self) -> bool { true }
63+
}
64+
```
65+
"##,
66+
2267
E0054: r##"
2368
It is not allowed to cast to a bool. If you are trying to cast a numeric type
2469
to a bool, you can compare it with zero instead:
@@ -138,6 +183,88 @@ enum Empty {}
138183
```
139184
"##,
140185

186+
E0106: r##"
187+
This error indicates that a lifetime is missing from a type. If it is an error
188+
inside a function signature, the problem may be with failing to adhere to the
189+
lifetime elision rules (see below).
190+
191+
Here are some simple examples of where you'll run into this error:
192+
193+
```
194+
struct Foo { x: &bool } // error
195+
struct Foo<'a> { x: &'a bool } // correct
196+
197+
enum Bar { A(u8), B(&bool), } // error
198+
enum Bar<'a> { A(u8), B(&'a bool), } // correct
199+
200+
type MyStr = &str; // error
201+
type MyStr<'a> = &'a str; //correct
202+
203+
```
204+
205+
Lifetime elision is a special, limited kind of inference for lifetimes in
206+
function signatures which allows you to leave out lifetimes in certain cases.
207+
For more background on lifetime elision see [the book][book-le].
208+
209+
The lifetime elision rules require that any function signature with an elided
210+
output lifetime must either have
211+
212+
- exactly one input lifetime
213+
- or, multiple input lifetimes, but the function must also be a method with a
214+
`&self` or `&mut self` receiver
215+
216+
In the first case, the output lifetime is inferred to be the same as the unique
217+
input lifetime. In the second case, the lifetime is instead inferred to be the
218+
same as the lifetime on `&self` or `&mut self`.
219+
220+
Here are some examples of elision errors:
221+
222+
```
223+
// error, no input lifetimes
224+
fn foo() -> &str { ... }
225+
226+
// error, `x` and `y` have distinct lifetimes inferred
227+
fn bar(x: &str, y: &str) -> &str { ... }
228+
229+
// error, `y`'s lifetime is inferred to be distinct from `x`'s
230+
fn baz<'a>(x: &'a str, y: &str) -> &str { ... }
231+
```
232+
233+
[book-le]: http://doc.rust-lang.org/nightly/book/lifetimes.html#lifetime-elision
234+
"##,
235+
236+
E0107: r##"
237+
This error means that an incorrect number of lifetime parameters were provided
238+
for a type (like a struct or enum) or trait.
239+
240+
Some basic examples include:
241+
242+
```
243+
struct Foo<'a>(&'a str);
244+
enum Bar { A, B, C }
245+
246+
struct Baz<'a> {
247+
foo: Foo, // error: expected 1, found 0
248+
bar: Bar<'a>, // error: expected 0, found 1
249+
}
250+
```
251+
252+
Here's an example that is currently an error, but may work in a future version
253+
of Rust:
254+
255+
```
256+
struct Foo<'a>(&'a str);
257+
258+
trait Quux { }
259+
impl Quux for Foo { } // error: expected 1, found 0
260+
```
261+
262+
Lifetime elision in implementation headers was part of the lifetime elision
263+
RFC. It is, however, [currently unimplemented][iss15872].
264+
265+
[iss15872]: https://github.com/rust-lang/rust/issues/15872
266+
"##,
267+
141268
E0131: r##"
142269
It is not possible to define `main` with type parameters, or even with function
143270
parameters. When `main` is present, it must take no arguments and return `()`.
@@ -152,6 +279,20 @@ fn(isize, *const *const u8) -> isize
152279
```
153280
"##,
154281

282+
E0166: r##"
283+
This error means that the compiler found a return expression in a function
284+
marked as diverging. A function diverges if it has `!` in the place of the
285+
return type in its signature. For example:
286+
287+
```
288+
fn foo() -> ! { return; } // error
289+
```
290+
291+
For a function that diverges, every control path in the function must never
292+
return, for example with a `loop` that never breaks or a call to another
293+
diverging function (such as `panic!()`).
294+
"##,
295+
155296
E0184: r##"
156297
Explicitly implementing both Drop and Copy for a type is currently disallowed.
157298
This feature can make some sense in theory, but the current implementation is
@@ -161,6 +302,24 @@ it has been disabled for now.
161302
[iss20126]: https://github.com/rust-lang/rust/issues/20126
162303
"##,
163304

305+
E0201: r##"
306+
It is an error to define a method--a trait method or an inherent method--more
307+
than once.
308+
309+
For example,
310+
311+
```
312+
struct Foo(u8);
313+
314+
impl Foo {
315+
fn bar() {}
316+
317+
// error: duplicate method
318+
fn bar(&self) -> bool { self.0 > 5 }
319+
}
320+
```
321+
"##,
322+
164323
E0204: r##"
165324
An attempt to implement the `Copy` trait for a struct failed because one of the
166325
fields does not implement `Copy`. To fix this, you must implement `Copy` for the
@@ -292,6 +451,13 @@ const B: [u32; foo()] = [];
292451
use std::{f64, u8};
293452
const C: [u32; u8::MAX + f64::EPSILON] = [];
294453
```
454+
"##,
455+
456+
E0322: r##"
457+
The `Sized` trait is a special trait built-in to the compiler for types with a
458+
constant size known at compile-time. This trait is automatically implemented
459+
for types as needed by the compiler, and it is currently disallowed to
460+
explicitly implement it for a type.
295461
"##
296462

297463
}
@@ -312,8 +478,6 @@ register_diagnostics! {
312478
E0040, // explicit use of destructor method
313479
E0044, // foreign items may not have type parameters
314480
E0045, // variadic function must have C calling convention
315-
E0049,
316-
E0050,
317481
E0053,
318482
E0055, // method has an incompatible type for trait
319483
E0057, // method has an incompatible type for trait
@@ -345,8 +509,6 @@ register_diagnostics! {
345509
E0102,
346510
E0103,
347511
E0104,
348-
E0106,
349-
E0107,
350512
E0116,
351513
E0117,
352514
E0118,
@@ -364,7 +526,6 @@ register_diagnostics! {
364526
E0159,
365527
E0163,
366528
E0164,
367-
E0166,
368529
E0167,
369530
E0168,
370531
E0172,
@@ -390,7 +551,6 @@ register_diagnostics! {
390551
E0198, // negative implementations are not unsafe
391552
E0199, // implementing trait is not unsafe
392553
E0200, // trait requires an `unsafe impl` declaration
393-
E0201, // duplicate method in trait impl
394554
E0202, // associated items are not allowed in inherent impls
395555
E0203, // type parameter has more than one relaxed default bound,
396556
// and only one is supported
@@ -421,7 +581,7 @@ register_diagnostics! {
421581
E0231, // only named substitution parameters are allowed
422582
E0232, // this attribute must have a value
423583
E0233,
424-
E0234, // `for` loop expression has type which does not implement the `Iterator` trait
584+
E0234,
425585
E0235, // structure constructor specifies a structure of type but
426586
E0236, // no lang item for range syntax
427587
E0237, // no lang item for range syntax
@@ -438,7 +598,6 @@ register_diagnostics! {
438598
E0319, // trait impls for defaulted traits allowed just for structs/enums
439599
E0320, // recursive overflow during dropck
440600
E0321, // extended coherence rules for defaulted traits violated
441-
E0322, // cannot implement Sized explicitly
442601
E0323, // implemented an associated const when another trait item expected
443602
E0324, // implemented a method when another trait item expected
444603
E0325, // implemented an associated type when another trait item expected

0 commit comments

Comments
 (0)