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.
22 Apple(String, String),
27 Here the `Apple` variant has two fields, and should be matched against like so:
31 Apple(String, String),
35 let x = Fruit::Apple(String::new(), String::new());
39 Fruit::Apple(a, b) => {},
44 Matching with the wrong number of fields has no sensible interpretation:
48 Apple(String, String),
52 let x = Fruit::Apple(String::new(), String::new());
56 Fruit::Apple(a) => {},
57 Fruit::Apple(a, b, c) => {},
61 Check how many fields the enum was declared with and ensure that your pattern
66 Each field of a struct can only be bound once in a pattern. Erroneous code
76 let x = Foo { a:1, b:2 };
78 let Foo { a: x, a: y } = x;
79 // error: field `a` bound multiple times in the pattern
83 Each occurrence of a field name binds the value of that field, so to fix this
84 error you will have to remove or alter the duplicate uses of the field name.
85 Perhaps you misspelled another field name? Example:
94 let x = Foo { a:1, b:2 };
96 let Foo { a: x, b: y } = x; // ok!
102 This error indicates that a struct pattern attempted to extract a non-existent
103 field from a struct. Struct fields are identified by the name used before the
104 colon `:` so struct patterns should resemble the declaration of the struct type
114 let thing = Thing { x: 1, y: 2 };
117 Thing { x: xfield, y: yfield } => {}
121 If you are using shorthand field patterns but want to refer to the struct field
122 by a different name, you should rename it explicitly.
126 ```compile_fail,E0026
132 let thing = Thing { x: 0, y: 0 };
147 let thing = Thing { x: 0, y: 0 };
150 Thing { x, y: z } => {}
156 This error indicates that a pattern for a struct fails to specify a sub-pattern
157 for every one of the struct's fields. Ensure that each field from the struct's
158 definition is mentioned in the pattern, or use `..` to ignore unwanted fields.
162 ```compile_fail,E0027
168 let d = Dog { name: "Rusty".to_string(), age: 8 };
170 // This is incorrect.
176 This is correct (explicit):
184 let d = Dog { name: "Rusty".to_string(), age: 8 };
187 Dog { name: ref n, age: x } => {}
190 // This is also correct (ignore unused fields).
192 Dog { age: x, .. } => {}
198 In a match expression, only numbers and characters can be matched against a
199 range. This is because the compiler checks that the range is non-empty at
200 compile-time, and is unable to evaluate arbitrary comparison functions. If you
201 want to capture values of an orderable type between two end-points, you can use
204 ```compile_fail,E0029
205 let string = "salutations !";
207 // The ordering relation for strings can't be evaluated at compile time,
208 // so this doesn't work:
210 "hello" ... "world" => {}
214 // This is a more general version, using a guard:
216 s if s >= "hello" && s <= "world" => {}
223 This error indicates that a pointer to a trait type cannot be implicitly
224 dereferenced by a pattern. Every trait defines a type, but because the
225 size of trait implementors isn't fixed, this type has no compile-time size.
226 Therefore, all accesses to trait types must be through pointers. If you
227 encounter this error you should try to avoid dereferencing the pointer.
230 let trait_obj: &SomeTrait = ...;
232 // This tries to implicitly dereference to create an unsized local variable.
233 let &invalid = trait_obj;
235 // You can call methods without binding to the value being pointed at.
236 trait_obj.method_one();
237 trait_obj.method_two();
240 You can read more about trait objects in the Trait Object section of the
243 https://doc.rust-lang.org/reference.html#trait-objects
247 The compiler doesn't know what method to call because more than one method
248 has the same prototype. Erroneous code example:
250 ```compile_fail,E0034
261 impl Trait1 for Test { fn foo() {} }
262 impl Trait2 for Test { fn foo() {} }
265 Test::foo() // error, which foo() to call?
269 To avoid this error, you have to keep only one of them and remove the others.
270 So let's take our example and fix it:
279 impl Trait1 for Test { fn foo() {} }
282 Test::foo() // and now that's good!
286 However, a better solution would be using fully explicit naming of type and
300 impl Trait1 for Test { fn foo() {} }
301 impl Trait2 for Test { fn foo() {} }
304 <Test as Trait1>::foo()
321 impl F for X { fn m(&self) { println!("I am F"); } }
322 impl G for X { fn m(&self) { println!("I am G"); } }
327 F::m(&f); // it displays "I am F"
328 G::m(&f); // it displays "I am G"
334 You tried to give a type parameter where it wasn't needed. Erroneous code
337 ```compile_fail,E0035
347 x.method::<i32>(); // Error: Test::method doesn't need type parameter!
351 To fix this error, just remove the type parameter:
363 x.method(); // OK, we're good!
369 This error occurrs when you pass too many or not enough type parameters to
370 a method. Erroneous code example:
372 ```compile_fail,E0036
376 fn method<T>(&self, v: &[T]) -> usize {
385 x.method::<i32, i32>(v); // error: only one type parameter is expected!
389 To fix it, just specify a correct number of type parameters:
395 fn method<T>(&self, v: &[T]) -> usize {
404 x.method::<i32>(v); // OK, we're good!
408 Please note on the last example that we could have called `method` like this:
416 It is not allowed to manually call destructors in Rust. It is also not
417 necessary to do this since `drop` is called automatically whenever a value goes
420 Here's an example of this error:
422 ```compile_fail,E0040
434 let mut x = Foo { x: -7 };
435 x.drop(); // error: explicit use of destructor method
441 You can't use type parameters on foreign items. Example of erroneous code:
443 ```compile_fail,E0044
444 extern { fn some_func<T>(x: T); }
447 To fix this, replace the type parameter with the specializations that you
451 extern { fn some_func_i32(x: i32); }
452 extern { fn some_func_i64(x: i64); }
457 Rust only supports variadic parameters for interoperability with C code in its
458 FFI. As such, variadic parameters can only be used with functions which are
459 using the C ABI. Examples of erroneous code:
462 #![feature(unboxed_closures)]
464 extern "rust-call" { fn foo(x: u8, ...); }
468 fn foo(x: u8, ...) {}
471 To fix such code, put them in an extern "C" block:
481 Items are missing in a trait implementation. Erroneous code example:
483 ```compile_fail,E0046
491 // error: not all trait items implemented, missing: `foo`
494 When trying to make some type implement a trait `Foo`, you must, at minimum,
495 provide implementations for all of `Foo`'s required methods (meaning the
496 methods that do not have default implementations), as well as any required
497 trait items like associated types or constants. Example:
513 This error indicates that an attempted implementation of a trait method
514 has the wrong number of type parameters.
516 For example, the trait below has a method `foo` with a type parameter `T`,
517 but the implementation of `foo` for the type `Bar` is missing this parameter:
519 ```compile_fail,E0049
521 fn foo<T: Default>(x: T) -> Self;
526 // error: method `foo` has 0 type parameters but its trait declaration has 1
529 fn foo(x: bool) -> Self { Bar }
535 This error indicates that an attempted implementation of a trait method
536 has the wrong number of function parameters.
538 For example, the trait below has a method `foo` with two function parameters
539 (`&self` and `u8`), but the implementation of `foo` for the type `Bar` omits
542 ```compile_fail,E0050
544 fn foo(&self, x: u8) -> bool;
549 // error: method `foo` has 1 parameter but the declaration in trait `Foo::foo`
552 fn foo(&self) -> bool { true }
558 The parameters of any trait method must match between a trait implementation
559 and the trait definition.
561 Here are a couple examples of this error:
563 ```compile_fail,E0053
572 // error, expected u16, found i16
575 // error, types differ in mutability
576 fn bar(&mut self) { }
582 It is not allowed to cast to a bool. If you are trying to cast a numeric type
583 to a bool, you can compare it with zero instead:
585 ```compile_fail,E0054
588 // Not allowed, won't compile
589 let x_is_nonzero = x as bool;
596 let x_is_nonzero = x != 0;
601 During a method call, a value is automatically dereferenced as many times as
602 needed to make the value's type match the method's receiver. The catch is that
603 the compiler will only attempt to dereference a number of times up to the
604 recursion limit (which can be set via the `recursion_limit` attribute).
606 For a somewhat artificial example:
608 ```compile_fail,E0055
609 #![recursion_limit="2"]
621 // error, reached the recursion limit while auto-dereferencing &&Foo
626 One fix may be to increase the recursion limit. Note that it is possible to
627 create an infinite recursion of dereferencing, in which case the only fix is to
628 somehow break the recursion.
632 When invoking closures or other implementations of the function traits `Fn`,
633 `FnMut` or `FnOnce` using call notation, the number of parameters passed to the
634 function must match its definition.
636 An example using a closure:
638 ```compile_fail,E0057
640 let a = f(); // invalid, too few parameters
641 let b = f(4); // this works!
642 let c = f(2, 3); // invalid, too many parameters
645 A generic function must be treated similarly:
648 fn foo<F: Fn()>(f: F) {
649 f(); // this is valid, but f(3) would not work
655 The built-in function traits are generic over a tuple of the function arguments.
656 If one uses angle-bracket notation (`Fn<(T,), Output=U>`) instead of parentheses
657 (`Fn(T) -> U`) to denote the function trait, the type parameter should be a
658 tuple. Otherwise function call notation cannot be used and the trait will not be
659 implemented by closures.
661 The most likely source of this error is using angle-bracket notation without
662 wrapping the function argument type into a tuple, for example:
664 ```compile_fail,E0059
665 #![feature(unboxed_closures)]
667 fn foo<F: Fn<i32>>(f: F) -> F::Output { f(3) }
670 It can be fixed by adjusting the trait bound like this:
673 #![feature(unboxed_closures)]
675 fn foo<F: Fn<(i32,)>>(f: F) -> F::Output { f(3) }
678 Note that `(T,)` always denotes the type of a 1-tuple containing an element of
679 type `T`. The comma is necessary for syntactic disambiguation.
683 External C functions are allowed to be variadic. However, a variadic function
684 takes a minimum number of arguments. For example, consider C's variadic `printf`
689 use libc::{ c_char, c_int };
692 fn printf(_: *const c_char, ...) -> c_int;
696 Using this declaration, it must be called with at least one argument, so
697 simply calling `printf()` is invalid. But the following uses are allowed:
701 use std::ffi::CString;
703 printf(CString::new("test\n").unwrap().as_ptr());
704 printf(CString::new("number = %d\n").unwrap().as_ptr(), 3);
705 printf(CString::new("%d, %d\n").unwrap().as_ptr(), 10, 5);
711 The number of arguments passed to a function must match the number of arguments
712 specified in the function signature.
714 For example, a function like:
717 fn f(a: u16, b: &str) {}
720 Must always be called with exactly two arguments, e.g. `f(2, "test")`.
722 Note that Rust does not have a notion of optional function arguments or
723 variadic functions (except for its C-FFI).
727 This error indicates that during an attempt to build a struct or struct-like
728 enum variant, one of the fields was specified more than once. Erroneous code
731 ```compile_fail,E0062
739 x: 0, // error: field `x` specified more than once
744 Each field should be specified exactly one time. Example:
752 let x = Foo { x: 0 }; // ok!
758 This error indicates that during an attempt to build a struct or struct-like
759 enum variant, one of the fields was not provided. Erroneous code example:
761 ```compile_fail,E0063
768 let x = Foo { x: 0 }; // error: missing field: `y`
772 Each field should be specified exactly once. Example:
781 let x = Foo { x: 0, y: 0 }; // ok!
787 Box placement expressions (like C++'s "placement new") do not yet support any
788 place expression except the exchange heap (i.e. `std::boxed::HEAP`).
789 Furthermore, the syntax is changing to use `in` instead of `box`. See [RFC 470]
790 and [RFC 809] for more details.
792 [RFC 470]: https://github.com/rust-lang/rfcs/pull/470
793 [RFC 809]: https://github.com/rust-lang/rfcs/blob/master/text/0809-box-and-in-for-stdlib.md
797 The left-hand side of a compound assignment expression must be an lvalue
798 expression. An lvalue expression represents a memory location and includes
799 item paths (ie, namespaced variables), dereferences, indexing expressions,
800 and field references.
802 Let's start with some erroneous code examples:
804 ```compile_fail,E0067
805 use std::collections::LinkedList;
807 // Bad: assignment to non-lvalue expression
808 LinkedList::new() += 1;
812 fn some_func(i: &mut i32) {
813 i += 12; // Error : '+=' operation cannot be applied on a reference !
817 And now some working examples:
826 fn some_func(i: &mut i32) {
833 The compiler found a function whose body contains a `return;` statement but
834 whose return type is not `()`. An example of this is:
836 ```compile_fail,E0069
843 Since `return;` is just like `return ();`, there is a mismatch between the
844 function's return type and the value being returned.
848 The left-hand side of an assignment operator must be an lvalue expression. An
849 lvalue expression represents a memory location and can be a variable (with
850 optional namespacing), a dereference, an indexing expression or a field
853 More details can be found here:
854 https://doc.rust-lang.org/reference.html#lvalues-rvalues-and-temporaries
856 Now, we can go further. Here are some erroneous code examples:
858 ```compile_fail,E0070
864 const SOME_CONST : i32 = 12;
866 fn some_other_func() {}
869 SOME_CONST = 14; // error : a constant value cannot be changed!
870 1 = 3; // error : 1 isn't a valid lvalue!
871 some_other_func() = 4; // error : we can't assign value to a function!
872 SomeStruct.x = 12; // error : SomeStruct a structure name but it is used
877 And now let's give working examples:
884 let mut s = SomeStruct {x: 0, y: 0};
886 s.x = 3; // that's good !
890 fn some_func(x: &mut i32) {
891 *x = 12; // that's good !
897 You tried to use structure-literal syntax to create an item that is
898 not a structure or enum variant.
900 Example of erroneous code:
902 ```compile_fail,E0071
904 let t = U32 { value: 4 }; // error: expected struct, variant or union type,
905 // found builtin type `u32`
908 To fix this, ensure that the name was correctly spelled, and that
909 the correct form of initializer was used.
911 For example, the code above can be fixed to:
919 let u = Foo::FirstValue(0i32);
927 You cannot define a struct (or enum) `Foo` that requires an instance of `Foo`
928 in order to make a new `Foo` value. This is because there would be no way a
929 first instance of `Foo` could be made to initialize another instance!
931 Here's an example of a struct that has this problem:
934 struct Foo { x: Box<Foo> } // error
937 One fix is to use `Option`, like so:
940 struct Foo { x: Option<Box<Foo>> }
943 Now it's possible to create at least one instance of `Foo`: `Foo { x: None }`.
947 When using the `#[simd]` attribute on a tuple struct, the components of the
948 tuple struct must all be of a concrete, nongeneric type so the compiler can
949 reason about how to use SIMD with them. This error will occur if the types
952 This will cause an error:
955 #![feature(repr_simd)]
958 struct Bad<T>(T, T, T);
964 #![feature(repr_simd)]
967 struct Good(u32, u32, u32);
972 The `#[simd]` attribute can only be applied to non empty tuple structs, because
973 it doesn't make sense to try to use SIMD operations when there are no values to
976 This will cause an error:
978 ```compile_fail,E0075
979 #![feature(repr_simd)]
988 #![feature(repr_simd)]
996 When using the `#[simd]` attribute to automatically use SIMD operations in tuple
997 struct, the types in the struct must all be of the same type, or the compiler
998 will trigger this error.
1000 This will cause an error:
1002 ```compile_fail,E0076
1003 #![feature(repr_simd)]
1006 struct Bad(u16, u32, u32);
1012 #![feature(repr_simd)]
1015 struct Good(u32, u32, u32);
1020 When using the `#[simd]` attribute on a tuple struct, the elements in the tuple
1021 must be machine types so SIMD operations can be applied to them.
1023 This will cause an error:
1025 ```compile_fail,E0077
1026 #![feature(repr_simd)]
1035 #![feature(repr_simd)]
1038 struct Good(u32, u32, u32);
1043 Enum discriminants are used to differentiate enum variants stored in memory.
1044 This error indicates that the same value was used for two or more variants,
1045 making them impossible to tell apart.
1047 ```compile_fail,E0081
1065 Note that variants without a manually specified discriminant are numbered from
1066 top to bottom starting from 0, so clashes can occur with seemingly unrelated
1069 ```compile_fail,E0081
1076 Here `X` will have already been specified the discriminant 0 by the time `Y` is
1077 encountered, so a conflict occurs.
1081 When you specify enum discriminants with `=`, the compiler expects `isize`
1082 values by default. Or you can add the `repr` attibute to the enum declaration
1083 for an explicit choice of the discriminant type. In either cases, the
1084 discriminant values must fall within a valid range for the expected type;
1085 otherwise this error is raised. For example:
1095 Here, 1024 lies outside the valid range for `u8`, so the discriminant for `A` is
1096 invalid. Here is another, more subtle example which depends on target word size:
1099 enum DependsOnPointerSize {
1104 Here, `1 << 32` is interpreted as an `isize` value. So it is invalid for 32 bit
1105 target (`target_pointer_width = "32"`) but valid for 64 bit target.
1107 You may want to change representation types to fix this, or else change invalid
1108 discriminant values so that they fit within the existing type.
1112 An unsupported representation was attempted on a zero-variant enum.
1114 Erroneous code example:
1116 ```compile_fail,E0084
1118 enum NightsWatch {} // error: unsupported representation for zero-variant enum
1121 It is impossible to define an integer type to be used to represent zero-variant
1122 enum values because there are no zero-variant enum values. There is no way to
1123 construct an instance of the following type using only safe code. So you have
1124 two solutions. Either you add variants in your enum:
1134 or you remove the integer represention of your enum:
1142 Too many type parameters were supplied for a function. For example:
1144 ```compile_fail,E0087
1148 foo::<f64, bool>(); // error, expected 1 parameter, found 2 parameters
1152 The number of supplied parameters must exactly match the number of defined type
1157 You gave too many lifetime parameters. Erroneous code example:
1159 ```compile_fail,E0088
1163 f::<'static>() // error: too many lifetime parameters provided
1167 Please check you give the right number of lifetime parameters. Example:
1177 It's also important to note that the Rust compiler can generally
1178 determine the lifetime by itself. Example:
1186 // it can be written like this
1187 fn get_value<'a>(&'a self) -> &'a str { &self.value }
1188 // but the compiler works fine with this too:
1189 fn without_lifetime(&self) -> &str { &self.value }
1193 let f = Foo { value: "hello".to_owned() };
1195 println!("{}", f.get_value());
1196 println!("{}", f.without_lifetime());
1202 Not enough type parameters were supplied for a function. For example:
1204 ```compile_fail,E0089
1208 foo::<f64>(); // error, expected 2 parameters, found 1 parameter
1212 Note that if a function takes multiple type parameters but you want the compiler
1213 to infer some of them, you can use type placeholders:
1215 ```compile_fail,E0089
1216 fn foo<T, U>(x: T) {}
1220 foo::<f64>(x); // error, expected 2 parameters, found 1 parameter
1221 foo::<_, f64>(x); // same as `foo::<bool, f64>(x)`
1227 You gave too few lifetime parameters. Example:
1229 ```compile_fail,E0090
1230 fn foo<'a: 'b, 'b: 'a>() {}
1233 foo::<'static>(); // error, expected 2 lifetime parameters
1237 Please check you give the right number of lifetime parameters. Example:
1240 fn foo<'a: 'b, 'b: 'a>() {}
1243 foo::<'static, 'static>();
1249 You gave an unnecessary type parameter in a type alias. Erroneous code
1252 ```compile_fail,E0091
1253 type Foo<T> = u32; // error: type parameter `T` is unused
1255 type Foo<A,B> = Box<A>; // error: type parameter `B` is unused
1258 Please check you didn't write too many type parameters. Example:
1261 type Foo = u32; // ok!
1262 type Foo2<A> = Box<A>; // ok!
1267 You tried to declare an undefined atomic operation function.
1268 Erroneous code example:
1270 ```compile_fail,E0092
1271 #![feature(intrinsics)]
1273 extern "rust-intrinsic" {
1274 fn atomic_foo(); // error: unrecognized atomic operation
1279 Please check you didn't make a mistake in the function's name. All intrinsic
1280 functions are defined in librustc_trans/trans/intrinsic.rs and in
1281 libcore/intrinsics.rs in the Rust source code. Example:
1284 #![feature(intrinsics)]
1286 extern "rust-intrinsic" {
1287 fn atomic_fence(); // ok!
1293 You declared an unknown intrinsic function. Erroneous code example:
1295 ```compile_fail,E0093
1296 #![feature(intrinsics)]
1298 extern "rust-intrinsic" {
1299 fn foo(); // error: unrecognized intrinsic function: `foo`
1309 Please check you didn't make a mistake in the function's name. All intrinsic
1310 functions are defined in librustc_trans/trans/intrinsic.rs and in
1311 libcore/intrinsics.rs in the Rust source code. Example:
1314 #![feature(intrinsics)]
1316 extern "rust-intrinsic" {
1317 fn atomic_fence(); // ok!
1329 You gave an invalid number of type parameters to an intrinsic function.
1330 Erroneous code example:
1332 ```compile_fail,E0094
1333 #![feature(intrinsics)]
1335 extern "rust-intrinsic" {
1336 fn size_of<T, U>() -> usize; // error: intrinsic has wrong number
1337 // of type parameters
1341 Please check that you provided the right number of type parameters
1342 and verify with the function declaration in the Rust source code.
1346 #![feature(intrinsics)]
1348 extern "rust-intrinsic" {
1349 fn size_of<T>() -> usize; // ok!
1355 This error means that an incorrect number of lifetime parameters were provided
1356 for a type (like a struct or enum) or trait:
1358 ```compile_fail,E0107
1359 struct Foo<'a, 'b>(&'a str, &'b str);
1360 enum Bar { A, B, C }
1363 foo: Foo<'a>, // error: expected 2, found 1
1364 bar: Bar<'a>, // error: expected 0, found 1
1370 You tried to give a type parameter to a type which doesn't need it. Erroneous
1373 ```compile_fail,E0109
1374 type X = u32<i32>; // error: type parameters are not allowed on this type
1377 Please check that you used the correct type and recheck its definition. Perhaps
1378 it doesn't need the type parameter.
1383 type X = u32; // this compiles
1386 Note that type parameters for enum-variant constructors go after the variant,
1387 not after the enum (`Option::None::<u32>`, not `Option::<u32>::None`).
1391 You tried to give a lifetime parameter to a type which doesn't need it.
1392 Erroneous code example:
1394 ```compile_fail,E0110
1395 type X = u32<'static>; // error: lifetime parameters are not allowed on
1399 Please check that the correct type was used and recheck its definition; perhaps
1400 it doesn't need the lifetime parameter. Example:
1403 type X = u32; // ok!
1408 You can only define an inherent implementation for a type in the same crate
1409 where the type was defined. For example, an `impl` block as below is not allowed
1410 since `Vec` is defined in the standard library:
1412 ```compile_fail,E0116
1413 impl Vec<u8> { } // error
1416 To fix this problem, you can do either of these things:
1418 - define a trait that has the desired associated functions/types/constants and
1419 implement the trait for the type in question
1420 - define a new type wrapping the type and define an implementation on the new
1423 Note that using the `type` keyword does not work here because `type` only
1424 introduces a type alias:
1426 ```compile_fail,E0116
1427 type Bytes = Vec<u8>;
1429 impl Bytes { } // error, same as above
1434 This error indicates a violation of one of Rust's orphan rules for trait
1435 implementations. The rule prohibits any implementation of a foreign trait (a
1436 trait defined in another crate) where
1438 - the type that is implementing the trait is foreign
1439 - all of the parameters being passed to the trait (if there are any) are also
1442 Here's one example of this error:
1444 ```compile_fail,E0117
1445 impl Drop for u32 {}
1448 To avoid this kind of error, ensure that at least one local type is referenced
1452 pub struct Foo; // you define your type in your crate
1454 impl Drop for Foo { // and you can implement the trait on it!
1455 // code of trait implementation here
1458 impl From<Foo> for i32 { // or you use a type from your crate as
1460 fn from(i: Foo) -> i32 {
1466 Alternatively, define a trait locally and implement that instead:
1470 fn get(&self) -> usize;
1474 fn get(&self) -> usize { 0 }
1478 For information on the design of the orphan rules, see [RFC 1023].
1480 [RFC 1023]: https://github.com/rust-lang/rfcs/blob/master/text/1023-rebalancing-coherence.md
1484 You're trying to write an inherent implementation for something which isn't a
1485 struct nor an enum. Erroneous code example:
1487 ```compile_fail,E0118
1488 impl (u8, u8) { // error: no base type found for inherent implementation
1489 fn get_state(&self) -> String {
1495 To fix this error, please implement a trait on the type or wrap it in a struct.
1499 // we create a trait here
1500 trait LiveLongAndProsper {
1501 fn get_state(&self) -> String;
1504 // and now you can implement it on (u8, u8)
1505 impl LiveLongAndProsper for (u8, u8) {
1506 fn get_state(&self) -> String {
1507 "He's dead, Jim!".to_owned()
1512 Alternatively, you can create a newtype. A newtype is a wrapping tuple-struct.
1513 For example, `NewType` is a newtype over `Foo` in `struct NewType(Foo)`.
1517 struct TypeWrapper((u8, u8));
1520 fn get_state(&self) -> String {
1521 "Fascinating!".to_owned()
1528 An attempt was made to implement Drop on a trait, which is not allowed: only
1529 structs and enums can implement Drop. An example causing this error:
1531 ```compile_fail,E0120
1534 impl Drop for MyTrait {
1535 fn drop(&mut self) {}
1539 A workaround for this problem is to wrap the trait up in a struct, and implement
1540 Drop on that. An example is shown below:
1544 struct MyWrapper<T: MyTrait> { foo: T }
1546 impl <T: MyTrait> Drop for MyWrapper<T> {
1547 fn drop(&mut self) {}
1552 Alternatively, wrapping trait objects requires something like the following:
1557 //or Box<MyTrait>, if you wanted an owned trait object
1558 struct MyWrapper<'a> { foo: &'a MyTrait }
1560 impl <'a> Drop for MyWrapper<'a> {
1561 fn drop(&mut self) {}
1567 In order to be consistent with Rust's lack of global type inference, type
1568 placeholders are disallowed by design in item signatures.
1570 Examples of this error include:
1572 ```compile_fail,E0121
1573 fn foo() -> _ { 5 } // error, explicitly write out the return type instead
1575 static BAR: _ = "test"; // error, explicitly write out the type instead
1580 An attempt was made to add a generic constraint to a type alias. While Rust will
1581 allow this with a warning, it will not currently enforce the constraint.
1582 Consider the example below:
1587 type MyType<R: Foo> = (R, ());
1594 We're able to declare a variable of type `MyType<u32>`, despite the fact that
1595 `u32` does not implement `Foo`. As a result, one should avoid using generic
1596 constraints in concert with type aliases.
1600 You declared two fields of a struct with the same name. Erroneous code
1603 ```compile_fail,E0124
1606 field1: i32, // error: field is already declared
1610 Please verify that the field names have been correctly spelled. Example:
1621 It is not possible to define `main` with type parameters, or even with function
1622 parameters. When `main` is present, it must take no arguments and return `()`.
1623 Erroneous code example:
1625 ```compile_fail,E0131
1626 fn main<T>() { // error: main function is not allowed to have type parameters
1632 A function with the `start` attribute was declared with type parameters.
1634 Erroneous code example:
1636 ```compile_fail,E0132
1643 It is not possible to declare type parameters on a function that has the `start`
1644 attribute. Such a function must have the following type signature (for more
1645 information: http://doc.rust-lang.org/stable/book/no-stdlib.html):
1648 fn(isize, *const *const u8) -> isize;
1657 fn my_start(argc: isize, argv: *const *const u8) -> isize {
1664 This error means that an attempt was made to match a struct type enum
1665 variant as a non-struct type:
1667 ```compile_fail,E0164
1668 enum Foo { B { i: u32 } }
1670 fn bar(foo: Foo) -> u32 {
1672 Foo::B(i) => i, // error E0164
1677 Try using `{}` instead:
1680 enum Foo { B { i: u32 } }
1682 fn bar(foo: Foo) -> u32 {
1691 You bound an associated type in an expression path which is not
1694 Erroneous code example:
1696 ```compile_fail,E0182
1702 impl Foo for isize {
1704 fn bar() -> isize { 42 }
1707 // error: unexpected binding of associated item in expression path
1708 let x: isize = Foo::<A=usize>::bar();
1711 To give a concrete type when using the Universal Function Call Syntax,
1712 use "Type as Trait". Example:
1720 impl Foo for isize {
1722 fn bar() -> isize { 42 }
1725 let x: isize = <isize as Foo>::bar(); // ok!
1730 Explicitly implementing both Drop and Copy for a type is currently disallowed.
1731 This feature can make some sense in theory, but the current implementation is
1732 incorrect and can lead to memory unsafety (see [issue #20126][iss20126]), so
1733 it has been disabled for now.
1735 [iss20126]: https://github.com/rust-lang/rust/issues/20126
1739 An associated function for a trait was defined to be static, but an
1740 implementation of the trait declared the same function to be a method (i.e. to
1741 take a `self` parameter).
1743 Here's an example of this error:
1745 ```compile_fail,E0185
1753 // error, method `foo` has a `&self` declaration in the impl, but not in
1761 An associated function for a trait was defined to be a method (i.e. to take a
1762 `self` parameter), but an implementation of the trait declared the same function
1765 Here's an example of this error:
1767 ```compile_fail,E0186
1775 // error, method `foo` has a `&self` declaration in the trait, but not in
1783 Trait objects need to have all associated types specified. Erroneous code
1786 ```compile_fail,E0191
1791 type Foo = Trait; // error: the value of the associated type `Bar` (from
1792 // the trait `Trait`) must be specified
1795 Please verify you specified all associated types of the trait and that you
1796 used the right trait. Example:
1803 type Foo = Trait<Bar=i32>; // ok!
1808 Negative impls are only allowed for traits with default impls. For more
1809 information see the [opt-in builtin traits RFC][RFC 19].
1811 [RFC 19]: https://github.com/rust-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md
1815 `where` clauses must use generic type parameters: it does not make sense to use
1816 them otherwise. An example causing this error:
1823 #[derive(Copy,Clone)]
1828 impl Foo for Wrapper<u32> where Wrapper<u32>: Clone {
1833 This use of a `where` clause is strange - a more common usage would look
1834 something like the following:
1841 #[derive(Copy,Clone)]
1845 impl <T> Foo for Wrapper<T> where Wrapper<T>: Clone {
1850 Here, we're saying that the implementation exists on Wrapper only when the
1851 wrapped type `T` implements `Clone`. The `where` clause is important because
1852 some types will not implement `Clone`, and thus will not get this method.
1854 In our erroneous example, however, we're referencing a single concrete type.
1855 Since we know for certain that `Wrapper<u32>` implements `Clone`, there's no
1856 reason to also specify it in a `where` clause.
1860 A type parameter was declared which shadows an existing one. An example of this
1863 ```compile_fail,E0194
1865 fn do_something(&self) -> T;
1866 fn do_something_else<T: Clone>(&self, bar: T);
1870 In this example, the trait `Foo` and the trait method `do_something_else` both
1871 define a type parameter `T`. This is not allowed: if the method wishes to
1872 define a type parameter, it must use a different name for it.
1876 Your method's lifetime parameters do not match the trait declaration.
1877 Erroneous code example:
1879 ```compile_fail,E0195
1881 fn bar<'a,'b:'a>(x: &'a str, y: &'b str);
1886 impl Trait for Foo {
1887 fn bar<'a,'b>(x: &'a str, y: &'b str) {
1888 // error: lifetime parameters or bounds on method `bar`
1889 // do not match the trait declaration
1894 The lifetime constraint `'b` for bar() implementation does not match the
1895 trait declaration. Ensure lifetime declarations match exactly in both trait
1896 declaration and implementation. Example:
1900 fn t<'a,'b:'a>(x: &'a str, y: &'b str);
1905 impl Trait for Foo {
1906 fn t<'a,'b:'a>(x: &'a str, y: &'b str) { // ok!
1913 Inherent implementations (one that do not implement a trait but provide
1914 methods associated with a type) are always safe because they are not
1915 implementing an unsafe trait. Removing the `unsafe` keyword from the inherent
1916 implementation will resolve this error.
1918 ```compile_fail,E0197
1921 // this will cause this error
1923 // converting it to this will fix it
1929 A negative implementation is one that excludes a type from implementing a
1930 particular trait. Not being able to use a trait is always a safe operation,
1931 so negative implementations are always safe and never need to be marked as
1935 #![feature(optin_builtin_traits)]
1939 // unsafe is unnecessary
1940 unsafe impl !Clone for Foo { }
1946 #![feature(optin_builtin_traits)]
1952 impl Enterprise for .. { }
1954 impl !Enterprise for Foo { }
1957 Please note that negative impls are only allowed for traits with default impls.
1961 Safe traits should not have unsafe implementations, therefore marking an
1962 implementation for a safe trait unsafe will cause a compiler error. Removing
1963 the unsafe marker on the trait noted in the error will resolve this problem.
1965 ```compile_fail,E0199
1970 // this won't compile because Bar is safe
1971 unsafe impl Bar for Foo { }
1972 // this will compile
1973 impl Bar for Foo { }
1978 Unsafe traits must have unsafe implementations. This error occurs when an
1979 implementation for an unsafe trait isn't marked as unsafe. This may be resolved
1980 by marking the unsafe implementation as unsafe.
1982 ```compile_fail,E0200
1985 unsafe trait Bar { }
1987 // this won't compile because Bar is unsafe and impl isn't unsafe
1988 impl Bar for Foo { }
1989 // this will compile
1990 unsafe impl Bar for Foo { }
1995 It is an error to define two associated items (like methods, associated types,
1996 associated functions, etc.) with the same identifier.
2000 ```compile_fail,E0201
2004 fn bar(&self) -> bool { self.0 > 5 }
2005 fn bar() {} // error: duplicate associated function
2010 fn baz(&self) -> bool;
2016 fn baz(&self) -> bool { true }
2018 // error: duplicate method
2019 fn baz(&self) -> bool { self.0 > 5 }
2021 // error: duplicate associated type
2026 Note, however, that items with the same name are allowed for inherent `impl`
2027 blocks that don't overlap:
2033 fn bar(&self) -> bool { self.0 > 5 }
2037 fn bar(&self) -> bool { self.0 }
2043 Inherent associated types were part of [RFC 195] but are not yet implemented.
2044 See [the tracking issue][iss8995] for the status of this implementation.
2046 [RFC 195]: https://github.com/rust-lang/rfcs/blob/master/text/0195-associated-items.md
2047 [iss8995]: https://github.com/rust-lang/rust/issues/8995
2051 An attempt to implement the `Copy` trait for a struct failed because one of the
2052 fields does not implement `Copy`. To fix this, you must implement `Copy` for the
2053 mentioned field. Note that this may not be possible, as in the example of
2055 ```compile_fail,E0204
2060 impl Copy for Foo { }
2063 This fails because `Vec<T>` does not implement `Copy` for any `T`.
2065 Here's another example that will fail:
2067 ```compile_fail,E0204
2074 This fails because `&mut T` is not `Copy`, even when `T` is `Copy` (this
2075 differs from the behavior for `&T`, which is always `Copy`).
2080 An attempt to implement the `Copy` trait for an enum failed because one of the
2081 variants does not implement `Copy`. To fix this, you must implement `Copy` for
2082 the mentioned variant. Note that this may not be possible, as in the example of
2084 ```compile_fail,E0205
2090 impl Copy for Foo { }
2093 This fails because `Vec<T>` does not implement `Copy` for any `T`.
2095 Here's another example that will fail:
2097 ```compile_fail,E0205
2105 This fails because `&mut T` is not `Copy`, even when `T` is `Copy` (this
2106 differs from the behavior for `&T`, which is always `Copy`).
2111 You can only implement `Copy` for a struct or enum. Both of the following
2112 examples will fail, because neither `i32` (primitive type) nor `&'static Bar`
2113 (reference to `Bar`) is a struct or enum:
2115 ```compile_fail,E0206
2117 impl Copy for Foo { } // error
2119 #[derive(Copy, Clone)]
2121 impl Copy for &'static Bar { } // error
2126 Any type parameter or lifetime parameter of an `impl` must meet at least one of
2127 the following criteria:
2129 - it appears in the self type of the impl
2130 - for a trait impl, it appears in the trait reference
2131 - it is bound as an associated type
2135 Suppose we have a struct `Foo` and we would like to define some methods for it.
2136 The following definition leads to a compiler error:
2138 ```compile_fail,E0207
2141 impl<T: Default> Foo {
2142 // error: the type parameter `T` is not constrained by the impl trait, self
2143 // type, or predicates [E0207]
2144 fn get(&self) -> T {
2145 <T as Default>::default()
2150 The problem is that the parameter `T` does not appear in the self type (`Foo`)
2151 of the impl. In this case, we can fix the error by moving the type parameter
2152 from the `impl` to the method `get`:
2158 // Move the type parameter from the impl to the method
2160 fn get<T: Default>(&self) -> T {
2161 <T as Default>::default()
2168 As another example, suppose we have a `Maker` trait and want to establish a
2169 type `FooMaker` that makes `Foo`s:
2171 ```compile_fail,E0207
2174 fn make(&mut self) -> Self::Item;
2183 impl<T: Default> Maker for FooMaker {
2184 // error: the type parameter `T` is not constrained by the impl trait, self
2185 // type, or predicates [E0207]
2188 fn make(&mut self) -> Foo<T> {
2189 Foo { foo: <T as Default>::default() }
2194 This fails to compile because `T` does not appear in the trait or in the
2197 One way to work around this is to introduce a phantom type parameter into
2198 `FooMaker`, like so:
2201 use std::marker::PhantomData;
2205 fn make(&mut self) -> Self::Item;
2212 // Add a type parameter to `FooMaker`
2213 struct FooMaker<T> {
2214 phantom: PhantomData<T>,
2217 impl<T: Default> Maker for FooMaker<T> {
2220 fn make(&mut self) -> Foo<T> {
2222 foo: <T as Default>::default(),
2228 Another way is to do away with the associated type in `Maker` and use an input
2229 type parameter instead:
2232 // Use a type parameter instead of an associated type here
2234 fn make(&mut self) -> Item;
2243 impl<T: Default> Maker<Foo<T>> for FooMaker {
2244 fn make(&mut self) -> Foo<T> {
2245 Foo { foo: <T as Default>::default() }
2250 ### Additional information
2252 For more information, please see [RFC 447].
2254 [RFC 447]: https://github.com/rust-lang/rfcs/blob/master/text/0447-no-unused-impl-parameters.md
2258 This error indicates a violation of one of Rust's orphan rules for trait
2259 implementations. The rule concerns the use of type parameters in an
2260 implementation of a foreign trait (a trait defined in another crate), and
2261 states that type parameters must be "covered" by a local type. To understand
2262 what this means, it is perhaps easiest to consider a few examples.
2264 If `ForeignTrait` is a trait defined in some external crate `foo`, then the
2265 following trait `impl` is an error:
2267 ```compile_fail,E0210
2268 extern crate collections;
2269 use collections::range::RangeArgument;
2271 impl<T> RangeArgument<T> for T { } // error
2276 To work around this, it can be covered with a local type, `MyType`:
2279 struct MyType<T>(T);
2280 impl<T> ForeignTrait for MyType<T> { } // Ok
2283 Please note that a type alias is not sufficient.
2285 For another example of an error, suppose there's another trait defined in `foo`
2286 named `ForeignTrait2` that takes two type parameters. Then this `impl` results
2287 in the same rule violation:
2291 impl<T> ForeignTrait2<T, MyType<T>> for MyType2 { } // error
2294 The reason for this is that there are two appearances of type parameter `T` in
2295 the `impl` header, both as parameters for `ForeignTrait2`. The first appearance
2296 is uncovered, and so runs afoul of the orphan rule.
2298 Consider one more example:
2301 impl<T> ForeignTrait2<MyType<T>, T> for MyType2 { } // Ok
2304 This only differs from the previous `impl` in that the parameters `T` and
2305 `MyType<T>` for `ForeignTrait2` have been swapped. This example does *not*
2306 violate the orphan rule; it is permitted.
2308 To see why that last example was allowed, you need to understand the general
2309 rule. Unfortunately this rule is a bit tricky to state. Consider an `impl`:
2312 impl<P1, ..., Pm> ForeignTrait<T1, ..., Tn> for T0 { ... }
2315 where `P1, ..., Pm` are the type parameters of the `impl` and `T0, ..., Tn`
2316 are types. One of the types `T0, ..., Tn` must be a local type (this is another
2317 orphan rule, see the explanation for E0117). Let `i` be the smallest integer
2318 such that `Ti` is a local type. Then no type parameter can appear in any of the
2321 For information on the design of the orphan rules, see [RFC 1023].
2323 [RFC 1023]: https://github.com/rust-lang/rfcs/blob/master/text/1023-rebalancing-coherence.md
2328 You used a function or type which doesn't fit the requirements for where it was
2329 used. Erroneous code examples:
2332 #![feature(intrinsics)]
2334 extern "rust-intrinsic" {
2335 fn size_of<T>(); // error: intrinsic has wrong type
2340 fn main() -> i32 { 0 }
2341 // error: main function expects type: `fn() {main}`: expected (), found i32
2348 // error: mismatched types in range: expected u8, found i8
2358 fn x(self: Rc<Foo>) {}
2359 // error: mismatched self type: expected `Foo`: expected struct
2360 // `Foo`, found struct `alloc::rc::Rc`
2364 For the first code example, please check the function definition. Example:
2367 #![feature(intrinsics)]
2369 extern "rust-intrinsic" {
2370 fn size_of<T>() -> usize; // ok!
2374 The second case example is a bit particular : the main function must always
2375 have this definition:
2381 They never take parameters and never return types.
2383 For the third example, when you match, all patterns must have the same type
2384 as the type you're matching on. Example:
2390 0u8...3u8 => (), // ok!
2395 And finally, for the last example, only `Box<Self>`, `&Self`, `Self`,
2396 or `&mut Self` work as explicit self parameters. Example:
2402 fn x(self: Box<Foo>) {} // ok!
2409 A generic type was described using parentheses rather than angle brackets. For
2412 ```compile_fail,E0214
2414 let v: Vec(&str) = vec!["foo"];
2418 This is not currently supported: `v` should be defined as `Vec<&str>`.
2419 Parentheses are currently only used with generic types when defining parameters
2420 for `Fn`-family traits.
2424 You used an associated type which isn't defined in the trait.
2425 Erroneous code example:
2427 ```compile_fail,E0220
2432 type Foo = T1<F=i32>; // error: associated type `F` not found for `T1`
2439 // error: Baz is used but not declared
2440 fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool;
2444 Make sure that you have defined the associated type in the trait body.
2445 Also, verify that you used the right trait or you didn't misspell the
2446 associated type name. Example:
2453 type Foo = T1<Bar=i32>; // ok!
2459 type Baz; // we declare `Baz` in our trait.
2461 // and now we can use it here:
2462 fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool;
2468 An attempt was made to retrieve an associated type, but the type was ambiguous.
2471 ```compile_fail,E0221
2487 In this example, `Foo` defines an associated type `A`. `Bar` inherits that type
2488 from `Foo`, and defines another associated type of the same name. As a result,
2489 when we attempt to use `Self::A`, it's ambiguous whether we mean the `A` defined
2490 by `Foo` or the one defined by `Bar`.
2492 There are two options to work around this issue. The first is simply to rename
2493 one of the types. Alternatively, one can specify the intended type using the
2507 let _: <Self as Bar>::A;
2514 An attempt was made to retrieve an associated type, but the type was ambiguous.
2517 ```compile_fail,E0223
2518 trait MyTrait {type X; }
2521 let foo: MyTrait::X;
2525 The problem here is that we're attempting to take the type of X from MyTrait.
2526 Unfortunately, the type of X is not defined, because it's only made concrete in
2527 implementations of the trait. A working version of this code might look like:
2530 trait MyTrait {type X; }
2533 impl MyTrait for MyStruct {
2538 let foo: <MyStruct as MyTrait>::X;
2542 This syntax specifies that we want the X type from MyTrait, as made concrete in
2543 MyStruct. The reason that we cannot simply use `MyStruct::X` is that MyStruct
2544 might implement two different traits with identically-named associated types.
2545 This syntax allows disambiguation between the two.
2549 You attempted to use multiple types as bounds for a closure or trait object.
2550 Rust does not currently support this. A simple example that causes this error:
2552 ```compile_fail,E0225
2554 let _: Box<std::io::Read + std::io::Write>;
2558 Send and Sync are an exception to this rule: it's possible to have bounds of
2559 one non-builtin trait, plus either or both of Send and Sync. For example, the
2560 following compiles correctly:
2564 let _: Box<std::io::Read + Send + Sync>;
2570 An associated type binding was done outside of the type parameter declaration
2571 and `where` clause. Erroneous code example:
2573 ```compile_fail,E0229
2576 fn boo(&self) -> <Self as Foo>::A;
2581 impl Foo for isize {
2583 fn boo(&self) -> usize { 42 }
2586 fn baz<I>(x: &<I as Foo<A=Bar>>::A) {}
2587 // error: associated type bindings are not allowed here
2590 To solve this error, please move the type bindings in the type parameter
2594 fn baz<I: Foo<A=Bar>>(x: &<I as Foo>::A) {} // ok!
2597 Or in the `where` clause:
2600 fn baz<I>(x: &<I as Foo>::A) where I: Foo<A=Bar> {}
2605 The trait has more type parameters specified than appear in its definition.
2607 Erroneous example code:
2609 ```compile_fail,E0230
2610 #![feature(on_unimplemented)]
2611 #[rustc_on_unimplemented = "Trait error on `{Self}` with `<{A},{B},{C}>`"]
2612 // error: there is no type parameter C on trait TraitWithThreeParams
2613 trait TraitWithThreeParams<A,B>
2617 Include the correct number of type parameters and the compilation should
2621 #![feature(on_unimplemented)]
2622 #[rustc_on_unimplemented = "Trait error on `{Self}` with `<{A},{B},{C}>`"]
2623 trait TraitWithThreeParams<A,B,C> // ok!
2629 The attribute must have a value. Erroneous code example:
2631 ```compile_fail,E0232
2632 #![feature(on_unimplemented)]
2634 #[rustc_on_unimplemented] // error: this attribute must have a value
2638 Please supply the missing value of the attribute. Example:
2641 #![feature(on_unimplemented)]
2643 #[rustc_on_unimplemented = "foo"] // ok!
2649 This error indicates that not enough type parameters were found in a type or
2652 For example, the `Foo` struct below is defined to be generic in `T`, but the
2653 type parameter is missing in the definition of `Bar`:
2655 ```compile_fail,E0243
2656 struct Foo<T> { x: T }
2658 struct Bar { x: Foo }
2663 This error indicates that too many type parameters were found in a type or
2666 For example, the `Foo` struct below has no type parameters, but is supplied
2667 with two in the definition of `Bar`:
2669 ```compile_fail,E0244
2670 struct Foo { x: bool }
2672 struct Bar<S, T> { x: Foo<S, T> }
2677 If an impl has a generic parameter with the `#[may_dangle]` attribute, then
2678 that impl must be declared as an `unsafe impl. For example:
2680 ```compile_fail,E0569
2681 #![feature(generic_param_attrs)]
2682 #![feature(dropck_eyepatch)]
2685 impl<#[may_dangle] X> Drop for Foo<X> {
2686 fn drop(&mut self) { }
2690 In this example, we are asserting that the destructor for `Foo` will not
2691 access any data of type `X`, and require this assertion to be true for
2692 overall safety in our program. The compiler does not currently attempt to
2693 verify this assertion; therefore we must tag this `impl` as unsafe.
2697 Default impls for a trait must be located in the same crate where the trait was
2698 defined. For more information see the [opt-in builtin traits RFC][RFC 19].
2700 [RFC 19]: https://github.com/rust-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md
2704 A cross-crate opt-out trait was implemented on something which wasn't a struct
2705 or enum type. Erroneous code example:
2707 ```compile_fail,E0321
2708 #![feature(optin_builtin_traits)]
2712 impl !Sync for Foo {}
2714 unsafe impl Send for &'static Foo {}
2715 // error: cross-crate traits with a default impl, like `core::marker::Send`,
2716 // can only be implemented for a struct/enum type, not
2720 Only structs and enums are permitted to impl Send, Sync, and other opt-out
2721 trait, and the struct or enum must be local to the current crate. So, for
2722 example, `unsafe impl Send for Rc<Foo>` is not allowed.
2726 The `Sized` trait is a special trait built-in to the compiler for types with a
2727 constant size known at compile-time. This trait is automatically implemented
2728 for types as needed by the compiler, and it is currently disallowed to
2729 explicitly implement it for a type.
2733 An associated const was implemented when another trait item was expected.
2734 Erroneous code example:
2736 ```compile_fail,E0323
2737 #![feature(associated_consts)]
2747 // error: item `N` is an associated const, which doesn't match its
2748 // trait `<Bar as Foo>`
2752 Please verify that the associated const wasn't misspelled and the correct trait
2753 was implemented. Example:
2763 type N = u32; // ok!
2770 #![feature(associated_consts)]
2779 const N : u32 = 0; // ok!
2785 A method was implemented when another trait item was expected. Erroneous
2788 ```compile_fail,E0324
2789 #![feature(associated_consts)]
2801 // error: item `N` is an associated method, which doesn't match its
2802 // trait `<Bar as Foo>`
2806 To fix this error, please verify that the method name wasn't misspelled and
2807 verify that you are indeed implementing the correct trait items. Example:
2810 #![feature(associated_consts)]
2829 An associated type was implemented when another trait item was expected.
2830 Erroneous code example:
2832 ```compile_fail,E0325
2833 #![feature(associated_consts)]
2843 // error: item `N` is an associated type, which doesn't match its
2844 // trait `<Bar as Foo>`
2848 Please verify that the associated type name wasn't misspelled and your
2849 implementation corresponds to the trait definition. Example:
2859 type N = u32; // ok!
2866 #![feature(associated_consts)]
2875 const N : u32 = 0; // ok!
2881 The types of any associated constants in a trait implementation must match the
2882 types in the trait definition. This error indicates that there was a mismatch.
2884 Here's an example of this error:
2886 ```compile_fail,E0326
2887 #![feature(associated_consts)]
2896 const BAR: u32 = 5; // error, expected bool, found u32
2902 The Unsize trait should not be implemented directly. All implementations of
2903 Unsize are provided automatically by the compiler.
2905 Erroneous code example:
2907 ```compile_fail,E0328
2910 use std::marker::Unsize;
2914 impl<T> Unsize<T> for MyType {}
2917 If you are defining your own smart pointer type and would like to enable
2918 conversion from a sized to an unsized type with the
2919 [DST coercion system][RFC 982], use [`CoerceUnsized`] instead.
2922 #![feature(coerce_unsized)]
2924 use std::ops::CoerceUnsized;
2926 pub struct MyType<T: ?Sized> {
2927 field_with_unsized_type: T,
2930 impl<T, U> CoerceUnsized<MyType<U>> for MyType<T>
2931 where T: CoerceUnsized<U> {}
2934 [RFC 982]: https://github.com/rust-lang/rfcs/blob/master/text/0982-dst-coercion.md
2935 [`CoerceUnsized`]: https://doc.rust-lang.org/std/ops/trait.CoerceUnsized.html
2939 An attempt was made to access an associated constant through either a generic
2940 type parameter or `Self`. This is not supported yet. An example causing this
2941 error is shown below:
2944 #![feature(associated_consts)]
2952 impl Foo for MyStruct {
2953 const BAR: f64 = 0f64;
2956 fn get_bar_bad<F: Foo>(t: F) -> f64 {
2961 Currently, the value of `BAR` for a particular type can only be accessed
2962 through a concrete type, as shown below:
2965 #![feature(associated_consts)]
2973 fn get_bar_good() -> f64 {
2974 <MyStruct as Foo>::BAR
2980 An attempt was made to implement `Drop` on a concrete specialization of a
2981 generic type. An example is shown below:
2983 ```compile_fail,E0366
2988 impl Drop for Foo<u32> {
2989 fn drop(&mut self) {}
2993 This code is not legal: it is not possible to specialize `Drop` to a subset of
2994 implementations of a generic type. One workaround for this is to wrap the
2995 generic type, as shown below:
3007 fn drop(&mut self) {}
3013 An attempt was made to implement `Drop` on a specialization of a generic type.
3014 An example is shown below:
3016 ```compile_fail,E0367
3019 struct MyStruct<T> {
3023 impl<T: Foo> Drop for MyStruct<T> {
3024 fn drop(&mut self) {}
3028 This code is not legal: it is not possible to specialize `Drop` to a subset of
3029 implementations of a generic type. In order for this code to work, `MyStruct`
3030 must also require that `T` implements `Foo`. Alternatively, another option is
3031 to wrap the generic type in another that specializes appropriately:
3036 struct MyStruct<T> {
3040 struct MyStructWrapper<T: Foo> {
3044 impl <T: Foo> Drop for MyStructWrapper<T> {
3045 fn drop(&mut self) {}
3051 This error indicates that a binary assignment operator like `+=` or `^=` was
3052 applied to a type that doesn't support it. For example:
3054 ```compile_fail,E0368
3055 let mut x = 12f32; // error: binary operation `<<` cannot be applied to
3061 To fix this error, please check that this type implements this binary
3065 let mut x = 12u32; // the `u32` type does implement the `ShlAssign` trait
3070 It is also possible to overload most operators for your own type by
3071 implementing the `[OP]Assign` traits from `std::ops`.
3073 Another problem you might be facing is this: suppose you've overloaded the `+`
3074 operator for some type `Foo` by implementing the `std::ops::Add` trait for
3075 `Foo`, but you find that using `+=` does not work, as in this example:
3077 ```compile_fail,E0368
3085 fn add(self, rhs: Foo) -> Foo {
3091 let mut x: Foo = Foo(5);
3092 x += Foo(7); // error, `+= cannot be applied to the type `Foo`
3096 This is because `AddAssign` is not automatically implemented, so you need to
3097 manually implement it for your type.
3101 A binary operation was attempted on a type which doesn't support it.
3102 Erroneous code example:
3104 ```compile_fail,E0369
3105 let x = 12f32; // error: binary operation `<<` cannot be applied to
3111 To fix this error, please check that this type implements this binary
3115 let x = 12u32; // the `u32` type does implement it:
3116 // https://doc.rust-lang.org/stable/std/ops/trait.Shl.html
3121 It is also possible to overload most operators for your own type by
3122 implementing traits from `std::ops`.
3124 String concatenation appends the string on the right to the string on the
3125 left and may require reallocation. This requires ownership of the string
3126 on the left. If something should be added to a string literal, move the
3127 literal to the heap by allocating it with `to_owned()` like in
3128 `"Your text".to_owned()`.
3133 The maximum value of an enum was reached, so it cannot be automatically
3134 set in the next enum value. Erroneous code example:
3137 #[deny(overflowing_literals)]
3139 X = 0x7fffffffffffffff,
3140 Y, // error: enum discriminant overflowed on value after
3141 // 9223372036854775807: i64; set explicitly via
3142 // Y = -9223372036854775808 if that is desired outcome
3146 To fix this, please set manually the next enum value or put the enum variant
3147 with the maximum value at the end of the enum. Examples:
3151 X = 0x7fffffffffffffff,
3161 X = 0x7fffffffffffffff,
3167 When `Trait2` is a subtrait of `Trait1` (for example, when `Trait2` has a
3168 definition like `trait Trait2: Trait1 { ... }`), it is not allowed to implement
3169 `Trait1` for `Trait2`. This is because `Trait2` already implements `Trait1` by
3170 definition, so it is not useful to do this.
3174 ```compile_fail,E0371
3175 trait Foo { fn foo(&self) { } }
3179 impl Bar for Baz { } // error, `Baz` implements `Bar` by definition
3180 impl Foo for Baz { } // error, `Baz` implements `Bar` which implements `Foo`
3181 impl Baz for Baz { } // error, `Baz` (trivially) implements `Baz`
3182 impl Baz for Bar { } // Note: This is OK
3187 A struct without a field containing an unsized type cannot implement
3189 [unsized type](https://doc.rust-lang.org/book/unsized-types.html)
3190 is any type that the compiler doesn't know the length or alignment of at
3191 compile time. Any struct containing an unsized type is also unsized.
3193 Example of erroneous code:
3195 ```compile_fail,E0374
3196 #![feature(coerce_unsized)]
3197 use std::ops::CoerceUnsized;
3199 struct Foo<T: ?Sized> {
3203 // error: Struct `Foo` has no unsized fields that need `CoerceUnsized`.
3204 impl<T, U> CoerceUnsized<Foo<U>> for Foo<T>
3205 where T: CoerceUnsized<U> {}
3208 `CoerceUnsized` is used to coerce one struct containing an unsized type
3209 into another struct containing a different unsized type. If the struct
3210 doesn't have any fields of unsized types then you don't need explicit
3211 coercion to get the types you want. To fix this you can either
3212 not try to implement `CoerceUnsized` or you can add a field that is
3213 unsized to the struct.
3218 #![feature(coerce_unsized)]
3219 use std::ops::CoerceUnsized;
3221 // We don't need to impl `CoerceUnsized` here.
3226 // We add the unsized type field to the struct.
3227 struct Bar<T: ?Sized> {
3232 // The struct has an unsized field so we can implement
3233 // `CoerceUnsized` for it.
3234 impl<T, U> CoerceUnsized<Bar<U>> for Bar<T>
3235 where T: CoerceUnsized<U> {}
3238 Note that `CoerceUnsized` is mainly used by smart pointers like `Box`, `Rc`
3239 and `Arc` to be able to mark that they can coerce unsized types that they
3244 A struct with more than one field containing an unsized type cannot implement
3245 `CoerceUnsized`. This only occurs when you are trying to coerce one of the
3246 types in your struct to another type in the struct. In this case we try to
3247 impl `CoerceUnsized` from `T` to `U` which are both types that the struct
3248 takes. An [unsized type](https://doc.rust-lang.org/book/unsized-types.html)
3249 is any type that the compiler doesn't know the length or alignment of at
3250 compile time. Any struct containing an unsized type is also unsized.
3252 Example of erroneous code:
3254 ```compile_fail,E0375
3255 #![feature(coerce_unsized)]
3256 use std::ops::CoerceUnsized;
3258 struct Foo<T: ?Sized, U: ?Sized> {
3264 // error: Struct `Foo` has more than one unsized field.
3265 impl<T, U> CoerceUnsized<Foo<U, T>> for Foo<T, U> {}
3268 `CoerceUnsized` only allows for coercion from a structure with a single
3269 unsized type field to another struct with a single unsized type field.
3270 In fact Rust only allows for a struct to have one unsized type in a struct
3271 and that unsized type must be the last field in the struct. So having two
3272 unsized types in a single struct is not allowed by the compiler. To fix this
3273 use only one field containing an unsized type in the struct and then use
3274 multiple structs to manage each unsized type field you need.
3279 #![feature(coerce_unsized)]
3280 use std::ops::CoerceUnsized;
3282 struct Foo<T: ?Sized> {
3287 impl <T, U> CoerceUnsized<Foo<U>> for Foo<T>
3288 where T: CoerceUnsized<U> {}
3290 fn coerce_foo<T: CoerceUnsized<U>, U>(t: T) -> Foo<U> {
3291 Foo { a: 12i32, b: t } // we use coercion to get the `Foo<U>` type we need
3298 The type you are trying to impl `CoerceUnsized` for is not a struct.
3299 `CoerceUnsized` can only be implemented for a struct. Unsized types are
3300 already able to be coerced without an implementation of `CoerceUnsized`
3301 whereas a struct containing an unsized type needs to know the unsized type
3302 field it's containing is able to be coerced. An
3303 [unsized type](https://doc.rust-lang.org/book/unsized-types.html)
3304 is any type that the compiler doesn't know the length or alignment of at
3305 compile time. Any struct containing an unsized type is also unsized.
3307 Example of erroneous code:
3309 ```compile_fail,E0376
3310 #![feature(coerce_unsized)]
3311 use std::ops::CoerceUnsized;
3313 struct Foo<T: ?Sized> {
3317 // error: The type `U` is not a struct
3318 impl<T, U> CoerceUnsized<U> for Foo<T> {}
3321 The `CoerceUnsized` trait takes a struct type. Make sure the type you are
3322 providing to `CoerceUnsized` is a struct with only the last field containing an
3328 #![feature(coerce_unsized)]
3329 use std::ops::CoerceUnsized;
3335 // The `Foo<U>` is a struct so `CoerceUnsized` can be implemented
3336 impl<T, U> CoerceUnsized<Foo<U>> for Foo<T> where T: CoerceUnsized<U> {}
3339 Note that in Rust, structs can only contain an unsized type if the field
3340 containing the unsized type is the last and only unsized type field in the
3345 Default impls are only allowed for traits with no methods or associated items.
3346 For more information see the [opt-in builtin traits RFC][RFC 19].
3348 [RFC 19]: https://github.com/rust-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md
3352 You tried to implement methods for a primitive type. Erroneous code example:
3354 ```compile_fail,E0390
3360 // error: only a single inherent implementation marked with
3361 // `#[lang = "mut_ptr"]` is allowed for the `*mut T` primitive
3364 This isn't allowed, but using a trait to implement a method is a good solution.
3376 impl Bar for *mut Foo {
3383 This error indicates that a type or lifetime parameter has been declared
3384 but not actually used. Here is an example that demonstrates the error:
3386 ```compile_fail,E0392
3392 If the type parameter was included by mistake, this error can be fixed
3393 by simply removing the type parameter, as shown below:
3401 Alternatively, if the type parameter was intentionally inserted, it must be
3402 used. A simple fix is shown below:
3410 This error may also commonly be found when working with unsafe code. For
3411 example, when using raw pointers one may wish to specify the lifetime for
3412 which the pointed-at data is valid. An initial attempt (below) causes this
3415 ```compile_fail,E0392
3421 We want to express the constraint that Foo should not outlive `'a`, because
3422 the data pointed to by `T` is only valid for that lifetime. The problem is
3423 that there are no actual uses of `'a`. It's possible to work around this
3424 by adding a PhantomData type to the struct, using it to tell the compiler
3425 to act as if the struct contained a borrowed reference `&'a T`:
3428 use std::marker::PhantomData;
3430 struct Foo<'a, T: 'a> {
3432 phantom: PhantomData<&'a T>
3436 PhantomData can also be used to express information about unused type
3437 parameters. You can read more about it in the API documentation:
3439 https://doc.rust-lang.org/std/marker/struct.PhantomData.html
3443 A type parameter which references `Self` in its default value was not specified.
3444 Example of erroneous code:
3446 ```compile_fail,E0393
3449 fn together_we_will_rule_the_galaxy(son: &A) {}
3450 // error: the type parameter `T` must be explicitly specified in an
3451 // object type because its default value `Self` references the
3455 A trait object is defined over a single, fully-defined trait. With a regular
3456 default parameter, this parameter can just be substituted in. However, if the
3457 default parameter is `Self`, the trait changes for each concrete type; i.e.
3458 `i32` will be expected to implement `A<i32>`, `bool` will be expected to
3459 implement `A<bool>`, etc... These types will not share an implementation of a
3460 fully-defined trait; instead they share implementations of a trait with
3461 different parameters substituted in for each implementation. This is
3462 irreconcilable with what we need to make a trait object work, and is thus
3463 disallowed. Making the trait concrete by explicitly specifying the value of the
3464 defaulted parameter will fix this issue. Fixed example:
3469 fn together_we_will_rule_the_galaxy(son: &A<i32>) {} // Ok!
3474 You implemented a trait, overriding one or more of its associated types but did
3475 not reimplement its default methods.
3477 Example of erroneous code:
3479 ```compile_fail,E0399
3480 #![feature(associated_type_defaults)]
3488 // error - the following trait items need to be reimplemented as
3489 // `Assoc` was overridden: `bar`
3494 To fix this, add an implementation for each default method from the trait:
3497 #![feature(associated_type_defaults)]
3506 fn bar(&self) {} // ok!
3512 The length of the platform-intrinsic function `simd_shuffle`
3513 wasn't specified. Erroneous code example:
3515 ```compile_fail,E0439
3516 #![feature(platform_intrinsics)]
3518 extern "platform-intrinsic" {
3519 fn simd_shuffle<A,B>(a: A, b: A, c: [u32; 8]) -> B;
3520 // error: invalid `simd_shuffle`, needs length: `simd_shuffle`
3524 The `simd_shuffle` function needs the length of the array passed as
3525 last parameter in its name. Example:
3528 #![feature(platform_intrinsics)]
3530 extern "platform-intrinsic" {
3531 fn simd_shuffle8<A,B>(a: A, b: A, c: [u32; 8]) -> B;
3537 A platform-specific intrinsic function has the wrong number of type
3538 parameters. Erroneous code example:
3540 ```compile_fail,E0440
3541 #![feature(repr_simd)]
3542 #![feature(platform_intrinsics)]
3545 struct f64x2(f64, f64);
3547 extern "platform-intrinsic" {
3548 fn x86_mm_movemask_pd<T>(x: f64x2) -> i32;
3549 // error: platform-specific intrinsic has wrong number of type
3554 Please refer to the function declaration to see if it corresponds
3555 with yours. Example:
3558 #![feature(repr_simd)]
3559 #![feature(platform_intrinsics)]
3562 struct f64x2(f64, f64);
3564 extern "platform-intrinsic" {
3565 fn x86_mm_movemask_pd(x: f64x2) -> i32;
3571 An unknown platform-specific intrinsic function was used. Erroneous
3574 ```compile_fail,E0441
3575 #![feature(repr_simd)]
3576 #![feature(platform_intrinsics)]
3579 struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
3581 extern "platform-intrinsic" {
3582 fn x86_mm_adds_ep16(x: i16x8, y: i16x8) -> i16x8;
3583 // error: unrecognized platform-specific intrinsic function
3587 Please verify that the function name wasn't misspelled, and ensure
3588 that it is declared in the rust source code (in the file
3589 src/librustc_platform_intrinsics/x86.rs). Example:
3592 #![feature(repr_simd)]
3593 #![feature(platform_intrinsics)]
3596 struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
3598 extern "platform-intrinsic" {
3599 fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i16x8; // ok!
3605 Intrinsic argument(s) and/or return value have the wrong type.
3606 Erroneous code example:
3608 ```compile_fail,E0442
3609 #![feature(repr_simd)]
3610 #![feature(platform_intrinsics)]
3613 struct i8x16(i8, i8, i8, i8, i8, i8, i8, i8,
3614 i8, i8, i8, i8, i8, i8, i8, i8);
3616 struct i32x4(i32, i32, i32, i32);
3618 struct i64x2(i64, i64);
3620 extern "platform-intrinsic" {
3621 fn x86_mm_adds_epi16(x: i8x16, y: i32x4) -> i64x2;
3622 // error: intrinsic arguments/return value have wrong type
3626 To fix this error, please refer to the function declaration to give
3627 it the awaited types. Example:
3630 #![feature(repr_simd)]
3631 #![feature(platform_intrinsics)]
3634 struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
3636 extern "platform-intrinsic" {
3637 fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i16x8; // ok!
3643 Intrinsic argument(s) and/or return value have the wrong type.
3644 Erroneous code example:
3646 ```compile_fail,E0443
3647 #![feature(repr_simd)]
3648 #![feature(platform_intrinsics)]
3651 struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
3653 struct i64x8(i64, i64, i64, i64, i64, i64, i64, i64);
3655 extern "platform-intrinsic" {
3656 fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i64x8;
3657 // error: intrinsic argument/return value has wrong type
3661 To fix this error, please refer to the function declaration to give
3662 it the awaited types. Example:
3665 #![feature(repr_simd)]
3666 #![feature(platform_intrinsics)]
3669 struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
3671 extern "platform-intrinsic" {
3672 fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i16x8; // ok!
3678 A platform-specific intrinsic function has wrong number of arguments.
3679 Erroneous code example:
3681 ```compile_fail,E0444
3682 #![feature(repr_simd)]
3683 #![feature(platform_intrinsics)]
3686 struct f64x2(f64, f64);
3688 extern "platform-intrinsic" {
3689 fn x86_mm_movemask_pd(x: f64x2, y: f64x2, z: f64x2) -> i32;
3690 // error: platform-specific intrinsic has invalid number of arguments
3694 Please refer to the function declaration to see if it corresponds
3695 with yours. Example:
3698 #![feature(repr_simd)]
3699 #![feature(platform_intrinsics)]
3702 struct f64x2(f64, f64);
3704 extern "platform-intrinsic" {
3705 fn x86_mm_movemask_pd(x: f64x2) -> i32; // ok!
3711 The `typeof` keyword is currently reserved but unimplemented.
3712 Erroneous code example:
3714 ```compile_fail,E0516
3716 let x: typeof(92) = 92;
3720 Try using type inference instead. Example:
3730 A non-default implementation was already made on this type so it cannot be
3731 specialized further. Erroneous code example:
3733 ```compile_fail,E0520
3734 #![feature(specialization)]
3741 impl<T> SpaceLlama for T {
3742 default fn fly(&self) {}
3746 // applies to all `Clone` T and overrides the previous impl
3747 impl<T: Clone> SpaceLlama for T {
3751 // since `i32` is clone, this conflicts with the previous implementation
3752 impl SpaceLlama for i32 {
3753 default fn fly(&self) {}
3754 // error: item `fly` is provided by an `impl` that specializes
3755 // another, but the item in the parent `impl` is not marked
3756 // `default` and so it cannot be specialized.
3760 Specialization only allows you to override `default` functions in
3763 To fix this error, you need to mark all the parent implementations as default.
3767 #![feature(specialization)]
3774 impl<T> SpaceLlama for T {
3775 default fn fly(&self) {} // This is a parent implementation.
3778 // applies to all `Clone` T; overrides the previous impl
3779 impl<T: Clone> SpaceLlama for T {
3780 default fn fly(&self) {} // This is a parent implementation but was
3781 // previously not a default one, causing the error
3784 // applies to i32, overrides the previous two impls
3785 impl SpaceLlama for i32 {
3786 fn fly(&self) {} // And now that's ok!
3792 The number of elements in an array or slice pattern differed from the number of
3793 elements in the array being matched.
3795 Example of erroneous code:
3797 ```compile_fail,E0527
3798 #![feature(slice_patterns)]
3800 let r = &[1, 2, 3, 4];
3802 &[a, b] => { // error: pattern requires 2 elements but array
3804 println!("a={}, b={}", a, b);
3809 Ensure that the pattern is consistent with the size of the matched
3810 array. Additional elements can be matched with `..`:
3813 #![feature(slice_patterns)]
3815 let r = &[1, 2, 3, 4];
3817 &[a, b, ..] => { // ok!
3818 println!("a={}, b={}", a, b);
3825 An array or slice pattern required more elements than were present in the
3828 Example of erroneous code:
3830 ```compile_fail,E0528
3831 #![feature(slice_patterns)]
3835 &[a, b, c, rest..] => { // error: pattern requires at least 3
3836 // elements but array has 2
3837 println!("a={}, b={}, c={} rest={:?}", a, b, c, rest);
3842 Ensure that the matched array has at least as many elements as the pattern
3843 requires. You can match an arbitrary number of remaining elements with `..`:
3846 #![feature(slice_patterns)]
3848 let r = &[1, 2, 3, 4, 5];
3850 &[a, b, c, rest..] => { // ok!
3851 // prints `a=1, b=2, c=3 rest=[4, 5]`
3852 println!("a={}, b={}, c={} rest={:?}", a, b, c, rest);
3859 An array or slice pattern was matched against some other type.
3861 Example of erroneous code:
3863 ```compile_fail,E0529
3864 #![feature(slice_patterns)]
3868 [a, b] => { // error: expected an array or slice, found `f32`
3869 println!("a={}, b={}", a, b);
3874 Ensure that the pattern and the expression being matched on are of consistent
3878 #![feature(slice_patterns)]
3883 println!("a={}, b={}", a, b);
3890 An unknown field was specified into an enum's structure variant.
3892 Erroneous code example:
3894 ```compile_fail,E0559
3899 let s = Field::Fool { joke: 0 };
3900 // error: struct variant `Field::Fool` has no field named `joke`
3903 Verify you didn't misspell the field's name or that the field exists. Example:
3910 let s = Field::Fool { joke: 0 }; // ok!
3915 An unknown field was specified into a structure.
3917 Erroneous code example:
3919 ```compile_fail,E0560
3924 let s = Simba { mother: 1, father: 0 };
3925 // error: structure `Simba` has no field named `father`
3928 Verify you didn't misspell the field's name or that the field exists. Example:
3936 let s = Simba { mother: 1, father: 0 }; // ok!
3941 The requested ABI is unsupported by the current target.
3943 The rust compiler maintains for each target a blacklist of ABIs unsupported on
3944 that target. If an ABI is present in such a list this usually means that the
3945 target / ABI combination is currently unsupported by llvm.
3947 If necessary, you can circumvent this check using custom target specifications.
3951 A return statement was found outside of a function body.
3953 Erroneous code example:
3955 ```compile_fail,E0572
3956 const FOO: u32 = return 0; // error: return statement outside of function body
3961 To fix this issue, just remove the return keyword or move the expression into a
3967 fn some_fn() -> u32 {
3978 In a `fn` type, a lifetime appears only in the return type,
3979 and not in the arguments types.
3981 Erroneous code example:
3983 ```compile_fail,E0581
3985 // Here, `'a` appears only in the return type:
3986 let x: for<'a> fn() -> &'a i32;
3990 To fix this issue, either use the lifetime in the arguments, or use
3995 // Here, `'a` appears only in the return type:
3996 let x: for<'a> fn(&'a i32) -> &'a i32;
3997 let y: fn() -> &'static i32;
4001 Note: The examples above used to be (erroneously) accepted by the
4002 compiler, but this was since corrected. See [issue #33685] for more
4005 [issue #33685]: https://github.com/rust-lang/rust/issues/33685
4009 A lifetime appears only in an associated-type binding,
4010 and not in the input types to the trait.
4012 Erroneous code example:
4014 ```compile_fail,E0582
4016 // No type can satisfy this requirement, since `'a` does not
4017 // appear in any of the input types (here, `i32`):
4018 where F: for<'a> Fn(i32) -> Option<&'a i32>
4025 To fix this issue, either use the lifetime in the inputs, or use
4029 fn bar<F, G>(t: F, u: G)
4030 where F: for<'a> Fn(&'a i32) -> Option<&'a i32>,
4031 G: Fn(i32) -> Option<&'static i32>,
4038 Note: The examples above used to be (erroneously) accepted by the
4039 compiler, but this was since corrected. See [issue #33685] for more
4042 [issue #33685]: https://github.com/rust-lang/rust/issues/33685
4046 ```compile_fail,E0599
4050 x.chocolate(); // error: no method named `chocolate` found for type `Mouth`
4051 // in the current scope
4056 An unary operator was used on a type which doesn't implement it.
4058 Example of erroneous code:
4060 ```compile_fail,E0600
4066 !Question::Yes; // error: cannot apply unary operator `!` to type `Question`
4069 In this case, `Question` would need to implement the `std::ops::Not` trait in
4070 order to be able to use `!` on it. Let's implement it:
4080 // We implement the `Not` trait on the enum.
4081 impl Not for Question {
4084 fn not(self) -> bool {
4086 Question::Yes => false, // If the `Answer` is `Yes`, then it
4088 Question::No => true, // And here we do the opposite.
4093 assert_eq!(!Question::Yes, false);
4094 assert_eq!(!Question::No, true);
4099 Attempted to access a non-existent field in a struct.
4101 Erroneous code example:
4103 ```compile_fail,E0609
4104 struct StructWithFields {
4108 let s = StructWithFields { x: 0 };
4109 println!("{}", s.foo); // error: no field `foo` on type `StructWithFields`
4112 To fix this error, check that you didn't misspell the field's name or that the
4113 field actually exists. Example:
4116 struct StructWithFields {
4120 let s = StructWithFields { x: 0 };
4121 println!("{}", s.x); // ok!
4126 Attempted to access a field on a primitive type.
4128 Erroneous code example:
4130 ```compile_fail,E0610
4132 println!("{}", x.foo); // error: `{integer}` is a primitive type, therefore
4133 // doesn't have fields
4136 Primitive types are the most basic types available in Rust and don't have
4137 fields. To access data via named fields, struct types are used. Example:
4140 // We declare struct called `Foo` containing two fields:
4146 // We create an instance of this struct:
4147 let variable = Foo { x: 0, y: -12 };
4148 // And we can now access its fields:
4149 println!("x: {}, y: {}", variable.x, variable.y);
4152 For more information see The Rust Book: https://doc.rust-lang.org/book/
4156 Attempted to pass an invalid type of variable into a variadic function.
4158 Erroneous code example:
4160 ```compile_fail,E0617
4162 fn printf(c: *const i8, ...);
4166 printf(::std::ptr::null(), 0f32);
4167 // error: can't pass an `f32` to variadic function, cast to `c_double`
4171 To fix this error, you need to pass variables corresponding to C types as much
4172 as possible. For better explanations, see The Rust Book:
4173 https://doc.rust-lang.org/book/
4178 register_diagnostics! {
4188 // E0159, // use of trait `{}` as struct constructor
4189 // E0163, // merged into E0071
4192 // E0172, // non-trait found in a type sum, moved to resolve
4193 // E0173, // manual implementations of unboxed closure traits are experimental
4196 // E0187, // can't infer the kind of the closure
4197 // E0188, // can not cast an immutable reference to a mutable pointer
4198 // E0189, // deprecated: can only cast a boxed pointer to a boxed object
4199 // E0190, // deprecated: can only cast a &-pointer to an &-object
4200 // E0196, // cannot determine a type for this closure
4201 E0203, // type parameter has more than one relaxed default bound,
4202 // and only one is supported
4204 // E0209, // builtin traits can only be implemented on structs or enums
4205 E0212, // cannot extract an associated type from a higher-ranked trait bound
4206 // E0213, // associated types are not accepted in this context
4207 // E0215, // angle-bracket notation is not stable with `Fn`
4208 // E0216, // parenthetical notation is only stable with `Fn`
4209 // E0217, // ambiguous associated type, defined in multiple supertraits
4210 // E0218, // no associated type defined
4211 // E0219, // associated type defined in higher-ranked supertrait
4212 // E0222, // Error code E0045 (variadic function must have C or cdecl calling
4213 // convention) duplicate
4214 E0224, // at least one non-builtin train is required for an object type
4215 E0227, // ambiguous lifetime bound, explicit lifetime bound required
4216 E0228, // explicit lifetime bound required
4217 E0231, // only named substitution parameters are allowed
4220 // E0235, // structure constructor specifies a structure of type but
4221 // E0236, // no lang item for range syntax
4222 // E0237, // no lang item for range syntax
4223 // E0238, // parenthesized parameters may only be used with a trait
4224 // E0239, // `next` method of `Iterator` trait has unexpected type
4228 E0245, // not a trait
4229 // E0246, // invalid recursive type
4231 // E0248, // value used as a type, now reported earlier during resolution as E0412
4233 // E0319, // trait impls for defaulted traits allowed just for structs/enums
4234 // E0372, // coherence not object safe
4235 E0377, // the trait `CoerceUnsized` may only be implemented for a coercion
4236 // between structures with the same definition
4237 E0436, // functional record update requires a struct
4238 E0521, // redundant default implementations of trait
4239 E0533, // `{}` does not name a unit variant, unit struct or a constant
4240 E0562, // `impl Trait` not allowed outside of function
4241 // and inherent method return types
4242 E0563, // cannot determine a type for this `impl Trait`: {}
4243 E0564, // only named lifetimes are allowed in `impl Trait`,
4244 // but `{}` was found in the type `{}`
4245 E0567, // auto traits can not have type parameters
4246 E0568, // auto-traits can not have predicates,
4247 E0588, // packed struct cannot transitively contain a `[repr(align)]` struct
4248 E0592, // duplicate definitions with name `{}`