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.
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.
11 #![allow(non_snake_case)]
13 register_long_diagnostics! {
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.
27 Here the `Apple` variant has two fields, and should be matched against like so:
36 Matching with the wrong number of fields has no sensible interpretation:
46 Check how many fields the enum was declared with and ensure that your pattern
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:
55 // This enum has two variants.
57 // This variant has no fields.
59 // This variant has one field.
63 // Assuming x is a Number we can pattern match on its contents.
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
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
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
95 let thing = Thing { x: 1, y: 2 };
97 Thing { x: xfield, y: yfield } => ...
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.
107 Thing { x, z } => ...
112 Thing { x, y: z } => ...
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.
130 let d = Dog { name: "Rusty".to_string(), age: 8 };
132 // This is incorrect.
134 Dog { age: x } => ...
137 // This is correct (explicit).
139 Dog { name: n, age: x } => ...
142 // This is also correct (ignore unused fields).
144 Dog { age: x, .. } => ...
150 This error indicates that a pointer to a trait type cannot be implicitly
151 dereferenced by a pattern. Every trait defines a type, but because the
152 size of trait implementors isn't fixed, this type has no compile-time size.
153 Therefore, all accesses to trait types must be through pointers. If you
154 encounter this error you should try to avoid dereferencing the pointer.
157 let trait_obj: &SomeTrait = ...;
159 // This tries to implicitly dereference to create an unsized local variable.
160 let &invalid = trait_obj;
162 // You can call methods without binding to the value being pointed at.
163 trait_obj.method_one();
164 trait_obj.method_two();
167 You can read more about trait objects in the Trait Object section of the
170 http://doc.rust-lang.org/reference.html#trait-objects
174 When trying to make some type implement a trait `Foo`, you must, at minimum,
175 provide implementations for all of `Foo`'s required methods (meaning the
176 methods that do not have default implementations), as well as any required
177 trait items like associated types or constants.
181 This error indicates that an attempted implementation of a trait method
182 has the wrong number of type parameters.
184 For example, the trait below has a method `foo` with a type parameter `T`,
185 but the implementation of `foo` for the type `Bar` is missing this parameter:
189 fn foo<T: Default>(x: T) -> Self;
194 // error: method `foo` has 0 type parameters but its trait declaration has 1
197 fn foo(x: bool) -> Self { Bar }
203 This error indicates that an attempted implementation of a trait method
204 has the wrong number of function parameters.
206 For example, the trait below has a method `foo` with two function parameters
207 (`&self` and `u8`), but the implementation of `foo` for the type `Bar` omits
212 fn foo(&self, x: u8) -> bool;
217 // error: method `foo` has 1 parameter but the declaration in trait `Foo::foo`
220 fn foo(&self) -> bool { true }
226 For any given method of a trait, the mutabilities of the parameters must match
227 between the trait definition and the implementation.
229 Here's an example where the mutability of the `self` parameter is wrong:
232 trait Foo { fn foo(&self); }
237 // error, the signature should be `fn foo(&self)` instead
238 fn foo(&mut self) { }
244 Here's another example, this time for a non-`self` parameter:
247 trait Foo { fn foo(x: &mut bool) -> bool; }
252 // error, the type of `x` should be `&mut bool` instead
253 fn foo(x: &bool) -> bool { *x }
263 It is not allowed to cast to a bool. If you are trying to cast a numeric type
264 to a bool, you can compare it with zero instead:
270 let x_is_nonzero = x != 0;
272 // Not allowed, won't compile
273 let x_is_nonzero = x as bool;
278 This error indicates that during an attempt to build a struct or struct-like
279 enum variant, one of the fields was specified more than once. Each field should
280 be specified exactly one time.
284 This error indicates that during an attempt to build a struct or struct-like
285 enum variant, one of the fields was not provided. Each field should be specified
290 Box placement expressions (like C++'s "placement new") do not yet support any
291 place expression except the exchange heap (i.e. `std::boxed::HEAP`).
292 Furthermore, the syntax is changing to use `in` instead of `box`. See [RFC 470]
293 and [RFC 809] for more details.
295 [RFC 470]: https://github.com/rust-lang/rfcs/pull/470
296 [RFC 809]: https://github.com/rust-lang/rfcs/pull/809
300 The left-hand side of an assignment operator must be an lvalue expression. An
301 lvalue expression represents a memory location and includes item paths (ie,
302 namespaced variables), dereferences, indexing expressions, and field references.
305 use std::collections::LinkedList;
308 let mut list = LinkedList::new();
311 // Bad: assignment to non-lvalue expression
312 LinkedList::new() += 1;
317 The compiler found a function whose body contains a `return;` statement but
318 whose return type is not `()`. An example of this is:
327 Since `return;` is just like `return ();`, there is a mismatch between the
328 function's return type and the value being returned.
332 When defining a recursive struct or enum, any use of the type being defined
333 from inside the definition must occur behind a pointer (like `Box` or `&`).
334 This is because structs and enums must have a well-defined size, and without
335 the pointer the size of the type would need to be unbounded.
337 Consider the following erroneous definition of a type for a list of bytes:
340 // error, illegal recursive struct type
343 tail: Option<ListNode>,
347 This type cannot have a well-defined size, because it needs to be arbitrarily
348 large (since we would be able to nest `ListNode`s to any depth). Specifically,
351 size of ListNode = 1 byte for head
352 + 1 byte for the discriminant of the Option
356 One way to fix this is by wrapping `ListNode` in a `Box`, like so:
361 tail: Option<Box<ListNode>>,
365 This works because `Box` is a pointer, so its size is well-known.
369 You cannot define a struct (or enum) `Foo` that requires an instance of `Foo`
370 in order to make a new `Foo` value. This is because there would be no way a
371 first instance of `Foo` could be made to initialize another instance!
373 Here's an example of a struct that has this problem:
376 struct Foo { x: Box<Foo> } // error
379 One fix is to use `Option`, like so:
382 struct Foo { x: Option<Box<Foo>> }
385 Now it's possible to create at least one instance of `Foo`: `Foo { x: None }`.
389 Enum discriminants are used to differentiate enum variants stored in memory.
390 This error indicates that the same value was used for two or more variants,
391 making them impossible to tell apart.
409 Note that variants without a manually specified discriminant are numbered from
410 top to bottom starting from 0, so clashes can occur with seemingly unrelated
420 Here `X` will have already been assigned the discriminant 0 by the time `Y` is
421 encountered, so a conflict occurs.
425 The default type for enum discriminants is `isize`, but it can be adjusted by
426 adding the `repr` attribute to the enum declaration. This error indicates that
427 an integer literal given as a discriminant is not a member of the discriminant
438 Here, 1024 lies outside the valid range for `u8`, so the discriminant for `A` is
439 invalid. You may want to change representation types to fix this, or else change
440 invalid discriminant values so that they fit within the existing type.
442 Note also that without a representation manually defined, the compiler will
443 optimize by using the smallest integer type possible.
447 At present, it's not possible to define a custom representation for an enum with
448 a single variant. As a workaround you can add a `Dummy` variant.
450 See: https://github.com/rust-lang/rust/issues/10292
454 It is impossible to define an integer type to be used to represent zero-variant
455 enum values because there are no zero-variant enum values. There is no way to
456 construct an instance of the following type using only safe code:
464 This error indicates that a lifetime is missing from a type. If it is an error
465 inside a function signature, the problem may be with failing to adhere to the
466 lifetime elision rules (see below).
468 Here are some simple examples of where you'll run into this error:
471 struct Foo { x: &bool } // error
472 struct Foo<'a> { x: &'a bool } // correct
474 enum Bar { A(u8), B(&bool), } // error
475 enum Bar<'a> { A(u8), B(&'a bool), } // correct
477 type MyStr = &str; // error
478 type MyStr<'a> = &'a str; //correct
482 Lifetime elision is a special, limited kind of inference for lifetimes in
483 function signatures which allows you to leave out lifetimes in certain cases.
484 For more background on lifetime elision see [the book][book-le].
486 The lifetime elision rules require that any function signature with an elided
487 output lifetime must either have
489 - exactly one input lifetime
490 - or, multiple input lifetimes, but the function must also be a method with a
491 `&self` or `&mut self` receiver
493 In the first case, the output lifetime is inferred to be the same as the unique
494 input lifetime. In the second case, the lifetime is instead inferred to be the
495 same as the lifetime on `&self` or `&mut self`.
497 Here are some examples of elision errors:
500 // error, no input lifetimes
501 fn foo() -> &str { ... }
503 // error, `x` and `y` have distinct lifetimes inferred
504 fn bar(x: &str, y: &str) -> &str { ... }
506 // error, `y`'s lifetime is inferred to be distinct from `x`'s
507 fn baz<'a>(x: &'a str, y: &str) -> &str { ... }
510 [book-le]: http://doc.rust-lang.org/nightly/book/lifetimes.html#lifetime-elision
514 This error means that an incorrect number of lifetime parameters were provided
515 for a type (like a struct or enum) or trait.
517 Some basic examples include:
520 struct Foo<'a>(&'a str);
524 foo: Foo, // error: expected 1, found 0
525 bar: Bar<'a>, // error: expected 0, found 1
529 Here's an example that is currently an error, but may work in a future version
533 struct Foo<'a>(&'a str);
536 impl Quux for Foo { } // error: expected 1, found 0
539 Lifetime elision in implementation headers was part of the lifetime elision
540 RFC. It is, however, [currently unimplemented][iss15872].
542 [iss15872]: https://github.com/rust-lang/rust/issues/15872
546 In order to be consistent with Rust's lack of global type inference, type
547 placeholders are disallowed by design in item signatures.
549 Examples of this error include:
552 fn foo() -> _ { 5 } // error, explicitly write out the return type instead
554 static BAR: _ = "test"; // error, explicitly write out the type instead
559 It is not possible to define `main` with type parameters, or even with function
560 parameters. When `main` is present, it must take no arguments and return `()`.
564 It is not possible to declare type parameters on a function that has the `start`
565 attribute. Such a function must have the following type signature:
568 fn(isize, *const *const u8) -> isize
573 This error means that the compiler found a return expression in a function
574 marked as diverging. A function diverges if it has `!` in the place of the
575 return type in its signature. For example:
578 fn foo() -> ! { return; } // error
581 For a function that diverges, every control path in the function must never
582 return, for example with a `loop` that never breaks or a call to another
583 diverging function (such as `panic!()`).
587 In types, the `+` type operator has low precedence, so it is often necessary
596 w: &'a Foo + Copy, // error, use &'a (Foo + Copy)
597 x: &'a Foo + 'a, // error, use &'a (Foo + 'a)
598 y: &'a mut Foo + 'a, // error, use &'a mut (Foo + 'a)
599 z: fn() -> Foo + 'a, // error, use fn() -> (Foo + 'a)
603 More details can be found in [RFC 438].
605 [RFC 438]: https://github.com/rust-lang/rfcs/pull/438
609 Explicitly implementing both Drop and Copy for a type is currently disallowed.
610 This feature can make some sense in theory, but the current implementation is
611 incorrect and can lead to memory unsafety (see [issue #20126][iss20126]), so
612 it has been disabled for now.
614 [iss20126]: https://github.com/rust-lang/rust/issues/20126
618 Inherent implementations (one that do not implement a trait but provide
619 methods associated with a type) are always safe because they are not
620 implementing an unsafe trait. Removing the `unsafe` keyword from the inherent
621 implementation will resolve this error.
626 // this will cause this error
628 // converting it to this will fix it
635 A negative implementation is one that excludes a type from implementing a
636 particular trait. Not being able to use a trait is always a safe operation,
637 so negative implementations are always safe and never need to be marked as
643 // unsafe is unnecessary
644 unsafe impl !Clone for Foo { }
646 impl !Clone for Foo { }
652 Safe traits should not have unsafe implementations, therefore marking an
653 implementation for a safe trait unsafe will cause a compiler error. Removing the
654 unsafe marker on the trait noted in the error will resolve this problem.
661 // this won't compile because Bar is safe
662 unsafe impl Bar for Foo { }
670 Unsafe traits must have unsafe implementations. This error occurs when an
671 implementation for an unsafe trait isn't marked as unsafe. This may be resolved
672 by marking the unsafe implementation as unsafe.
679 // this won't compile because Bar is unsafe and impl isn't unsafe
682 unsafe impl Bar for Foo { }
688 It is an error to define a method--a trait method or an inherent method--more
699 // error: duplicate method
700 fn bar(&self) -> bool { self.0 > 5 }
706 An attempt to implement the `Copy` trait for a struct failed because one of the
707 fields does not implement `Copy`. To fix this, you must implement `Copy` for the
708 mentioned field. Note that this may not be possible, as in the example of
715 impl Copy for Foo { }
718 This fails because `Vec<T>` does not implement `Copy` for any `T`.
720 Here's another example that will fail:
729 This fails because `&mut T` is not `Copy`, even when `T` is `Copy` (this
730 differs from the behavior for `&T`, which is always `Copy`).
734 An attempt to implement the `Copy` trait for an enum failed because one of the
735 variants does not implement `Copy`. To fix this, you must implement `Copy` for
736 the mentioned variant. Note that this may not be possible, as in the example of
744 impl Copy for Foo { }
747 This fails because `Vec<T>` does not implement `Copy` for any `T`.
749 Here's another example that will fail:
759 This fails because `&mut T` is not `Copy`, even when `T` is `Copy` (this
760 differs from the behavior for `&T`, which is always `Copy`).
764 You can only implement `Copy` for a struct or enum. Both of the following
765 examples will fail, because neither `i32` (primitive type) nor `&'static Bar`
766 (reference to `Bar`) is a struct or enum:
770 impl Copy for Foo { } // error
772 #[derive(Copy, Clone)]
774 impl Copy for &'static Bar { } // error
779 This error indicates that not enough type parameters were found in a type or
782 For example, the `Foo` struct below is defined to be generic in `T`, but the
783 type parameter is missing in the definition of `Bar`:
786 struct Foo<T> { x: T }
788 struct Bar { x: Foo }
793 This error indicates that too many type parameters were found in a type or
796 For example, the `Foo` struct below has no type parameters, but is supplied
797 with two in the definition of `Bar`:
800 struct Foo { x: bool }
802 struct Bar<S, T> { x: Foo<S, T> }
807 This error indicates a constant expression for the array length was found, but
808 it was not an integer (signed or unsigned) expression.
810 Some examples of code that produces this error are:
813 const A: [u32; "hello"] = []; // error
814 const B: [u32; true] = []; // error
815 const C: [u32; 0.0] = []; // error
819 This means there was an error while evaluating the expression for the length of
820 a fixed-size array type.
822 Some examples of code that produces this error are:
825 // divide by zero in the length expression
826 const A: [u32; 1/0] = [];
828 // Rust currently will not evaluate the function `foo` at compile time
829 fn foo() -> usize { 12 }
830 const B: [u32; foo()] = [];
832 // it is an error to try to add `u8` and `f64`
834 const C: [u32; u8::MAX + f64::EPSILON] = [];
839 The `Sized` trait is a special trait built-in to the compiler for types with a
840 constant size known at compile-time. This trait is automatically implemented
841 for types as needed by the compiler, and it is currently disallowed to
842 explicitly implement it for a type.
846 This error indicates that a binary assignment operator like `+=` or `^=` was
847 applied to the wrong types.
849 A couple examples of this are as follows:
853 x ^= true; // error, `^=` cannot be applied to types `u16` and `bool`
854 x += (); // error, `+=` cannot be applied to types `u16` and `()`
857 Another problem you might be facing is this: suppose you've overloaded the `+`
858 operator for some type `Foo` by implementing the `std::ops::Add` trait for
859 `Foo`, but you find that using `+=` does not work, as in this example:
869 fn add(self, rhs: Foo) -> Foo {
875 let mut x: Foo = Foo(5);
876 x += Foo(7); // error, `+= cannot be applied to types `Foo` and `Foo`
880 This is because the binary assignment operators currently do not work off of
881 traits, so it is not possible to overload them. See [RFC 953] for a proposal
884 [RFC 953]: https://github.com/rust-lang/rfcs/pull/953
888 When `Trait2` is a subtrait of `Trait1` (for example, when `Trait2` has a
889 definition like `trait Trait2: Trait1 { ... }`), it is not allowed to implement
890 `Trait1` for `Trait2`. This is because `Trait2` already implements `Trait1` by
891 definition, so it is not useful to do this.
896 trait Foo { fn foo(&self) { } }
900 impl Bar for Baz { } // error, `Baz` implements `Bar` by definition
901 impl Foo for Baz { } // error, `Baz` implements `Bar` which implements `Foo`
902 impl Baz for Baz { } // error, `Baz` (trivially) implements `Baz`
903 impl Baz for Bar { } // Note: This is OK
908 Trying to implement a trait for a trait object (as in `impl Trait1 for
909 Trait2 { ... }`) does not work if the trait is not object-safe. Please see the
910 [RFC 255] for more details on object safety rules.
912 [RFC 255]:https://github.com/rust-lang/rfcs/blob/master/text/0255-object-\
918 register_diagnostics! {
922 E0034, // multiple applicable methods in scope
923 E0035, // does not take type parameters
924 E0036, // incorrect number of type parameters given for this method
925 E0040, // explicit use of destructor method
926 E0044, // foreign items may not have type parameters
927 E0045, // variadic function must have C calling convention
928 E0055, // method has an incompatible type for trait
929 E0057, // method has an incompatible type for trait
973 E0173, // manual implementations of unboxed closure traits are experimental
974 E0174, // explicit use of unboxed closure methods are experimental
979 E0187, // can't infer the kind of the closure
980 E0188, // types differ in mutability
981 E0189, // can only cast a boxed pointer to a boxed object
982 E0190, // can only cast a &-pointer to an &-object
983 E0191, // value of the associated type must be specified
984 E0192, // negative imples are allowed just for `Send` and `Sync`
985 E0193, // cannot bound type where clause bounds may only be attached to types
986 // involving type parameters
988 E0195, // lifetime parameters or bounds on method do not match the trait declaration
989 E0196, // cannot determine a type for this closure
990 E0202, // associated items are not allowed in inherent impls
991 E0203, // type parameter has more than one relaxed default bound,
992 // and only one is supported
993 E0207, // type parameter is not constrained by the impl trait, self type, or predicate
995 E0209, // builtin traits can only be implemented on structs or enums
996 E0210, // type parameter is not constrained by any local type
998 E0212, // cannot extract an associated type from a higher-ranked trait bound
999 E0213, // associated types are not accepted in this context
1000 E0214, // parenthesized parameters may only be used with a trait
1001 E0215, // angle-bracket notation is not stable with `Fn`
1002 E0216, // parenthetical notation is only stable with `Fn`
1003 E0217, // ambiguous associated type, defined in multiple supertraits
1004 E0218, // no associated type defined
1005 E0219, // associated type defined in higher-ranked supertrait
1006 E0220, // associated type not found for type parameter
1007 E0221, // ambiguous associated type in bounds
1008 E0222, // variadic function must have C calling convention
1009 E0223, // ambiguous associated type
1010 E0224, // at least one non-builtin train is required for an object type
1011 E0225, // only the builtin traits can be used as closure or object bounds
1012 E0226, // only a single explicit lifetime bound is permitted
1013 E0227, // ambiguous lifetime bound, explicit lifetime bound required
1014 E0228, // explicit lifetime bound required
1015 E0229, // associated type bindings are not allowed here
1016 E0230, // there is no type parameter on trait
1017 E0231, // only named substitution parameters are allowed
1018 E0232, // this attribute must have a value
1021 E0235, // structure constructor specifies a structure of type but
1022 E0236, // no lang item for range syntax
1023 E0237, // no lang item for range syntax
1024 E0238, // parenthesized parameters may only be used with a trait
1025 E0239, // `next` method of `Iterator` trait has unexpected type
1028 E0242, // internal error looking up a definition
1029 E0245, // not a trait
1030 E0246, // illegal recursive type
1031 E0247, // found module name used as a type
1032 E0248, // found value name used as a type
1033 E0318, // can't create default impls for traits outside their crates
1034 E0319, // trait impls for defaulted traits allowed just for structs/enums
1035 E0320, // recursive overflow during dropck
1036 E0321, // extended coherence rules for defaulted traits violated
1037 E0323, // implemented an associated const when another trait item expected
1038 E0324, // implemented a method when another trait item expected
1039 E0325, // implemented an associated type when another trait item expected
1040 E0326, // associated const implemented with different type from trait
1041 E0327, // referred to method instead of constant in match pattern
1042 E0328, // cannot implement Unsize explicitly
1043 E0366, // dropck forbid specialization to concrete type or region
1044 E0367, // dropck forbid specialization to predicate not in struct/enum
1045 E0369, // binary operation `<op>` cannot be applied to types
1046 E0374, // the trait `CoerceUnsized` may only be implemented for a coercion
1047 // between structures with one field being coerced, none found
1048 E0375, // the trait `CoerceUnsized` may only be implemented for a coercion
1049 // between structures with one field being coerced, but multiple
1050 // fields need coercions
1051 E0376, // the trait `CoerceUnsized` may only be implemented for a coercion
1052 // between structures
1053 E0377 // the trait `CoerceUnsized` may only be implemented for a coercion
1054 // between structures with the same definition