1 // Error messages for EXXXX errors.
2 // Each message should start and end with a new line, and be wrapped to 80 characters.
3 // In vim you can `:set tw=80` and use `gq` to wrap paragraphs. Use `:set tw=0` to disable.
4 register_long_diagnostics! {
6 Trait objects like `Box<Trait>` can only be constructed when certain
7 requirements are satisfied by the trait in question.
9 Trait objects are a form of dynamic dispatch and use a dynamically sized type
10 for the inner type. So, for a given trait `Trait`, when `Trait` is treated as a
11 type, as in `Box<Trait>`, the inner type is 'unsized'. In such cases the boxed
12 pointer is a 'fat pointer' that contains an extra pointer to a table of methods
13 (among other things) for dynamic dispatch. This design mandates some
14 restrictions on the types of traits that are allowed to be used in trait
15 objects, which are collectively termed as 'object safety' rules.
17 Attempting to create a trait object for a non object-safe trait will trigger
20 There are various rules:
22 ### The trait cannot require `Self: Sized`
24 When `Trait` is treated as a type, the type does not implement the special
25 `Sized` trait, because the type does not have a known size at compile time and
26 can only be accessed behind a pointer. Thus, if we have a trait like the
30 trait Foo where Self: Sized {
35 We cannot create an object of type `Box<Foo>` or `&Foo` since in this case
36 `Self` would not be `Sized`.
38 Generally, `Self: Sized` is used to indicate that the trait should not be used
39 as a trait object. If the trait comes from your own crate, consider removing
42 ### Method references the `Self` type in its arguments or return type
44 This happens when a trait has a method like the following:
48 fn foo(&self) -> Self;
51 impl Trait for String {
52 fn foo(&self) -> Self {
58 fn foo(&self) -> Self {
64 (Note that `&self` and `&mut self` are okay, it's additional `Self` types which
67 In such a case, the compiler cannot predict the return type of `foo()` in a
68 situation like the following:
72 fn foo(&self) -> Self;
75 fn call_foo(x: Box<Trait>) {
76 let y = x.foo(); // What type is y?
81 If only some methods aren't object-safe, you can add a `where Self: Sized` bound
82 on them to mark them as explicitly unavailable to trait objects. The
83 functionality will still be available to all other implementers, including
84 `Box<Trait>` which is itself sized (assuming you `impl Trait for Box<Trait>`).
88 fn foo(&self) -> Self where Self: Sized;
93 Now, `foo()` can no longer be called on a trait object, but you will now be
94 allowed to make a trait object, and that will be able to call any object-safe
95 methods. With such a bound, one can still call `foo()` on types implementing
96 that trait that aren't behind trait objects.
98 ### Method has generic type parameters
100 As mentioned before, trait objects contain pointers to method tables. So, if we
108 impl Trait for String {
122 At compile time each implementation of `Trait` will produce a table containing
123 the various methods (and other items) related to the implementation.
125 This works fine, but when the method gains generic parameters, we can have a
128 Usually, generic parameters get _monomorphized_. For example, if I have
136 The machine code for `foo::<u8>()`, `foo::<bool>()`, `foo::<String>()`, or any
137 other type substitution is different. Hence the compiler generates the
138 implementation on-demand. If you call `foo()` with a `bool` parameter, the
139 compiler will only generate code for `foo::<bool>()`. When we have additional
140 type parameters, the number of monomorphized implementations the compiler
141 generates does not grow drastically, since the compiler will only generate an
142 implementation if the function is called with unparametrized substitutions
143 (i.e., substitutions where none of the substituted types are themselves
146 However, with trait objects we have to make a table containing _every_ object
147 that implements the trait. Now, if it has type parameters, we need to add
148 implementations for every type that implements the trait, and there could
149 theoretically be an infinite number of types.
155 fn foo<T>(&self, on: T);
159 impl Trait for String {
160 fn foo<T>(&self, on: T) {
166 fn foo<T>(&self, on: T) {
171 // 8 more implementations
174 Now, if we have the following code:
176 ```compile_fail,E0038
177 # trait Trait { fn foo<T>(&self, on: T); }
178 # impl Trait for String { fn foo<T>(&self, on: T) {} }
179 # impl Trait for u8 { fn foo<T>(&self, on: T) {} }
180 # impl Trait for bool { fn foo<T>(&self, on: T) {} }
182 fn call_foo(thing: Box<Trait>) {
183 thing.foo(true); // this could be any one of the 8 types above
189 We don't just need to create a table of all implementations of all methods of
190 `Trait`, we need to create such a table, for each different type fed to
191 `foo()`. In this case this turns out to be (10 types implementing `Trait`)*(3
192 types being fed to `foo()`) = 30 implementations!
194 With real world traits these numbers can grow drastically.
196 To fix this, it is suggested to use a `where Self: Sized` bound similar to the
197 fix for the sub-error above if you do not intend to call the method with type
202 fn foo<T>(&self, on: T) where Self: Sized;
207 If this is not an option, consider replacing the type parameter with another
208 trait object (e.g., if `T: OtherTrait`, use `on: Box<OtherTrait>`). If the
209 number of types you intend to feed to this method is limited, consider manually
210 listing out the methods of different types.
212 ### Method has no receiver
214 Methods that do not take a `self` parameter can't be called since there won't be
215 a way to get a pointer to the method table for them.
223 This could be called as `<Foo as Foo>::foo()`, which would not be able to pick
226 Adding a `Self: Sized` bound to these methods will generally make this compile.
230 fn foo() -> u8 where Self: Sized;
234 ### The trait cannot contain associated constants
236 Just like static functions, associated constants aren't stored on the method
237 table. If the trait or any subtrait contain an associated constant, they cannot
238 be made into an object.
240 ```compile_fail,E0038
248 A simple workaround is to use a helper method instead:
256 ### The trait cannot use `Self` as a type parameter in the supertrait listing
258 This is similar to the second sub-error, but subtler. It happens in situations
264 trait Trait: Super<Self> {
269 impl Super<Foo> for Foo{}
271 impl Trait for Foo {}
274 Here, the supertrait might have methods as follows:
278 fn get_a(&self) -> A; // note that this is object safe!
282 If the trait `Foo` was deriving from something like `Super<String>` or
283 `Super<T>` (where `Foo` itself is `Foo<T>`), this is okay, because given a type
284 `get_a()` will definitely return an object of that type.
286 However, if it derives from `Super<Self>`, even though `Super` is object safe,
287 the method `get_a()` would return an object of unknown type when called on the
288 function. `Self` type parameters let us make object safe traits no longer safe,
289 so they are forbidden when specifying supertraits.
291 There's no easy fix for this, generally code will need to be refactored so that
292 you no longer need to derive from `Super<Self>`.
296 When defining a recursive struct or enum, any use of the type being defined
297 from inside the definition must occur behind a pointer (like `Box` or `&`).
298 This is because structs and enums must have a well-defined size, and without
299 the pointer, the size of the type would need to be unbounded.
301 Consider the following erroneous definition of a type for a list of bytes:
303 ```compile_fail,E0072
304 // error, invalid recursive struct type
307 tail: Option<ListNode>,
311 This type cannot have a well-defined size, because it needs to be arbitrarily
312 large (since we would be able to nest `ListNode`s to any depth). Specifically,
315 size of `ListNode` = 1 byte for `head`
316 + 1 byte for the discriminant of the `Option`
320 One way to fix this is by wrapping `ListNode` in a `Box`, like so:
325 tail: Option<Box<ListNode>>,
329 This works because `Box` is a pointer, so its size is well-known.
333 This error indicates that the compiler was unable to sensibly evaluate an
334 constant expression that had to be evaluated. Attempting to divide by 0
335 or causing integer overflow are two ways to induce this error. For example:
337 ```compile_fail,E0080
344 Ensure that the expressions given can be evaluated as the desired integer type.
345 See the FFI section of the Reference for more information about using a custom
348 https://doc.rust-lang.org/reference.html#ffi-attributes
352 This error indicates that a lifetime is missing from a type. If it is an error
353 inside a function signature, the problem may be with failing to adhere to the
354 lifetime elision rules (see below).
356 Here are some simple examples of where you'll run into this error:
358 ```compile_fail,E0106
359 struct Foo1 { x: &bool }
360 // ^ expected lifetime parameter
361 struct Foo2<'a> { x: &'a bool } // correct
363 struct Bar1 { x: Foo2 }
364 // ^^^^ expected lifetime parameter
365 struct Bar2<'a> { x: Foo2<'a> } // correct
367 enum Baz1 { A(u8), B(&bool), }
368 // ^ expected lifetime parameter
369 enum Baz2<'a> { A(u8), B(&'a bool), } // correct
372 // ^ expected lifetime parameter
373 type MyStr2<'a> = &'a str; // correct
376 Lifetime elision is a special, limited kind of inference for lifetimes in
377 function signatures which allows you to leave out lifetimes in certain cases.
378 For more background on lifetime elision see [the book][book-le].
380 The lifetime elision rules require that any function signature with an elided
381 output lifetime must either have
383 - exactly one input lifetime
384 - or, multiple input lifetimes, but the function must also be a method with a
385 `&self` or `&mut self` receiver
387 In the first case, the output lifetime is inferred to be the same as the unique
388 input lifetime. In the second case, the lifetime is instead inferred to be the
389 same as the lifetime on `&self` or `&mut self`.
391 Here are some examples of elision errors:
393 ```compile_fail,E0106
394 // error, no input lifetimes
397 // error, `x` and `y` have distinct lifetimes inferred
398 fn bar(x: &str, y: &str) -> &str { }
400 // error, `y`'s lifetime is inferred to be distinct from `x`'s
401 fn baz<'a>(x: &'a str, y: &str) -> &str { }
404 [book-le]: https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#lifetime-elision
408 There are conflicting trait implementations for the same type.
409 Example of erroneous code:
411 ```compile_fail,E0119
413 fn get(&self) -> usize;
416 impl<T> MyTrait for T {
417 fn get(&self) -> usize { 0 }
424 impl MyTrait for Foo { // error: conflicting implementations of trait
425 // `MyTrait` for type `Foo`
426 fn get(&self) -> usize { self.value }
430 When looking for the implementation for the trait, the compiler finds
431 both the `impl<T> MyTrait for T` where T is all types and the `impl
432 MyTrait for Foo`. Since a trait cannot be implemented multiple times,
433 this is an error. So, when you write:
437 fn get(&self) -> usize;
440 impl<T> MyTrait for T {
441 fn get(&self) -> usize { 0 }
445 This makes the trait implemented on all types in the scope. So if you
446 try to implement it on another one after that, the implementations will
451 fn get(&self) -> usize;
454 impl<T> MyTrait for T {
455 fn get(&self) -> usize { 0 }
463 f.get(); // the trait is implemented so we can use it
468 // This shouldn't really ever trigger since the repeated value error comes first
470 A binary can only have one entry point, and by default that entry point is the
471 function `main()`. If there are multiple such functions, please rename one.
475 More than one function was declared with the `#[main]` attribute.
477 Erroneous code example:
479 ```compile_fail,E0137
486 fn f() {} // error: multiple functions with a `#[main]` attribute
489 This error indicates that the compiler found multiple functions with the
490 `#[main]` attribute. This is an error because there must be a unique entry
491 point into a Rust program. Example:
502 More than one function was declared with the `#[start]` attribute.
504 Erroneous code example:
506 ```compile_fail,E0138
510 fn foo(argc: isize, argv: *const *const u8) -> isize {}
513 fn f(argc: isize, argv: *const *const u8) -> isize {}
514 // error: multiple 'start' functions
517 This error indicates that the compiler found multiple functions with the
518 `#[start]` attribute. This is an error because there must be a unique entry
519 point into a Rust program. Example:
525 fn foo(argc: isize, argv: *const *const u8) -> isize { 0 } // ok!
530 #### Note: this error code is no longer emitted by the compiler.
532 There are various restrictions on transmuting between types in Rust; for example
533 types being transmuted must have the same size. To apply all these restrictions,
534 the compiler must know the exact types that may be transmuted. When type
535 parameters are involved, this cannot always be done.
537 So, for example, the following is not allowed:
540 use std::mem::transmute;
542 struct Foo<T>(Vec<T>);
544 fn foo<T>(x: Vec<T>) {
545 // we are transmuting between Vec<T> and Foo<F> here
546 let y: Foo<T> = unsafe { transmute(x) };
547 // do something with y
551 In this specific case there's a good chance that the transmute is harmless (but
552 this is not guaranteed by Rust). However, when alignment and enum optimizations
553 come into the picture, it's quite likely that the sizes may or may not match
554 with different type parameter substitutions. It's not possible to check this for
555 _all_ possible types, so `transmute()` simply only accepts types without any
556 unsubstituted type parameters.
558 If you need this, there's a good chance you're doing something wrong. Keep in
559 mind that Rust doesn't guarantee much about the layout of different structs
560 (even two structs with identical declarations may have different layouts). If
561 there is a solution that avoids the transmute entirely, try it instead.
563 If it's possible, hand-monomorphize the code by writing the function for each
564 possible type substitution. It's possible to use traits to do this cleanly,
568 use std::mem::transmute;
570 struct Foo<T>(Vec<T>);
572 trait MyTransmutableType: Sized {
573 fn transmute(_: Vec<Self>) -> Foo<Self>;
576 impl MyTransmutableType for u8 {
577 fn transmute(x: Vec<u8>) -> Foo<u8> {
578 unsafe { transmute(x) }
582 impl MyTransmutableType for String {
583 fn transmute(x: Vec<String>) -> Foo<String> {
584 unsafe { transmute(x) }
588 // ... more impls for the types you intend to transmute
590 fn foo<T: MyTransmutableType>(x: Vec<T>) {
591 let y: Foo<T> = <T as MyTransmutableType>::transmute(x);
592 // do something with y
596 Each impl will be checked for a size match in the transmute as usual, and since
597 there are no unbound type parameters involved, this should compile unless there
598 is a size mismatch in one of the impls.
600 It is also possible to manually transmute:
604 # let v = Some("value");
605 # type SomeType = &'static [u8];
607 ptr::read(&v as *const _ as *const SomeType) // `v` transmuted to `SomeType`
612 Note that this does not move `v` (unlike `transmute`), and may need a
613 call to `mem::forget(v)` in case you want to avoid destructors being called.
617 A lang item was redefined.
619 Erroneous code example:
621 ```compile_fail,E0152
622 #![feature(lang_items)]
625 struct Foo; // error: duplicate lang item found: `arc`
628 Lang items are already implemented in the standard library. Unless you are
629 writing a free-standing application (e.g., a kernel), you do not need to provide
632 You can build a free-standing crate by adding `#![no_std]` to the crate
635 ```ignore (only-for-syntax-highlight)
639 See also the [unstable book][1].
641 [1]: https://doc.rust-lang.org/unstable-book/language-features/lang-items.html#writing-an-executable-without-stdlib
645 A generic type was described using parentheses rather than angle brackets.
648 ```compile_fail,E0214
650 let v: Vec(&str) = vec!["foo"];
654 This is not currently supported: `v` should be defined as `Vec<&str>`.
655 Parentheses are currently only used with generic types when defining parameters
656 for `Fn`-family traits.
660 The `#[rustc_on_unimplemented]` attribute lets you specify a custom error
661 message for when a particular trait isn't implemented on a type placed in a
662 position that needs that trait. For example, when the following code is
666 #![feature(on_unimplemented)]
668 fn foo<T: Index<u8>>(x: T){}
670 #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
671 trait Index<Idx> { /* ... */ }
673 foo(true); // `bool` does not implement `Index<u8>`
676 There will be an error about `bool` not implementing `Index<u8>`, followed by a
677 note saying "the type `bool` cannot be indexed by `u8`".
679 As you can see, you can specify type parameters in curly braces for
680 substitution with the actual types (using the regular format string syntax) in
681 a given situation. Furthermore, `{Self}` will substitute to the type (in this
682 case, `bool`) that we tried to use.
684 This error appears when the curly braces contain an identifier which doesn't
685 match with any of the type parameters or the string `Self`. This might happen
686 if you misspelled a type parameter, or if you intended to use literal curly
687 braces. If it is the latter, escape the curly braces with a second curly brace
688 of the same type; e.g., a literal `{` is `{{`.
692 The `#[rustc_on_unimplemented]` attribute lets you specify a custom error
693 message for when a particular trait isn't implemented on a type placed in a
694 position that needs that trait. For example, when the following code is
698 #![feature(on_unimplemented)]
700 fn foo<T: Index<u8>>(x: T){}
702 #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
703 trait Index<Idx> { /* ... */ }
705 foo(true); // `bool` does not implement `Index<u8>`
708 there will be an error about `bool` not implementing `Index<u8>`, followed by a
709 note saying "the type `bool` cannot be indexed by `u8`".
711 As you can see, you can specify type parameters in curly braces for
712 substitution with the actual types (using the regular format string syntax) in
713 a given situation. Furthermore, `{Self}` will substitute to the type (in this
714 case, `bool`) that we tried to use.
716 This error appears when the curly braces do not contain an identifier. Please
717 add one of the same name as a type parameter. If you intended to use literal
718 braces, use `{{` and `}}` to escape them.
722 The `#[rustc_on_unimplemented]` attribute lets you specify a custom error
723 message for when a particular trait isn't implemented on a type placed in a
724 position that needs that trait. For example, when the following code is
728 #![feature(on_unimplemented)]
730 fn foo<T: Index<u8>>(x: T){}
732 #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
733 trait Index<Idx> { /* ... */ }
735 foo(true); // `bool` does not implement `Index<u8>`
738 there will be an error about `bool` not implementing `Index<u8>`, followed by a
739 note saying "the type `bool` cannot be indexed by `u8`".
741 For this to work, some note must be specified. An empty attribute will not do
742 anything, please remove the attribute or add some helpful note for users of the
747 When using a lifetime like `'a` in a type, it must be declared before being
750 These two examples illustrate the problem:
752 ```compile_fail,E0261
753 // error, use of undeclared lifetime name `'a`
754 fn foo(x: &'a str) { }
757 // error, use of undeclared lifetime name `'a`
762 These can be fixed by declaring lifetime parameters:
769 fn foo<'a>(x: &'a str) {}
772 Impl blocks declare lifetime parameters separately. You need to add lifetime
773 parameters to an impl block if you're implementing a type that has a lifetime
774 parameter of its own.
777 ```compile_fail,E0261
782 // error, use of undeclared lifetime name `'a`
784 fn foo<'a>(x: &'a str) {}
788 This is fixed by declaring the impl block like this:
797 fn foo(x: &'a str) {}
803 Declaring certain lifetime names in parameters is disallowed. For example,
804 because the `'static` lifetime is a special built-in lifetime name denoting
805 the lifetime of the entire program, this is an error:
807 ```compile_fail,E0262
808 // error, invalid lifetime parameter name `'static`
809 fn foo<'static>(x: &'static str) { }
814 A lifetime name cannot be declared more than once in the same scope. For
817 ```compile_fail,E0263
818 // error, lifetime name `'a` declared twice in the same scope
819 fn foo<'a, 'b, 'a>(x: &'a str, y: &'b str) { }
824 An unknown external lang item was used. Erroneous code example:
826 ```compile_fail,E0264
827 #![feature(lang_items)]
830 #[lang = "cake"] // error: unknown external lang item: `cake`
835 A list of available external lang items is available in
836 `src/librustc/middle/weak_lang_items.rs`. Example:
839 #![feature(lang_items)]
842 #[lang = "panic_impl"] // ok!
849 This is because of a type mismatch between the associated type of some
850 trait (e.g., `T::Bar`, where `T` implements `trait Quux { type Bar; }`)
851 and another type `U` that is required to be equal to `T::Bar`, but is not.
854 Here is a basic example:
856 ```compile_fail,E0271
857 trait Trait { type AssociatedType; }
859 fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
863 impl Trait for i8 { type AssociatedType = &'static str; }
868 Here is that same example again, with some explanatory comments:
870 ```compile_fail,E0271
871 trait Trait { type AssociatedType; }
873 fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
874 // ~~~~~~~~ ~~~~~~~~~~~~~~~~~~
876 // This says `foo` can |
877 // only be used with |
879 // implements `Trait`. |
881 // This says not only must
882 // `T` be an impl of `Trait`
883 // but also that the impl
884 // must assign the type `u32`
885 // to the associated type.
889 impl Trait for i8 { type AssociatedType = &'static str; }
890 //~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
895 // ... but it is an implementation
896 // that assigns `&'static str` to
897 // the associated type.
900 // Here, we invoke `foo` with an `i8`, which does not satisfy
901 // the constraint `<i8 as Trait>::AssociatedType=u32`, and
902 // therefore the type-checker complains with this error code.
905 To avoid those issues, you have to make the types match correctly.
906 So we can fix the previous examples like this:
910 trait Trait { type AssociatedType; }
912 fn foo<T>(t: T) where T: Trait<AssociatedType = &'static str> {
916 impl Trait for i8 { type AssociatedType = &'static str; }
921 let vs = vec![1, 2, 3, 4];
933 This error occurs when there was a recursive trait requirement that overflowed
934 before it could be evaluated. Often this means that there is unbounded
935 recursion in resolving some type bounds.
937 For example, in the following code:
939 ```compile_fail,E0275
944 impl<T> Foo for T where Bar<T>: Foo {}
947 To determine if a `T` is `Foo`, we need to check if `Bar<T>` is `Foo`. However,
948 to do this check, we need to determine that `Bar<Bar<T>>` is `Foo`. To
949 determine this, we check if `Bar<Bar<Bar<T>>>` is `Foo`, and so on. This is
950 clearly a recursive requirement that can't be resolved directly.
952 Consider changing your trait bounds so that they're less self-referential.
956 This error occurs when a bound in an implementation of a trait does not match
957 the bounds specified in the original trait. For example:
959 ```compile_fail,E0276
965 fn foo<T>(x: T) where T: Copy {}
969 Here, all types implementing `Foo` must have a method `foo<T>(x: T)` which can
970 take any type `T`. However, in the `impl` for `bool`, we have added an extra
971 bound that `T` is `Copy`, which isn't compatible with the original trait.
973 Consider removing the bound from the method or adding the bound to the original
974 method definition in the trait.
978 You tried to use a type which doesn't implement some trait in a place which
979 expected that trait. Erroneous code example:
981 ```compile_fail,E0277
982 // here we declare the Foo trait with a bar method
987 // we now declare a function which takes an object implementing the Foo trait
988 fn some_func<T: Foo>(foo: T) {
993 // we now call the method with the i32 type, which doesn't implement
995 some_func(5i32); // error: the trait bound `i32 : Foo` is not satisfied
999 In order to fix this error, verify that the type you're using does implement
1007 fn some_func<T: Foo>(foo: T) {
1008 foo.bar(); // we can now use this method since i32 implements the
1012 // we implement the trait on the i32 type
1018 some_func(5i32); // ok!
1022 Or in a generic context, an erroneous code example would look like:
1024 ```compile_fail,E0277
1025 fn some_func<T>(foo: T) {
1026 println!("{:?}", foo); // error: the trait `core::fmt::Debug` is not
1027 // implemented for the type `T`
1031 // We now call the method with the i32 type,
1032 // which *does* implement the Debug trait.
1037 Note that the error here is in the definition of the generic function: Although
1038 we only call it with a parameter that does implement `Debug`, the compiler
1039 still rejects the function: It must work with all possible input types. In
1040 order to make this example compile, we need to restrict the generic type we're
1046 // Restrict the input type to types that implement Debug.
1047 fn some_func<T: fmt::Debug>(foo: T) {
1048 println!("{:?}", foo);
1052 // Calling the method is still fine, as i32 implements Debug.
1055 // This would fail to compile now:
1056 // struct WithoutDebug;
1057 // some_func(WithoutDebug);
1061 Rust only looks at the signature of the called function, as such it must
1062 already specify all requirements that will be used for every type parameter.
1066 #### Note: this error code is no longer emitted by the compiler.
1068 You tried to supply a type which doesn't implement some trait in a location
1069 which expected that trait. This error typically occurs when working with
1070 `Fn`-based types. Erroneous code example:
1073 fn foo<F: Fn(usize)>(x: F) { }
1076 // type mismatch: ... implements the trait `core::ops::Fn<(String,)>`,
1077 // but the trait `core::ops::Fn<(usize,)>` is required
1079 foo(|y: String| { });
1083 The issue in this case is that `foo` is defined as accepting a `Fn` with one
1084 argument of type `String`, but the closure we attempted to pass to it requires
1085 one arguments of type `usize`.
1089 This error indicates that type inference did not result in one unique possible
1090 type, and extra information is required. In most cases this can be provided
1091 by adding a type annotation. Sometimes you need to specify a generic type
1094 A common example is the `collect` method on `Iterator`. It has a generic type
1095 parameter with a `FromIterator` bound, which for a `char` iterator is
1096 implemented by `Vec` and `String` among others. Consider the following snippet
1097 that reverses the characters of a string:
1099 ```compile_fail,E0282
1100 let x = "hello".chars().rev().collect();
1103 In this case, the compiler cannot infer what the type of `x` should be:
1104 `Vec<char>` and `String` are both suitable candidates. To specify which type to
1105 use, you can use a type annotation on `x`:
1108 let x: Vec<char> = "hello".chars().rev().collect();
1111 It is not necessary to annotate the full type. Once the ambiguity is resolved,
1112 the compiler can infer the rest:
1115 let x: Vec<_> = "hello".chars().rev().collect();
1118 Another way to provide the compiler with enough information, is to specify the
1119 generic type parameter:
1122 let x = "hello".chars().rev().collect::<Vec<char>>();
1125 Again, you need not specify the full type if the compiler can infer it:
1128 let x = "hello".chars().rev().collect::<Vec<_>>();
1131 Apart from a method or function with a generic type parameter, this error can
1132 occur when a type parameter of a struct or trait cannot be inferred. In that
1133 case it is not always possible to use a type annotation, because all candidates
1134 have the same return type. For instance:
1136 ```compile_fail,E0282
1147 let number = Foo::bar();
1152 This will fail because the compiler does not know which instance of `Foo` to
1153 call `bar` on. Change `Foo::bar()` to `Foo::<T>::bar()` to resolve the error.
1157 This error occurs when the compiler doesn't have enough information
1158 to unambiguously choose an implementation.
1162 ```compile_fail,E0283
1169 impl Generator for Impl {
1170 fn create() -> u32 { 1 }
1175 impl Generator for AnotherImpl {
1176 fn create() -> u32 { 2 }
1180 let cont: u32 = Generator::create();
1181 // error, impossible to choose one of Generator trait implementation
1182 // Should it be Impl or AnotherImpl, maybe something else?
1186 To resolve this error use the concrete type:
1195 impl Generator for AnotherImpl {
1196 fn create() -> u32 { 2 }
1200 let gen1 = AnotherImpl::create();
1202 // if there are multiple methods with same name (different traits)
1203 let gen2 = <AnotherImpl as Generator>::create();
1209 This error occurs when the compiler is unable to unambiguously infer the
1210 return type of a function or method which is generic on return type, such
1211 as the `collect` method for `Iterator`s.
1215 ```compile_fail,E0284
1216 fn foo() -> Result<bool, ()> {
1217 let results = [Ok(true), Ok(false), Err(())].iter().cloned();
1218 let v: Vec<bool> = results.collect()?;
1219 // Do things with v...
1224 Here we have an iterator `results` over `Result<bool, ()>`.
1225 Hence, `results.collect()` can return any type implementing
1226 `FromIterator<Result<bool, ()>>`. On the other hand, the
1227 `?` operator can accept any type implementing `Try`.
1229 The author of this code probably wants `collect()` to return a
1230 `Result<Vec<bool>, ()>`, but the compiler can't be sure
1231 that there isn't another type `T` implementing both `Try` and
1232 `FromIterator<Result<bool, ()>>` in scope such that
1233 `T::Ok == Vec<bool>`. Hence, this code is ambiguous and an error
1236 To resolve this error, use a concrete type for the intermediate expression:
1239 fn foo() -> Result<bool, ()> {
1240 let results = [Ok(true), Ok(false), Err(())].iter().cloned();
1242 let temp: Result<Vec<bool>, ()> = results.collect();
1245 // Do things with v...
1250 Note that the type of `v` can now be inferred from the type of `temp`.
1254 This error occurs when the compiler was unable to infer the concrete type of a
1255 variable. It can occur for several cases, the most common of which is a
1256 mismatch in the expected type that the compiler inferred for a variable's
1257 initializing expression, and the actual type explicitly assigned to the
1262 ```compile_fail,E0308
1263 let x: i32 = "I am not a number!";
1264 // ~~~ ~~~~~~~~~~~~~~~~~~~~
1266 // | initializing expression;
1267 // | compiler infers type `&str`
1269 // type `i32` assigned to variable `x`
1274 The type definition contains some field whose type
1275 requires an outlives annotation. Outlives annotations
1276 (e.g., `T: 'a`) are used to guarantee that all the data in T is valid
1277 for at least the lifetime `'a`. This scenario most commonly
1278 arises when the type contains an associated type reference
1279 like `<T as SomeTrait<'a>>::Output`, as shown in this example:
1281 ```compile_fail,E0309
1282 // This won't compile because the applicable impl of
1283 // `SomeTrait` (below) requires that `T: 'a`, but the struct does
1284 // not have a matching where-clause.
1286 foo: <T as SomeTrait<'a>>::Output,
1289 trait SomeTrait<'a> {
1293 impl<'a, T> SomeTrait<'a> for T
1301 Here, the where clause `T: 'a` that appears on the impl is not known to be
1302 satisfied on the struct. To make this example compile, you have to add
1303 a where-clause like `T: 'a` to the struct definition:
1310 foo: <T as SomeTrait<'a>>::Output
1313 trait SomeTrait<'a> {
1317 impl<'a, T> SomeTrait<'a> for T
1327 Types in type definitions have lifetimes associated with them that represent
1328 how long the data stored within them is guaranteed to be live. This lifetime
1329 must be as long as the data needs to be alive, and missing the constraint that
1330 denotes this will cause this error.
1332 ```compile_fail,E0310
1333 // This won't compile because T is not constrained to the static lifetime
1334 // the reference needs
1340 This will compile, because it has the constraint on the type parameter:
1343 struct Foo<T: 'static> {
1350 This error occurs when an `if` expression without an `else` block is used in a
1351 context where a type other than `()` is expected, for example a `let`
1354 ```compile_fail,E0317
1357 let a = if x == 5 { 1 };
1361 An `if` expression without an `else` block has the type `()`, so this is a type
1362 error. To resolve it, add an `else` block having the same type as the `if`
1367 This error indicates that some types or traits depend on each other
1368 and therefore cannot be constructed.
1370 The following example contains a circular dependency between two traits:
1372 ```compile_fail,E0391
1373 trait FirstTrait : SecondTrait {
1377 trait SecondTrait : FirstTrait {
1384 #### Note: this error code is no longer emitted by the compiler.
1386 In Rust 1.3, the default object lifetime bounds are expected to change, as
1387 described in [RFC 1156]. You are getting a warning because the compiler
1388 thinks it is possible that this change will cause a compilation error in your
1389 code. It is possible, though unlikely, that this is a false alarm.
1391 The heart of the change is that where `&'a Box<SomeTrait>` used to default to
1392 `&'a Box<SomeTrait+'a>`, it now defaults to `&'a Box<SomeTrait+'static>` (here,
1393 `SomeTrait` is the name of some trait type). Note that the only types which are
1394 affected are references to boxes, like `&Box<SomeTrait>` or
1395 `&[Box<SomeTrait>]`. More common types like `&SomeTrait` or `Box<SomeTrait>`
1398 To silence this warning, edit your code to use an explicit bound. Most of the
1399 time, this means that you will want to change the signature of a function that
1400 you are calling. For example, if the error is reported on a call like `foo(x)`,
1401 and `foo` is defined as follows:
1404 # trait SomeTrait {}
1405 fn foo(arg: &Box<SomeTrait>) { /* ... */ }
1408 You might change it to:
1411 # trait SomeTrait {}
1412 fn foo<'a>(arg: &'a Box<SomeTrait+'a>) { /* ... */ }
1415 This explicitly states that you expect the trait object `SomeTrait` to contain
1416 references (with a maximum lifetime of `'a`).
1418 [RFC 1156]: https://github.com/rust-lang/rfcs/blob/master/text/1156-adjust-default-object-bounds.md
1422 An invalid lint attribute has been given. Erroneous code example:
1424 ```compile_fail,E0452
1425 #![allow(foo = "")] // error: malformed lint attribute
1428 Lint attributes only accept a list of identifiers (where each identifier is a
1429 lint name). Ensure the attribute is of this form:
1432 #![allow(foo)] // ok!
1434 #![allow(foo, foo2)] // ok!
1439 A lint check attribute was overruled by a `forbid` directive set as an
1440 attribute on an enclosing scope, or on the command line with the `-F` option.
1442 Example of erroneous code:
1444 ```compile_fail,E0453
1445 #![forbid(non_snake_case)]
1447 #[allow(non_snake_case)]
1449 let MyNumber = 2; // error: allow(non_snake_case) overruled by outer
1450 // forbid(non_snake_case)
1454 The `forbid` lint setting, like `deny`, turns the corresponding compiler
1455 warning into a hard error. Unlike `deny`, `forbid` prevents itself from being
1456 overridden by inner attributes.
1458 If you're sure you want to override the lint check, you can change `forbid` to
1459 `deny` (or use `-D` instead of `-F` if the `forbid` setting was given as a
1460 command-line option) to allow the inner lint check attribute:
1463 #![deny(non_snake_case)]
1465 #[allow(non_snake_case)]
1467 let MyNumber = 2; // ok!
1471 Otherwise, edit the code to pass the lint check, and remove the overruled
1475 #![forbid(non_snake_case)]
1484 A lifetime bound was not satisfied.
1486 Erroneous code example:
1488 ```compile_fail,E0478
1489 // Check that the explicit lifetime bound (`'SnowWhite`, in this example) must
1490 // outlive all the superbounds from the trait (`'kiss`, in this example).
1492 trait Wedding<'t>: 't { }
1494 struct Prince<'kiss, 'SnowWhite> {
1495 child: Box<Wedding<'kiss> + 'SnowWhite>,
1496 // error: lifetime bound not satisfied
1500 In this example, the `'SnowWhite` lifetime is supposed to outlive the `'kiss`
1501 lifetime but the declaration of the `Prince` struct doesn't enforce it. To fix
1502 this issue, you need to specify it:
1505 trait Wedding<'t>: 't { }
1507 struct Prince<'kiss, 'SnowWhite: 'kiss> { // You say here that 'kiss must live
1508 // longer than 'SnowWhite.
1509 child: Box<Wedding<'kiss> + 'SnowWhite>, // And now it's all good!
1515 A reference has a longer lifetime than the data it references.
1517 Erroneous code example:
1519 ```compile_fail,E0491
1520 trait SomeTrait<'a> {
1524 impl<'a, T> SomeTrait<'a> for T {
1525 type Output = &'a T; // compile error E0491
1529 Here, the problem is that a reference type like `&'a T` is only valid
1530 if all the data in T outlives the lifetime `'a`. But this impl as written
1531 is applicable to any lifetime `'a` and any type `T` -- we have no guarantee
1532 that `T` outlives `'a`. To fix this, you can add a where clause like
1536 trait SomeTrait<'a> {
1540 impl<'a, T> SomeTrait<'a> for T
1544 type Output = &'a T; // compile error E0491
1550 A lifetime name is shadowing another lifetime name. Erroneous code example:
1552 ```compile_fail,E0496
1558 fn f<'a>(x: &'a i32) { // error: lifetime name `'a` shadows a lifetime
1559 // name that is already in scope
1564 Please change the name of one of the lifetimes to remove this error. Example:
1572 fn f<'b>(x: &'b i32) { // ok!
1582 A stability attribute was used outside of the standard library. Erroneous code
1586 #[stable] // error: stability attributes may not be used outside of the
1591 It is not possible to use stability attributes outside of the standard library.
1592 Also, for now, it is not possible to write deprecation messages either.
1596 Transmute with two differently sized types was attempted. Erroneous code
1599 ```compile_fail,E0512
1600 fn takes_u8(_: u8) {}
1603 unsafe { takes_u8(::std::mem::transmute(0u16)); }
1604 // error: cannot transmute between types of different sizes,
1605 // or dependently-sized types
1609 Please use types with same size or use the expected type directly. Example:
1612 fn takes_u8(_: u8) {}
1615 unsafe { takes_u8(::std::mem::transmute(0i8)); } // ok!
1617 unsafe { takes_u8(0u8); } // ok!
1623 This error indicates that a `#[repr(..)]` attribute was placed on an
1626 Examples of erroneous code:
1628 ```compile_fail,E0517
1636 struct Foo {bar: bool, baz: bool}
1644 * The `#[repr(C)]` attribute can only be placed on structs and enums.
1645 * The `#[repr(packed)]` and `#[repr(simd)]` attributes only work on structs.
1646 * The `#[repr(u8)]`, `#[repr(i16)]`, etc attributes only work on enums.
1648 These attributes do not work on typedefs, since typedefs are just aliases.
1650 Representations like `#[repr(u8)]`, `#[repr(i64)]` are for selecting the
1651 discriminant size for enums with no data fields on any of the variants, e.g.
1652 `enum Color {Red, Blue, Green}`, effectively setting the size of the enum to
1653 the size of the provided type. Such an enum can be cast to a value of the same
1654 type as well. In short, `#[repr(u8)]` makes the enum behave like an integer
1655 with a constrained set of allowed values.
1657 Only field-less enums can be cast to numerical primitives, so this attribute
1658 will not apply to structs.
1660 `#[repr(packed)]` reduces padding to make the struct size smaller. The
1661 representation of enums isn't strictly defined in Rust, and this attribute
1662 won't work on enums.
1664 `#[repr(simd)]` will give a struct consisting of a homogeneous series of machine
1665 types (i.e., `u8`, `i32`, etc) a representation that permits vectorization via
1666 SIMD. This doesn't make much sense for enums since they don't consist of a
1667 single list of data.
1671 This error indicates that an `#[inline(..)]` attribute was incorrectly placed
1672 on something other than a function or method.
1674 Examples of erroneous code:
1676 ```compile_fail,E0518
1686 `#[inline]` hints the compiler whether or not to attempt to inline a method or
1687 function. By default, the compiler does a pretty good job of figuring this out
1688 itself, but if you feel the need for annotations, `#[inline(always)]` and
1689 `#[inline(never)]` can override or force the compiler's decision.
1691 If you wish to apply this attribute to all methods in an impl, manually annotate
1692 each method; it is not possible to annotate the entire impl with an `#[inline]`
1697 The lang attribute is intended for marking special items that are built-in to
1698 Rust itself. This includes special traits (like `Copy` and `Sized`) that affect
1699 how the compiler behaves, as well as special functions that may be automatically
1700 invoked (such as the handler for out-of-bounds accesses when indexing a slice).
1701 Erroneous code example:
1703 ```compile_fail,E0522
1704 #![feature(lang_items)]
1707 fn cookie() -> ! { // error: definition of an unknown language item: `cookie`
1714 A closure was used but didn't implement the expected trait.
1716 Erroneous code example:
1718 ```compile_fail,E0525
1722 fn bar<T: Fn(u32)>(_: T) {}
1726 let closure = |_| foo(x); // error: expected a closure that implements
1727 // the `Fn` trait, but this closure only
1728 // implements `FnOnce`
1733 In the example above, `closure` is an `FnOnce` closure whereas the `bar`
1734 function expected an `Fn` closure. In this case, it's simple to fix the issue,
1735 you just have to implement `Copy` and `Clone` traits on `struct X` and it'll
1739 #[derive(Clone, Copy)] // We implement `Clone` and `Copy` traits.
1743 fn bar<T: Fn(u32)>(_: T) {}
1747 let closure = |_| foo(x);
1748 bar(closure); // ok!
1752 To understand better how closures work in Rust, read:
1753 https://doc.rust-lang.org/book/ch13-01-closures.html
1757 The `main` function was incorrectly declared.
1759 Erroneous code example:
1761 ```compile_fail,E0580
1762 fn main(x: i32) { // error: main function has wrong type
1767 The `main` function prototype should never take arguments.
1776 If you want to get command-line arguments, use `std::env::args`. To exit with a
1777 specified exit code, use `std::process::exit`.
1781 Abstract return types (written `impl Trait` for some trait `Trait`) are only
1782 allowed as function and inherent impl return types.
1784 Erroneous code example:
1786 ```compile_fail,E0562
1788 let count_to_ten: impl Iterator<Item=usize> = 0..10;
1789 // error: `impl Trait` not allowed outside of function and inherent method
1791 for i in count_to_ten {
1797 Make sure `impl Trait` only appears in return-type position.
1800 fn count_to_n(n: usize) -> impl Iterator<Item=usize> {
1805 for i in count_to_n(10) { // ok!
1811 See [RFC 1522] for more details.
1813 [RFC 1522]: https://github.com/rust-lang/rfcs/blob/master/text/1522-conservative-impl-trait.md
1817 Per [RFC 401][rfc401], if you have a function declaration `foo`:
1820 // For the purposes of this explanation, all of these
1821 // different kinds of `fn` declarations are equivalent:
1823 fn foo(x: S) { /* ... */ }
1824 # #[cfg(for_demonstration_only)]
1825 extern "C" { fn foo(x: S); }
1826 # #[cfg(for_demonstration_only)]
1827 impl S { fn foo(self) { /* ... */ } }
1830 the type of `foo` is **not** `fn(S)`, as one might expect.
1831 Rather, it is a unique, zero-sized marker type written here as `typeof(foo)`.
1832 However, `typeof(foo)` can be _coerced_ to a function pointer `fn(S)`,
1833 so you rarely notice this:
1838 let x: fn(S) = foo; // OK, coerces
1841 The reason that this matter is that the type `fn(S)` is not specific to
1842 any particular function: it's a function _pointer_. So calling `x()` results
1843 in a virtual call, whereas `foo()` is statically dispatched, because the type
1844 of `foo` tells us precisely what function is being called.
1846 As noted above, coercions mean that most code doesn't have to be
1847 concerned with this distinction. However, you can tell the difference
1848 when using **transmute** to convert a fn item into a fn pointer.
1850 This is sometimes done as part of an FFI:
1852 ```compile_fail,E0591
1853 extern "C" fn foo(userdata: Box<i32>) {
1857 # fn callback(_: extern "C" fn(*mut i32)) {}
1858 # use std::mem::transmute;
1860 let f: extern "C" fn(*mut i32) = transmute(foo);
1865 Here, transmute is being used to convert the types of the fn arguments.
1866 This pattern is incorrect because, because the type of `foo` is a function
1867 **item** (`typeof(foo)`), which is zero-sized, and the target type (`fn()`)
1868 is a function pointer, which is not zero-sized.
1869 This pattern should be rewritten. There are a few possible ways to do this:
1871 - change the original fn declaration to match the expected signature,
1872 and do the cast in the fn body (the preferred option)
1873 - cast the fn item fo a fn pointer before calling transmute, as shown here:
1876 # extern "C" fn foo(_: Box<i32>) {}
1877 # use std::mem::transmute;
1879 let f: extern "C" fn(*mut i32) = transmute(foo as extern "C" fn(_));
1880 let f: extern "C" fn(*mut i32) = transmute(foo as usize); // works too
1884 The same applies to transmutes to `*mut fn()`, which were observed in practice.
1885 Note though that use of this type is generally incorrect.
1886 The intention is typically to describe a function pointer, but just `fn()`
1887 alone suffices for that. `*mut fn()` is a pointer to a fn pointer.
1888 (Since these values are typically just passed to C code, however, this rarely
1889 makes a difference in practice.)
1891 [rfc401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
1895 You tried to supply an `Fn`-based type with an incorrect number of arguments
1896 than what was expected.
1898 Erroneous code example:
1900 ```compile_fail,E0593
1901 fn foo<F: Fn()>(x: F) { }
1904 // [E0593] closure takes 1 argument but 0 arguments are required
1911 No `main` function was found in a binary crate. To fix this error, add a
1912 `main` function. For example:
1916 // Your program will start here.
1917 println!("Hello world!");
1921 If you don't know the basics of Rust, you can go look to the Rust Book to get
1922 started: https://doc.rust-lang.org/book/
1926 An unknown lint was used on the command line.
1931 rustc -D bogus omse_file.rs
1934 Maybe you just misspelled the lint name or the lint doesn't exist anymore.
1935 Either way, try to update/remove it in order to fix the error.
1939 This error code indicates a mismatch between the lifetimes appearing in the
1940 function signature (i.e., the parameter types and the return type) and the
1941 data-flow found in the function body.
1943 Erroneous code example:
1945 ```compile_fail,E0621
1946 fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 { // error: explicit lifetime
1947 // required in the type of
1949 if x > y { x } else { y }
1953 In the code above, the function is returning data borrowed from either `x` or
1954 `y`, but the `'a` annotation indicates that it is returning data only from `x`.
1955 To fix the error, the signature and the body must be made to match. Typically,
1956 this is done by updating the function signature. So, in this case, we change
1957 the type of `y` to `&'a i32`, like so:
1960 fn foo<'a>(x: &'a i32, y: &'a i32) -> &'a i32 {
1961 if x > y { x } else { y }
1965 Now the signature indicates that the function data borrowed from either `x` or
1966 `y`. Alternatively, you could change the body to not return data from `y`:
1969 fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 {
1976 The `#![feature]` attribute specified an unknown feature.
1978 Erroneous code example:
1980 ```compile_fail,E0635
1981 #![feature(nonexistent_rust_feature)] // error: unknown feature
1987 A `#![feature]` attribute was declared multiple times.
1989 Erroneous code example:
1991 ```compile_fail,E0636
1992 #![allow(stable_features)]
1994 #![feature(rust1)] // error: the feature `rust1` has already been declared
2000 A closure or generator was constructed that references its own type.
2004 ```compile-fail,E0644
2013 // Here, when `x` is called, the parameter `y` is equal to `x`.
2018 Rust does not permit a closure to directly reference its own type,
2019 either through an argument (as in the example above) or by capturing
2020 itself through its environment. This restriction helps keep closure
2021 inference tractable.
2023 The easiest fix is to rewrite your closure into a top-level function,
2024 or into a method. In some cases, you may also be able to have your
2025 closure call itself by capturing a `&Fn()` object or `fn()` pointer
2026 that refers to itself. That is permitting, since the closure would be
2027 invoking itself via a virtual call, and hence does not directly
2028 reference its own *type*.
2033 A `repr(transparent)` type was also annotated with other, incompatible
2034 representation hints.
2036 Erroneous code example:
2038 ```compile_fail,E0692
2039 #[repr(transparent, C)] // error: incompatible representation hints
2043 A type annotated as `repr(transparent)` delegates all representation concerns to
2044 another type, so adding more representation hints is contradictory. Remove
2045 either the `transparent` hint or the other hints, like this:
2048 #[repr(transparent)]
2052 Alternatively, move the other attributes to the contained type:
2061 #[repr(transparent)]
2062 struct FooWrapper(Foo);
2065 Note that introducing another `struct` just to have a place for the other
2066 attributes may have unintended side effects on the representation:
2069 #[repr(transparent)]
2075 #[repr(transparent)]
2076 struct Grams2(Float); // this is not equivalent to `Grams` above
2079 Here, `Grams2` is a not equivalent to `Grams` -- the former transparently wraps
2080 a (non-transparent) struct containing a single float, while `Grams` is a
2081 transparent wrapper around a float. This can make a difference for the ABI.
2085 When using generators (or async) all type variables must be bound so a
2086 generator can be constructed.
2088 Erroneous code example:
2090 ```edition2018,compile-fail,E0698
2091 #![feature(async_await)]
2092 async fn bar<T>() -> () {}
2095 bar().await; // error: cannot infer type for `T`
2099 In the above example `T` is unknowable by the compiler.
2100 To fix this you must bind `T` to a concrete type such as `String`
2101 so that a generator can then be constructed:
2104 #![feature(async_await)]
2105 async fn bar<T>() -> () {}
2108 bar::<String>().await;
2109 // ^^^^^^^^ specify type explicitly
2115 The `impl Trait` return type captures lifetime parameters that do not
2116 appear within the `impl Trait` itself.
2118 Erroneous code example:
2120 ```compile-fail,E0700
2121 use std::cell::Cell;
2125 impl<'a, 'b> Trait<'b> for Cell<&'a u32> { }
2127 fn foo<'x, 'y>(x: Cell<&'x u32>) -> impl Trait<'y>
2134 Here, the function `foo` returns a value of type `Cell<&'x u32>`,
2135 which references the lifetime `'x`. However, the return type is
2136 declared as `impl Trait<'y>` -- this indicates that `foo` returns
2137 "some type that implements `Trait<'y>`", but it also indicates that
2138 the return type **only captures data referencing the lifetime `'y`**.
2139 In this case, though, we are referencing data with lifetime `'x`, so
2140 this function is in error.
2142 To fix this, you must reference the lifetime `'x` from the return
2143 type. For example, changing the return type to `impl Trait<'y> + 'x`
2147 use std::cell::Cell;
2151 impl<'a,'b> Trait<'b> for Cell<&'a u32> { }
2153 fn foo<'x, 'y>(x: Cell<&'x u32>) -> impl Trait<'y> + 'x
2162 This error indicates that a `#[non_exhaustive]` attribute was incorrectly placed
2163 on something other than a struct or enum.
2165 Examples of erroneous code:
2167 ```compile_fail,E0701
2168 # #![feature(non_exhaustive)]
2176 This error indicates that a `#[lang = ".."]` attribute was placed
2177 on the wrong type of item.
2179 Examples of erroneous code:
2181 ```compile_fail,E0718
2182 #![feature(lang_items)]
2192 register_diagnostics! {
2193 // E0006, // merged with E0005
2194 // E0101, // replaced with E0282
2195 // E0102, // replaced with E0282
2198 // E0272, // on_unimplemented #0
2199 // E0273, // on_unimplemented #1
2200 // E0274, // on_unimplemented #2
2201 E0278, // requirement is not satisfied
2202 E0279, // requirement is not satisfied
2203 E0280, // requirement is not satisfied
2204 // E0285, // overflow evaluation builtin bounds
2205 // E0296, // replaced with a generic attribute input check
2206 // E0300, // unexpanded macro
2207 // E0304, // expected signed integer constant
2208 // E0305, // expected constant
2209 E0311, // thing may not live long enough
2210 E0312, // lifetime of reference outlives lifetime of borrowed content
2211 E0313, // lifetime of borrowed pointer outlives lifetime of captured variable
2212 E0314, // closure outlives stack frame
2213 E0315, // cannot invoke closure outside of its lifetime
2214 E0316, // nested quantification of lifetimes
2215 E0320, // recursive overflow during dropck
2216 E0473, // dereference of reference outside its lifetime
2217 E0474, // captured variable `..` does not outlive the enclosing closure
2218 E0475, // index of slice outside its lifetime
2219 E0476, // lifetime of the source pointer does not outlive lifetime bound...
2220 E0477, // the type `..` does not fulfill the required lifetime...
2221 E0479, // the type `..` (provided as the value of a type parameter) is...
2222 E0480, // lifetime of method receiver does not outlive the method call
2223 E0481, // lifetime of function argument does not outlive the function call
2224 E0482, // lifetime of return value does not outlive the function call
2225 E0483, // lifetime of operand does not outlive the operation
2226 E0484, // reference is not valid at the time of borrow
2227 E0485, // automatically reference is not valid at the time of borrow
2228 E0486, // type of expression contains references that are not valid during...
2229 E0487, // unsafe use of destructor: destructor might be called while...
2230 E0488, // lifetime of variable does not enclose its declaration
2231 E0489, // type/lifetime parameter not in scope here
2232 E0490, // a value of type `..` is borrowed for too long
2233 E0495, // cannot infer an appropriate lifetime due to conflicting requirements
2234 E0566, // conflicting representation hints
2235 E0623, // lifetime mismatch where both parameters are anonymous regions
2236 E0628, // generators cannot have explicit arguments
2237 E0631, // type mismatch in closure arguments
2238 E0637, // "'_" is not a valid lifetime bound
2239 E0657, // `impl Trait` can only capture lifetimes bound at the fn level
2240 E0687, // in-band lifetimes cannot be used in `fn`/`Fn` syntax
2241 E0688, // in-band lifetimes cannot be mixed with explicit lifetime binders
2242 E0697, // closures cannot be static
2243 E0707, // multiple elided lifetimes used in arguments of `async fn`
2244 E0708, // `async` non-`move` closures with arguments are not currently supported
2245 E0709, // multiple different lifetimes used in arguments of `async fn`
2246 E0710, // an unknown tool name found in scoped lint
2247 E0711, // a feature has been declared with conflicting stability attributes
2248 // E0702, // replaced with a generic attribute input check
2249 E0726, // non-explicit (not `'_`) elided lifetime in unsupported position
2250 E0727, // `async` generators are not yet supported
2251 E0728, // `await` must be in an `async` function or block