]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/diagnostics.rs
d89174295a8927db8c9f8db4f416a59863b9145d
[rust.git] / src / librustc_typeck / diagnostics.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![allow(non_snake_case)]
12
13 register_long_diagnostics! {
14
15 E0023: r##"
16 A pattern used to match against an enum variant must provide a sub-pattern for
17 each field of the enum variant. This error indicates that a pattern attempted to
18 extract an incorrect number of fields from a variant.
19
20 ```
21 enum Fruit {
22     Apple(String, String)
23     Pear(u32)
24 }
25 ```
26
27 Here the `Apple` variant has two fields, and should be matched against like so:
28
29 ```
30 // Correct.
31 match x {
32     Apple(a, b) => ...
33 }
34 ```
35
36 Matching with the wrong number of fields has no sensible interpretation:
37
38 ```
39 // Incorrect.
40 match x {
41     Apple(a) => ...,
42     Apple(a, b, c) => ...
43 }
44 ```
45
46 Check how many fields the enum was declared with and ensure that your pattern
47 uses the same number.
48 "##,
49
50 E0024: r##"
51 This error indicates that a pattern attempted to extract the fields of an enum
52 variant with no fields. Here's a tiny example of this error:
53
54 ```
55 // This enum has two variants.
56 enum Number {
57     // This variant has no fields.
58     Zero,
59     // This variant has one field.
60     One(u32)
61 }
62
63 // Assuming x is a Number we can pattern match on its contents.
64 match x {
65     Zero(inside) => ...,
66     One(inside) => ...
67 }
68 ```
69
70 The pattern match `Zero(inside)` is incorrect because the `Zero` variant
71 contains no fields, yet the `inside` name attempts to bind the first field of
72 the enum.
73 "##,
74
75 E0025: r##"
76 Each field of a struct can only be bound once in a pattern. Each occurrence of a
77 field name binds the value of that field, so to fix this error you will have to
78 remove or alter the duplicate uses of the field name. Perhaps you misspelt
79 another field name?
80 "##,
81
82 E0026: r##"
83 This error indicates that a struct pattern attempted to extract a non-existant
84 field from a struct. Struct fields are identified by the name used before the
85 colon `:` so struct patterns should resemble the declaration of the struct type
86 being matched.
87
88 ```
89 // Correct matching.
90 struct Thing {
91     x: u32,
92     y: u32
93 }
94
95 let thing = Thing { x: 1, y: 2 };
96 match thing {
97     Thing { x: xfield, y: yfield } => ...
98 }
99 ```
100
101 If you are using shorthand field patterns but want to refer to the struct field
102 by a different name, you should rename it explicitly.
103
104 ```
105 // Change this:
106 match thing {
107     Thing { x, z } => ...
108 }
109
110 // To this:
111 match thing {
112     Thing { x, y: z } => ...
113 }
114 ```
115 "##,
116
117 E0027: r##"
118 This error indicates that a pattern for a struct fails to specify a sub-pattern
119 for every one of the struct's fields. Ensure that each field from the struct's
120 definition is mentioned in the pattern, or use `..` to ignore unwanted fields.
121
122 For example:
123
124 ```
125 struct Dog {
126     name: String,
127     age: u32
128 }
129
130 let d = Dog { name: "Rusty".to_string(), age: 8 };
131
132 // This is incorrect.
133 match d {
134     Dog { age: x } => ...
135 }
136
137 // This is correct (explicit).
138 match d {
139     Dog { name: n, age: x } => ...
140 }
141
142 // This is also correct (ignore unused fields).
143 match d {
144     Dog { age: x, .. } => ...
145 }
146 ```
147 "##,
148
149 E0029: r##"
150 In a match expression, only numbers and characters can be matched against a
151 range. This is because the compiler checks that the range is non-empty at
152 compile-time, and is unable to evaluate arbitrary comparison functions. If you
153 want to capture values of an orderable type between two end-points, you can use
154 a guard.
155
156 ```
157 // The ordering relation for strings can't be evaluated at compile time,
158 // so this doesn't work:
159 match string {
160     "hello" ... "world" => ...
161     _ => ...
162 }
163
164 // This is a more general version, using a guard:
165 match string {
166     s if s >= "hello" && s <= "world" => ...
167     _ => ...
168 }
169 ```
170 "##,
171
172 E0030: r##"
173 When matching against a range, the compiler verifies that the range is
174 non-empty.  Range patterns include both end-points, so this is equivalent to
175 requiring the start of the range to be less than or equal to the end of the
176 range.
177
178 For example:
179
180 ```
181 match 5u32 {
182     // This range is ok, albeit pointless.
183     1 ... 1 => ...
184     // This range is empty, and the compiler can tell.
185     1000 ... 5 => ...
186 }
187 ```
188 "##,
189
190 E0033: r##"
191 This error indicates that a pointer to a trait type cannot be implicitly
192 dereferenced by a pattern. Every trait defines a type, but because the
193 size of trait implementors isn't fixed, this type has no compile-time size.
194 Therefore, all accesses to trait types must be through pointers. If you
195 encounter this error you should try to avoid dereferencing the pointer.
196
197 ```
198 let trait_obj: &SomeTrait = ...;
199
200 // This tries to implicitly dereference to create an unsized local variable.
201 let &invalid = trait_obj;
202
203 // You can call methods without binding to the value being pointed at.
204 trait_obj.method_one();
205 trait_obj.method_two();
206 ```
207
208 You can read more about trait objects in the Trait Object section of the
209 Reference:
210
211 http://doc.rust-lang.org/reference.html#trait-objects
212 "##,
213
214 E0040: r##"
215 It is not allowed to manually call destructors in Rust. It is also not
216 necessary to do this since `drop` is called automatically whenever a value goes
217 out of scope.
218
219 Here's an example of this error:
220
221 ```
222 struct Foo {
223     x: i32,
224 }
225
226 impl Drop for Foo {
227     fn drop(&mut self) {
228         println!("kaboom");
229     }
230 }
231
232 fn main() {
233     let mut x = Foo { x: -7 };
234     x.drop(); // error: explicit use of destructor method
235 }
236 ```
237 "##,
238
239 E0046: r##"
240 When trying to make some type implement a trait `Foo`, you must, at minimum,
241 provide implementations for all of `Foo`'s required methods (meaning the
242 methods that do not have default implementations), as well as any required
243 trait items like associated types or constants.
244 "##,
245
246 E0049: r##"
247 This error indicates that an attempted implementation of a trait method
248 has the wrong number of type parameters.
249
250 For example, the trait below has a method `foo` with a type parameter `T`,
251 but the implementation of `foo` for the type `Bar` is missing this parameter:
252
253 ```
254 trait Foo {
255     fn foo<T: Default>(x: T) -> Self;
256 }
257
258 struct Bar;
259
260 // error: method `foo` has 0 type parameters but its trait declaration has 1
261 // type parameter
262 impl Foo for Bar {
263     fn foo(x: bool) -> Self { Bar }
264 }
265 ```
266 "##,
267
268 E0050: r##"
269 This error indicates that an attempted implementation of a trait method
270 has the wrong number of function parameters.
271
272 For example, the trait below has a method `foo` with two function parameters
273 (`&self` and `u8`), but the implementation of `foo` for the type `Bar` omits
274 the `u8` parameter:
275
276 ```
277 trait Foo {
278     fn foo(&self, x: u8) -> bool;
279 }
280
281 struct Bar;
282
283 // error: method `foo` has 1 parameter but the declaration in trait `Foo::foo`
284 // has 2
285 impl Foo for Bar {
286     fn foo(&self) -> bool { true }
287 }
288 ```
289 "##,
290
291 E0053: r##"
292 The parameters of any trait method must match between a trait implementation
293 and the trait definition.
294
295 Here are a couple examples of this error:
296
297 ```
298 trait Foo {
299     fn foo(x: u16);
300     fn bar(&self);
301 }
302
303 struct Bar;
304
305 impl Foo for Bar {
306     // error, expected u16, found i16
307     fn foo(x: i16) { }
308
309     // error, values differ in mutability
310     fn bar(&mut self) { }
311 }
312 ```
313 "##,
314
315 E0054: r##"
316 It is not allowed to cast to a bool. If you are trying to cast a numeric type
317 to a bool, you can compare it with zero instead:
318
319 ```
320 let x = 5;
321
322 // Ok
323 let x_is_nonzero = x != 0;
324
325 // Not allowed, won't compile
326 let x_is_nonzero = x as bool;
327 ```
328 "##,
329
330 E0055: r##"
331 During a method call, a value is automatically dereferenced as many times as
332 needed to make the value's type match the method's receiver. The catch is that
333 the compiler will only attempt to dereference a number of times up to the
334 recursion limit (which can be set via the `recursion_limit` attribute).
335
336 For a somewhat artificial example:
337
338 ```
339 #![recursion_limit="2"]
340
341 struct Foo;
342
343 impl Foo {
344     fn foo(&self) {}
345 }
346
347 fn main() {
348     let foo = Foo;
349     let ref_foo = &&Foo;
350
351     // error, reached the recursion limit while auto-dereferencing &&Foo
352     ref_foo.foo();
353 }
354 ```
355
356 One fix may be to increase the recursion limit. Note that it is possible to
357 create an infinite recursion of dereferencing, in which case the only fix is to
358 somehow break the recursion.
359 "##,
360
361 E0057: r##"
362 When invoking closures or other implementations of the function traits `Fn`,
363 `FnMut` or `FnOnce` using call notation, the number of parameters passed to the
364 function must match its definition.
365
366 An example using a closure:
367
368 ```
369 let f = |x| x * 3;
370 let a = f();        // invalid, too few parameters
371 let b = f(4);       // this works!
372 let c = f(2, 3);    // invalid, too many parameters
373 ```
374
375 A generic function must be treated similarly:
376
377 ```
378 fn foo<F: Fn()>(f: F) {
379     f(); // this is valid, but f(3) would not work
380 }
381 ```
382 "##,
383
384 E0059: r##"
385 The built-in function traits are generic over a tuple of the function arguments.
386 If one uses angle-bracket notation (`Fn<(T,), Output=U>`) instead of parentheses
387 (`Fn(T) -> U`) to denote the function trait, the type parameter should be a
388 tuple. Otherwise function call notation cannot be used and the trait will not be
389 implemented by closures.
390
391 The most likely source of this error is using angle-bracket notation without
392 wrapping the function argument type into a tuple, for example:
393
394 ```
395 fn foo<F: Fn<i32>>(f: F) -> F::Output { f(3) }
396 ```
397
398 It can be fixed by adjusting the trait bound like this:
399
400 ```
401 fn foo<F: Fn<(i32,)>>(f: F) -> F::Output { f(3) }
402 ```
403
404 Note that `(T,)` always denotes the type of a 1-tuple containing an element of
405 type `T`. The comma is necessary for syntactic disambiguation.
406 "##,
407
408 E0060: r##"
409 External C functions are allowed to be variadic. However, a variadic function
410 takes a minimum number of arguments. For example, consider C's variadic `printf`
411 function:
412
413 ```
414 extern crate libc;
415 use libc::{ c_char, c_int };
416
417 extern "C" {
418     fn printf(_: *const c_char, ...) -> c_int;
419 }
420 ```
421
422 Using this declaration, it must be called with at least one argument, so
423 simply calling `printf()` is illegal. But the following uses are allowed:
424
425 ```
426 unsafe {
427     use std::ffi::CString;
428
429     printf(CString::new("test\n").unwrap().as_ptr());
430     printf(CString::new("number = %d\n").unwrap().as_ptr(), 3);
431     printf(CString::new("%d, %d\n").unwrap().as_ptr(), 10, 5);
432 }
433 ```
434 "##,
435
436 E0061: r##"
437 The number of arguments passed to a function must match the number of arguments
438 specified in the function signature.
439
440 For example, a function like
441
442 ```
443 fn f(a: u16, b: &str) {}
444 ```
445
446 must always be called with exactly two arguments, e.g. `f(2, "test")`.
447
448 Note, that Rust does not have a notion of optional function arguments or
449 variadic functions (except for its C-FFI).
450 "##,
451
452 E0062: r##"
453 This error indicates that during an attempt to build a struct or struct-like
454 enum variant, one of the fields was specified more than once. Each field should
455 be specified exactly one time.
456 "##,
457
458 E0063: r##"
459 This error indicates that during an attempt to build a struct or struct-like
460 enum variant, one of the fields was not provided. Each field should be
461 specified exactly once.
462 "##,
463
464 E0066: r##"
465 Box placement expressions (like C++'s "placement new") do not yet support any
466 place expression except the exchange heap (i.e. `std::boxed::HEAP`).
467 Furthermore, the syntax is changing to use `in` instead of `box`. See [RFC 470]
468 and [RFC 809] for more details.
469
470 [RFC 470]: https://github.com/rust-lang/rfcs/pull/470
471 [RFC 809]: https://github.com/rust-lang/rfcs/pull/809
472 "##,
473
474 E0067: r##"
475 The left-hand side of a compound assignment expression must be an lvalue
476 expression. An lvalue expression represents a memory location and includes
477 item paths (ie, namespaced variables), dereferences, indexing expressions,
478 and field references.
479
480 Let's start with some bad examples:
481 ```
482 use std::collections::LinkedList;
483
484 // Bad: assignment to non-lvalue expression
485 LinkedList::new() += 1;
486
487 // ...
488
489 fn some_func(i: &mut i32) {
490     i += 12; // Error : '+=' operation cannot be applied on a reference !
491 }
492
493 And now some good examples:
494 ```
495 let mut i : i32 = 0;
496
497 i += 12; // Good !
498
499 // ...
500
501 fn some_func(i: &mut i32) {
502     *i += 12; // Good !
503 }
504
505 ```
506 "##,
507
508 E0069: r##"
509 The compiler found a function whose body contains a `return;` statement but
510 whose return type is not `()`. An example of this is:
511
512 ```
513 // error
514 fn foo() -> u8 {
515     return;
516 }
517 ```
518
519 Since `return;` is just like `return ();`, there is a mismatch between the
520 function's return type and the value being returned.
521 "##,
522
523 E0070: r##"
524 The left-hand side of an assignment operator must be an lvalue expression. An
525 lvalue expression represents a memory location and can be a variable (with
526 optional namespacing), a dereference, an indexing expression or a field
527 reference.
528
529 More details can be found here:
530 https://doc.rust-lang.org/reference.html#lvalues,-rvalues-and-temporaries
531
532 Now, we can go further. Here are some bad examples:
533 ```
534 struct SomeStruct {
535     x: i32,
536     y: i32
537 }
538 const SOME_CONST : i32 = 12;
539
540 fn some_other_func() {}
541
542 fn some_function() {
543     SOME_CONST = 14; // error : a constant value cannot be changed!
544     1 = 3; // error : 1 isn't a valid lvalue!
545     some_other_func() = 4; // error : we can't assign value to a function!
546     SomeStruct.x = 12; // error : SomeStruct a structure name but it is used
547                        // like a variable!
548 }
549 ```
550
551 And now let's give good examples:
552
553 ```
554 struct SomeStruct {
555     x: i32,
556     y: i32
557 }
558 let mut s = SomeStruct {x: 0, y: 0};
559
560 s.x = 3; // that's good !
561
562 // ...
563
564 fn some_func(x: &mut i32) {
565     *x = 12; // that's good !
566 }
567 ```
568 "##,
569
570 E0072: r##"
571 When defining a recursive struct or enum, any use of the type being defined
572 from inside the definition must occur behind a pointer (like `Box` or `&`).
573 This is because structs and enums must have a well-defined size, and without
574 the pointer the size of the type would need to be unbounded.
575
576 Consider the following erroneous definition of a type for a list of bytes:
577
578 ```
579 // error, illegal recursive struct type
580 struct ListNode {
581     head: u8,
582     tail: Option<ListNode>,
583 }
584 ```
585
586 This type cannot have a well-defined size, because it needs to be arbitrarily
587 large (since we would be able to nest `ListNode`s to any depth). Specifically,
588
589     size of `ListNode` = 1 byte for `head`
590                        + 1 byte for the discriminant of the `Option`
591                        + size of `ListNode`
592
593 One way to fix this is by wrapping `ListNode` in a `Box`, like so:
594
595 ```
596 struct ListNode {
597     head: u8,
598     tail: Option<Box<ListNode>>,
599 }
600 ```
601
602 This works because `Box` is a pointer, so its size is well-known.
603 "##,
604
605 E0073: r##"
606 You cannot define a struct (or enum) `Foo` that requires an instance of `Foo`
607 in order to make a new `Foo` value. This is because there would be no way a
608 first instance of `Foo` could be made to initialize another instance!
609
610 Here's an example of a struct that has this problem:
611
612 ```
613 struct Foo { x: Box<Foo> } // error
614 ```
615
616 One fix is to use `Option`, like so:
617
618 ```
619 struct Foo { x: Option<Box<Foo>> }
620 ```
621
622 Now it's possible to create at least one instance of `Foo`: `Foo { x: None }`.
623 "##,
624
625 E0081: r##"
626 Enum discriminants are used to differentiate enum variants stored in memory.
627 This error indicates that the same value was used for two or more variants,
628 making them impossible to tell apart.
629
630 ```
631 // Good.
632 enum Enum {
633     P,
634     X = 3,
635     Y = 5
636 }
637
638 // Bad.
639 enum Enum {
640     P = 3,
641     X = 3,
642     Y = 5
643 }
644 ```
645
646 Note that variants without a manually specified discriminant are numbered from
647 top to bottom starting from 0, so clashes can occur with seemingly unrelated
648 variants.
649
650 ```
651 enum Bad {
652     X,
653     Y = 0
654 }
655 ```
656
657 Here `X` will have already been assigned the discriminant 0 by the time `Y` is
658 encountered, so a conflict occurs.
659 "##,
660
661 E0082: r##"
662 The default type for enum discriminants is `isize`, but it can be adjusted by
663 adding the `repr` attribute to the enum declaration. This error indicates that
664 an integer literal given as a discriminant is not a member of the discriminant
665 type. For example:
666
667 ```
668 #[repr(u8)]
669 enum Thing {
670     A = 1024,
671     B = 5
672 }
673 ```
674
675 Here, 1024 lies outside the valid range for `u8`, so the discriminant for `A` is
676 invalid. You may want to change representation types to fix this, or else change
677 invalid discriminant values so that they fit within the existing type.
678
679 Note also that without a representation manually defined, the compiler will
680 optimize by using the smallest integer type possible.
681 "##,
682
683 E0083: r##"
684 At present, it's not possible to define a custom representation for an enum with
685 a single variant. As a workaround you can add a `Dummy` variant.
686
687 See: https://github.com/rust-lang/rust/issues/10292
688 "##,
689
690 E0084: r##"
691 It is impossible to define an integer type to be used to represent zero-variant
692 enum values because there are no zero-variant enum values. There is no way to
693 construct an instance of the following type using only safe code:
694
695 ```
696 enum Empty {}
697 ```
698 "##,
699
700 E0087: r##"
701 Too many type parameters were supplied for a function. For example:
702
703 ```
704 fn foo<T>() {}
705
706 fn main() {
707     foo::<f64, bool>(); // error, expected 1 parameter, found 2 parameters
708 }
709 ```
710
711 The number of supplied parameters much exactly match the number of defined type
712 parameters.
713 "##,
714
715 E0089: r##"
716 Not enough type parameters were supplied for a function. For example:
717
718 ```
719 fn foo<T, U>() {}
720
721 fn main() {
722     foo::<f64>(); // error, expected 2 parameters, found 1 parameter
723 }
724 ```
725
726 Note that if a function takes multiple type parameters but you want the compiler
727 to infer some of them, you can use type placeholders:
728
729 ```
730 fn foo<T, U>(x: T) {}
731
732 fn main() {
733     let x: bool = true;
734     foo::<f64>(x);    // error, expected 2 parameters, found 1 parameter
735     foo::<_, f64>(x); // same as `foo::<bool, f64>(x)`
736 }
737 ```
738 "##,
739
740 E0106: r##"
741 This error indicates that a lifetime is missing from a type. If it is an error
742 inside a function signature, the problem may be with failing to adhere to the
743 lifetime elision rules (see below).
744
745 Here are some simple examples of where you'll run into this error:
746
747 ```
748 struct Foo { x: &bool }        // error
749 struct Foo<'a> { x: &'a bool } // correct
750
751 enum Bar { A(u8), B(&bool), }        // error
752 enum Bar<'a> { A(u8), B(&'a bool), } // correct
753
754 type MyStr = &str;        // error
755 type MyStr<'a> = &'a str; //correct
756
757 ```
758
759 Lifetime elision is a special, limited kind of inference for lifetimes in
760 function signatures which allows you to leave out lifetimes in certain cases.
761 For more background on lifetime elision see [the book][book-le].
762
763 The lifetime elision rules require that any function signature with an elided
764 output lifetime must either have
765
766  - exactly one input lifetime
767  - or, multiple input lifetimes, but the function must also be a method with a
768    `&self` or `&mut self` receiver
769
770 In the first case, the output lifetime is inferred to be the same as the unique
771 input lifetime. In the second case, the lifetime is instead inferred to be the
772 same as the lifetime on `&self` or `&mut self`.
773
774 Here are some examples of elision errors:
775
776 ```
777 // error, no input lifetimes
778 fn foo() -> &str { ... }
779
780 // error, `x` and `y` have distinct lifetimes inferred
781 fn bar(x: &str, y: &str) -> &str { ... }
782
783 // error, `y`'s lifetime is inferred to be distinct from `x`'s
784 fn baz<'a>(x: &'a str, y: &str) -> &str { ... }
785 ```
786
787 [book-le]: http://doc.rust-lang.org/nightly/book/lifetimes.html#lifetime-elision
788 "##,
789
790 E0107: r##"
791 This error means that an incorrect number of lifetime parameters were provided
792 for a type (like a struct or enum) or trait.
793
794 Some basic examples include:
795
796 ```
797 struct Foo<'a>(&'a str);
798 enum Bar { A, B, C }
799
800 struct Baz<'a> {
801     foo: Foo,     // error: expected 1, found 0
802     bar: Bar<'a>, // error: expected 0, found 1
803 }
804 ```
805
806 Here's an example that is currently an error, but may work in a future version
807 of Rust:
808
809 ```
810 struct Foo<'a>(&'a str);
811
812 trait Quux { }
813 impl Quux for Foo { } // error: expected 1, found 0
814 ```
815
816 Lifetime elision in implementation headers was part of the lifetime elision
817 RFC. It is, however, [currently unimplemented][iss15872].
818
819 [iss15872]: https://github.com/rust-lang/rust/issues/15872
820 "##,
821
822 E0116: r##"
823 You can only define an inherent implementation for a type in the same crate
824 where the type was defined. For example, an `impl` block as below is not allowed
825 since `Vec` is defined in the standard library:
826
827 ```
828 impl Vec<u8> { ... } // error
829 ```
830
831 To fix this problem, you can do either of these things:
832
833  - define a trait that has the desired associated functions/types/constants and
834    implement the trait for the type in question
835  - define a new type wrapping the type and define an implementation on the new
836    type
837
838 Note that using the `type` keyword does not work here because `type` only
839 introduces a type alias:
840
841 ```
842 type Bytes = Vec<u8>;
843
844 impl Bytes { ... } // error, same as above
845 ```
846 "##,
847
848 E0121: r##"
849 In order to be consistent with Rust's lack of global type inference, type
850 placeholders are disallowed by design in item signatures.
851
852 Examples of this error include:
853
854 ```
855 fn foo() -> _ { 5 } // error, explicitly write out the return type instead
856
857 static BAR: _ = "test"; // error, explicitly write out the type instead
858 ```
859 "##,
860
861 E0131: r##"
862 It is not possible to define `main` with type parameters, or even with function
863 parameters. When `main` is present, it must take no arguments and return `()`.
864 "##,
865
866 E0132: r##"
867 It is not possible to declare type parameters on a function that has the `start`
868 attribute. Such a function must have the following type signature:
869
870 ```
871 fn(isize, *const *const u8) -> isize
872 ```
873 "##,
874
875 E0166: r##"
876 This error means that the compiler found a return expression in a function
877 marked as diverging. A function diverges if it has `!` in the place of the
878 return type in its signature. For example:
879
880 ```
881 fn foo() -> ! { return; } // error
882 ```
883
884 For a function that diverges, every control path in the function must never
885 return, for example with a `loop` that never breaks or a call to another
886 diverging function (such as `panic!()`).
887 "##,
888
889 E0178: r##"
890 In types, the `+` type operator has low precedence, so it is often necessary
891 to use parentheses.
892
893 For example:
894
895 ```
896 trait Foo {}
897
898 struct Bar<'a> {
899     w: &'a Foo + Copy,   // error, use &'a (Foo + Copy)
900     x: &'a Foo + 'a,     // error, use &'a (Foo + 'a)
901     y: &'a mut Foo + 'a, // error, use &'a mut (Foo + 'a)
902     z: fn() -> Foo + 'a, // error, use fn() -> (Foo + 'a)
903 }
904 ```
905
906 More details can be found in [RFC 438].
907
908 [RFC 438]: https://github.com/rust-lang/rfcs/pull/438
909 "##,
910
911 E0184: r##"
912 Explicitly implementing both Drop and Copy for a type is currently disallowed.
913 This feature can make some sense in theory, but the current implementation is
914 incorrect and can lead to memory unsafety (see [issue #20126][iss20126]), so
915 it has been disabled for now.
916
917 [iss20126]: https://github.com/rust-lang/rust/issues/20126
918 "##,
919
920 E0185: r##"
921 An associated function for a trait was defined to be static, but an
922 implementation of the trait declared the same function to be a method (i.e. to
923 take a `self` parameter).
924
925 Here's an example of this error:
926
927 ```
928 trait Foo {
929     fn foo();
930 }
931
932 struct Bar;
933
934 impl Foo for Bar {
935     // error, method `foo` has a `&self` declaration in the impl, but not in
936     // the trait
937     fn foo(&self) {}
938 }
939 "##,
940
941 E0186: r##"
942 An associated function for a trait was defined to be a method (i.e. to take a
943 `self` parameter), but an implementation of the trait declared the same function
944 to be static.
945
946 Here's an example of this error:
947
948 ```
949 trait Foo {
950     fn foo(&self);
951 }
952
953 struct Bar;
954
955 impl Foo for Bar {
956     // error, method `foo` has a `&self` declaration in the trait, but not in
957     // the impl
958     fn foo() {}
959 }
960 "##,
961
962 E0192: r##"
963 Negative impls are only allowed for traits with default impls. For more
964 information see the [opt-in builtin traits RFC](https://github.com/rust-lang/
965 rfcs/blob/master/text/0019-opt-in-builtin-traits.md).
966 "##,
967
968 E0197: r##"
969 Inherent implementations (one that do not implement a trait but provide
970 methods associated with a type) are always safe because they are not
971 implementing an unsafe trait. Removing the `unsafe` keyword from the inherent
972 implementation will resolve this error.
973
974 ```
975 struct Foo;
976
977 // this will cause this error
978 unsafe impl Foo { }
979 // converting it to this will fix it
980 impl Foo { }
981 ```
982
983 "##,
984
985 E0198: r##"
986 A negative implementation is one that excludes a type from implementing a
987 particular trait. Not being able to use a trait is always a safe operation,
988 so negative implementations are always safe and never need to be marked as
989 unsafe.
990
991 ```
992 struct Foo;
993
994 // unsafe is unnecessary
995 unsafe impl !Clone for Foo { }
996 // this will compile
997 impl !Clone for Foo { }
998 ```
999
1000 "##,
1001
1002 E0199: r##"
1003 Safe traits should not have unsafe implementations, therefore marking an
1004 implementation for a safe trait unsafe will cause a compiler error. Removing the
1005 unsafe marker on the trait noted in the error will resolve this problem.
1006
1007 ```
1008 struct Foo;
1009
1010 trait Bar { }
1011
1012 // this won't compile because Bar is safe
1013 unsafe impl Bar for Foo { }
1014 // this will compile
1015 impl Bar for Foo { }
1016 ```
1017
1018 "##,
1019
1020 E0200: r##"
1021 Unsafe traits must have unsafe implementations. This error occurs when an
1022 implementation for an unsafe trait isn't marked as unsafe. This may be resolved
1023 by marking the unsafe implementation as unsafe.
1024
1025 ```
1026 struct Foo;
1027
1028 unsafe trait Bar { }
1029
1030 // this won't compile because Bar is unsafe and impl isn't unsafe
1031 impl Bar for Foo { }
1032 // this will compile
1033 unsafe impl Bar for Foo { }
1034 ```
1035
1036 "##,
1037
1038 E0201: r##"
1039 It is an error to define an associated function more than once.
1040
1041 For example:
1042
1043 ```
1044 struct Foo(u8);
1045
1046 impl Foo {
1047     fn bar(&self) -> bool { self.0 > 5 }
1048
1049     // error: duplicate associated function
1050     fn bar() {}
1051 }
1052
1053 trait Baz {
1054     fn baz(&self) -> bool;
1055 }
1056
1057 impl Baz for Foo {
1058     fn baz(&self) -> bool { true }
1059
1060     // error: duplicate method
1061     fn baz(&self) -> bool { self.0 > 5 }
1062 }
1063 ```
1064 "##,
1065
1066 E0202: r##"
1067 Inherent associated types were part of [RFC 195] but are not yet implemented.
1068 See [the tracking issue][iss8995] for the status of this implementation.
1069
1070 [RFC 195]: https://github.com/rust-lang/rfcs/pull/195
1071 [iss8995]: https://github.com/rust-lang/rust/issues/8995
1072 "##,
1073
1074 E0204: r##"
1075 An attempt to implement the `Copy` trait for a struct failed because one of the
1076 fields does not implement `Copy`. To fix this, you must implement `Copy` for the
1077 mentioned field. Note that this may not be possible, as in the example of
1078
1079 ```
1080 struct Foo {
1081     foo : Vec<u32>,
1082 }
1083
1084 impl Copy for Foo { }
1085 ```
1086
1087 This fails because `Vec<T>` does not implement `Copy` for any `T`.
1088
1089 Here's another example that will fail:
1090
1091 ```
1092 #[derive(Copy)]
1093 struct Foo<'a> {
1094     ty: &'a mut bool,
1095 }
1096 ```
1097
1098 This fails because `&mut T` is not `Copy`, even when `T` is `Copy` (this
1099 differs from the behavior for `&T`, which is always `Copy`).
1100 "##,
1101
1102 E0205: r##"
1103 An attempt to implement the `Copy` trait for an enum failed because one of the
1104 variants does not implement `Copy`. To fix this, you must implement `Copy` for
1105 the mentioned variant. Note that this may not be possible, as in the example of
1106
1107 ```
1108 enum Foo {
1109     Bar(Vec<u32>),
1110     Baz,
1111 }
1112
1113 impl Copy for Foo { }
1114 ```
1115
1116 This fails because `Vec<T>` does not implement `Copy` for any `T`.
1117
1118 Here's another example that will fail:
1119
1120 ```
1121 #[derive(Copy)]
1122 enum Foo<'a> {
1123     Bar(&'a mut bool),
1124     Baz
1125 }
1126 ```
1127
1128 This fails because `&mut T` is not `Copy`, even when `T` is `Copy` (this
1129 differs from the behavior for `&T`, which is always `Copy`).
1130 "##,
1131
1132 E0206: r##"
1133 You can only implement `Copy` for a struct or enum. Both of the following
1134 examples will fail, because neither `i32` (primitive type) nor `&'static Bar`
1135 (reference to `Bar`) is a struct or enum:
1136
1137 ```
1138 type Foo = i32;
1139 impl Copy for Foo { } // error
1140
1141 #[derive(Copy, Clone)]
1142 struct Bar;
1143 impl Copy for &'static Bar { } // error
1144 ```
1145 "##,
1146
1147 E0243: r##"
1148 This error indicates that not enough type parameters were found in a type or
1149 trait.
1150
1151 For example, the `Foo` struct below is defined to be generic in `T`, but the
1152 type parameter is missing in the definition of `Bar`:
1153
1154 ```
1155 struct Foo<T> { x: T }
1156
1157 struct Bar { x: Foo }
1158 ```
1159 "##,
1160
1161 E0244: r##"
1162 This error indicates that too many type parameters were found in a type or
1163 trait.
1164
1165 For example, the `Foo` struct below has no type parameters, but is supplied
1166 with two in the definition of `Bar`:
1167
1168 ```
1169 struct Foo { x: bool }
1170
1171 struct Bar<S, T> { x: Foo<S, T> }
1172 ```
1173 "##,
1174
1175 E0249: r##"
1176 This error indicates a constant expression for the array length was found, but
1177 it was not an integer (signed or unsigned) expression.
1178
1179 Some examples of code that produces this error are:
1180
1181 ```
1182 const A: [u32; "hello"] = []; // error
1183 const B: [u32; true] = []; // error
1184 const C: [u32; 0.0] = []; // error
1185 "##,
1186
1187 E0250: r##"
1188 There was an error while evaluating the expression for the length of a fixed-
1189 size array type.
1190
1191 Some examples of this error are:
1192
1193 ```
1194 // divide by zero in the length expression
1195 const A: [u32; 1/0] = [];
1196
1197 // Rust currently will not evaluate the function `foo` at compile time
1198 fn foo() -> usize { 12 }
1199 const B: [u32; foo()] = [];
1200
1201 // it is an error to try to add `u8` and `f64`
1202 use std::{f64, u8};
1203 const C: [u32; u8::MAX + f64::EPSILON] = [];
1204 ```
1205 "##,
1206
1207 E0318: r##"
1208 Default impls for a trait must be located in the same crate where the trait was
1209 defined. For more information see the [opt-in builtin traits RFC](https://github
1210 .com/rust-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md).
1211 "##,
1212
1213 E0322: r##"
1214 The `Sized` trait is a special trait built-in to the compiler for types with a
1215 constant size known at compile-time. This trait is automatically implemented
1216 for types as needed by the compiler, and it is currently disallowed to
1217 explicitly implement it for a type.
1218 "##,
1219
1220 E0326: r##"
1221 The types of any associated constants in a trait implementation must match the
1222 types in the trait definition. This error indicates that there was a mismatch.
1223
1224 Here's an example of this error:
1225
1226 ```
1227 trait Foo {
1228     const BAR: bool;
1229 }
1230
1231 struct Bar;
1232
1233 impl Foo for Bar {
1234     const BAR: u32 = 5; // error, expected bool, found u32
1235 }
1236 ```
1237 "##,
1238
1239 E0368: r##"
1240 This error indicates that a binary assignment operator like `+=` or `^=` was
1241 applied to the wrong types. For example:
1242
1243 ```
1244 let mut x: u16 = 5;
1245 x ^= true; // error, `^=` cannot be applied to types `u16` and `bool`
1246 x += ();   // error, `+=` cannot be applied to types `u16` and `()`
1247 ```
1248
1249 Another problem you might be facing is this: suppose you've overloaded the `+`
1250 operator for some type `Foo` by implementing the `std::ops::Add` trait for
1251 `Foo`, but you find that using `+=` does not work, as in this example:
1252
1253 ```
1254 use std::ops::Add;
1255
1256 struct Foo(u32);
1257
1258 impl Add for Foo {
1259     type Output = Foo;
1260
1261     fn add(self, rhs: Foo) -> Foo {
1262         Foo(self.0 + rhs.0)
1263     }
1264 }
1265
1266 fn main() {
1267     let mut x: Foo = Foo(5);
1268     x += Foo(7); // error, `+= cannot be applied to types `Foo` and `Foo`
1269 }
1270 ```
1271
1272 This is because the binary assignment operators currently do not work off of
1273 traits, so it is not possible to overload them. See [RFC 953] for a proposal
1274 to change this.
1275
1276 [RFC 953]: https://github.com/rust-lang/rfcs/pull/953
1277 "##,
1278
1279 E0371: r##"
1280 When `Trait2` is a subtrait of `Trait1` (for example, when `Trait2` has a
1281 definition like `trait Trait2: Trait1 { ... }`), it is not allowed to implement
1282 `Trait1` for `Trait2`. This is because `Trait2` already implements `Trait1` by
1283 definition, so it is not useful to do this.
1284
1285 Example:
1286
1287 ```
1288 trait Foo { fn foo(&self) { } }
1289 trait Bar: Foo { }
1290 trait Baz: Bar { }
1291
1292 impl Bar for Baz { } // error, `Baz` implements `Bar` by definition
1293 impl Foo for Baz { } // error, `Baz` implements `Bar` which implements `Foo`
1294 impl Baz for Baz { } // error, `Baz` (trivially) implements `Baz`
1295 impl Baz for Bar { } // Note: This is OK
1296 ```
1297 "##,
1298
1299 E0372: r##"
1300 Trying to implement a trait for a trait object (as in `impl Trait1 for
1301 Trait2 { ... }`) does not work if the trait is not object-safe. Please see the
1302 [RFC 255] for more details on object safety rules.
1303
1304 [RFC 255]: https://github.com/rust-lang/rfcs/pull/255
1305 "##,
1306
1307 E0379: r##"
1308 Trait methods cannot be declared `const` by design. For more information, see
1309 [RFC 911].
1310
1311 [RFC 911]: https://github.com/rust-lang/rfcs/pull/911
1312 "##,
1313
1314 E0380: r##"
1315 Default impls are only allowed for traits with no methods or associated items.
1316 For more information see the [opt-in builtin traits RFC](https://github.com/rust
1317 -lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md).
1318 "##
1319
1320 }
1321
1322 register_diagnostics! {
1323     E0034, // multiple applicable methods in scope
1324     E0035, // does not take type parameters
1325     E0036, // incorrect number of type parameters given for this method
1326     E0044, // foreign items may not have type parameters
1327     E0045, // variadic function must have C calling convention
1328     E0068,
1329     E0071,
1330     E0074,
1331     E0075,
1332     E0076,
1333     E0077,
1334     E0085,
1335     E0086,
1336     E0088,
1337     E0090,
1338     E0091,
1339     E0092,
1340     E0093,
1341     E0094,
1342     E0101,
1343     E0102,
1344     E0103,
1345     E0104,
1346     E0117,
1347     E0118,
1348     E0119,
1349     E0120,
1350     E0122,
1351     E0123,
1352     E0124,
1353     E0127,
1354     E0128,
1355     E0129,
1356     E0130,
1357     E0141,
1358     E0159,
1359     E0163,
1360     E0164,
1361     E0167,
1362     E0168,
1363     E0172,
1364     E0173, // manual implementations of unboxed closure traits are experimental
1365     E0174, // explicit use of unboxed closure methods are experimental
1366     E0182,
1367     E0183,
1368     E0187, // can't infer the kind of the closure
1369     E0188, // can not cast a immutable reference to a mutable pointer
1370     E0189, // deprecated: can only cast a boxed pointer to a boxed object
1371     E0190, // deprecated: can only cast a &-pointer to an &-object
1372     E0191, // value of the associated type must be specified
1373     E0193, // cannot bound type where clause bounds may only be attached to types
1374            // involving type parameters
1375     E0194,
1376     E0195, // lifetime parameters or bounds on method do not match the trait declaration
1377     E0196, // cannot determine a type for this closure
1378     E0203, // type parameter has more than one relaxed default bound,
1379            // and only one is supported
1380     E0207, // type parameter is not constrained by the impl trait, self type, or predicate
1381     E0208,
1382     E0209, // builtin traits can only be implemented on structs or enums
1383     E0210, // type parameter is not constrained by any local type
1384     E0211,
1385     E0212, // cannot extract an associated type from a higher-ranked trait bound
1386     E0213, // associated types are not accepted in this context
1387     E0214, // parenthesized parameters may only be used with a trait
1388     E0215, // angle-bracket notation is not stable with `Fn`
1389     E0216, // parenthetical notation is only stable with `Fn`
1390     E0217, // ambiguous associated type, defined in multiple supertraits
1391     E0218, // no associated type defined
1392     E0219, // associated type defined in higher-ranked supertrait
1393     E0220, // associated type not found for type parameter
1394     E0221, // ambiguous associated type in bounds
1395     E0222, // variadic function must have C calling convention
1396     E0223, // ambiguous associated type
1397     E0224, // at least one non-builtin train is required for an object type
1398     E0225, // only the builtin traits can be used as closure or object bounds
1399     E0226, // only a single explicit lifetime bound is permitted
1400     E0227, // ambiguous lifetime bound, explicit lifetime bound required
1401     E0228, // explicit lifetime bound required
1402     E0229, // associated type bindings are not allowed here
1403     E0230, // there is no type parameter on trait
1404     E0231, // only named substitution parameters are allowed
1405     E0232, // this attribute must have a value
1406     E0233,
1407     E0234,
1408     E0235, // structure constructor specifies a structure of type but
1409     E0236, // no lang item for range syntax
1410     E0237, // no lang item for range syntax
1411     E0238, // parenthesized parameters may only be used with a trait
1412     E0239, // `next` method of `Iterator` trait has unexpected type
1413     E0240,
1414     E0241,
1415     E0242, // internal error looking up a definition
1416     E0245, // not a trait
1417     E0246, // illegal recursive type
1418     E0247, // found module name used as a type
1419     E0248, // found value name used as a type
1420     E0319, // trait impls for defaulted traits allowed just for structs/enums
1421     E0320, // recursive overflow during dropck
1422     E0321, // extended coherence rules for defaulted traits violated
1423     E0323, // implemented an associated const when another trait item expected
1424     E0324, // implemented a method when another trait item expected
1425     E0325, // implemented an associated type when another trait item expected
1426     E0327, // referred to method instead of constant in match pattern
1427     E0328, // cannot implement Unsize explicitly
1428     E0329, // associated const depends on type parameter or Self.
1429     E0366, // dropck forbid specialization to concrete type or region
1430     E0367, // dropck forbid specialization to predicate not in struct/enum
1431     E0369, // binary operation `<op>` cannot be applied to types
1432     E0374, // the trait `CoerceUnsized` may only be implemented for a coercion
1433            // between structures with one field being coerced, none found
1434     E0375, // the trait `CoerceUnsized` may only be implemented for a coercion
1435            // between structures with one field being coerced, but multiple
1436            // fields need coercions
1437     E0376, // the trait `CoerceUnsized` may only be implemented for a coercion
1438            // between structures
1439     E0377, // the trait `CoerceUnsized` may only be implemented for a coercion
1440            // between structures with the same definition
1441     E0390, // only a single inherent implementation marked with
1442            // `#[lang = \"{}\"]` is allowed for the `{}` primitive
1443     E0391, // unsupported cyclic reference between types/traits detected
1444     E0392, // parameter `{}` is never used
1445     E0393  // the type parameter `{}` must be explicitly specified in an object
1446            // type because its default value `{}` references the type `Self`"
1447 }