]> git.proxmox.com Git - rustc.git/blame - src/librustc_typeck/diagnostics.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc_typeck / diagnostics.rs
CommitLineData
1a4d82fc
JJ
1// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11#![allow(non_snake_case)]
12
d9579d0f
AL
13register_long_diagnostics! {
14
62682a34
SL
15E0023: r##"
16A pattern used to match against an enum variant must provide a sub-pattern for
17each field of the enum variant. This error indicates that a pattern attempted to
18extract an incorrect number of fields from a variant.
19
20```
21enum Fruit {
7453a54e
SL
22 Apple(String, String),
23 Pear(u32),
62682a34
SL
24}
25```
26
27Here the `Apple` variant has two fields, and should be matched against like so:
28
29```
7453a54e
SL
30enum Fruit {
31 Apple(String, String),
32 Pear(u32),
33}
34
35let x = Fruit::Apple(String::new(), String::new());
36
62682a34
SL
37// Correct.
38match x {
7453a54e
SL
39 Fruit::Apple(a, b) => {},
40 _ => {}
62682a34
SL
41}
42```
43
44Matching with the wrong number of fields has no sensible interpretation:
45
7453a54e
SL
46```compile_fail
47enum Fruit {
48 Apple(String, String),
49 Pear(u32),
50}
51
52let x = Fruit::Apple(String::new(), String::new());
53
62682a34
SL
54// Incorrect.
55match x {
7453a54e
SL
56 Apple(a) => {},
57 Apple(a, b, c) => {},
62682a34
SL
58}
59```
60
61Check how many fields the enum was declared with and ensure that your pattern
62uses the same number.
63"##,
64
65E0024: r##"
66This error indicates that a pattern attempted to extract the fields of an enum
67variant with no fields. Here's a tiny example of this error:
68
7453a54e 69```compile_fail
62682a34
SL
70// This enum has two variants.
71enum Number {
72 // This variant has no fields.
73 Zero,
74 // This variant has one field.
75 One(u32)
76}
77
78// Assuming x is a Number we can pattern match on its contents.
79match x {
7453a54e
SL
80 Zero(inside) => {},
81 One(inside) => {},
62682a34
SL
82}
83```
84
85The pattern match `Zero(inside)` is incorrect because the `Zero` variant
86contains no fields, yet the `inside` name attempts to bind the first field of
87the enum.
88"##,
89
90E0025: r##"
e9174d1e
SL
91Each field of a struct can only be bound once in a pattern. Erroneous code
92example:
93
7453a54e 94```compile_fail
e9174d1e
SL
95struct Foo {
96 a: u8,
97 b: u8,
98}
99
100fn main(){
101 let x = Foo { a:1, b:2 };
102
103 let Foo { a: x, a: y } = x;
104 // error: field `a` bound multiple times in the pattern
105}
106```
107
108Each occurrence of a field name binds the value of that field, so to fix this
109error you will have to remove or alter the duplicate uses of the field name.
110Perhaps you misspelled another field name? Example:
111
112```
113struct Foo {
114 a: u8,
115 b: u8,
116}
117
118fn main(){
119 let x = Foo { a:1, b:2 };
120
121 let Foo { a: x, b: y } = x; // ok!
122}
123```
62682a34
SL
124"##,
125
126E0026: r##"
b039eaaf 127This error indicates that a struct pattern attempted to extract a non-existent
62682a34
SL
128field from a struct. Struct fields are identified by the name used before the
129colon `:` so struct patterns should resemble the declaration of the struct type
130being matched.
131
132```
133// Correct matching.
134struct Thing {
135 x: u32,
136 y: u32
137}
138
139let thing = Thing { x: 1, y: 2 };
7453a54e 140
62682a34 141match thing {
7453a54e 142 Thing { x: xfield, y: yfield } => {}
62682a34
SL
143}
144```
145
146If you are using shorthand field patterns but want to refer to the struct field
147by a different name, you should rename it explicitly.
148
7453a54e
SL
149Change this:
150
151```compile_fail
152struct Thing {
153 x: u32,
154 y: u32
155}
156
157let thing = Thing { x: 0, y: 0 };
158
62682a34 159match thing {
7453a54e 160 Thing { x, z } => {}
62682a34 161}
7453a54e
SL
162```
163
164To this:
165
166```
167struct Thing {
168 x: u32,
169 y: u32
170}
171
172let thing = Thing { x: 0, y: 0 };
62682a34 173
62682a34 174match thing {
7453a54e 175 Thing { x, y: z } => {}
62682a34
SL
176}
177```
178"##,
179
180E0027: r##"
181This error indicates that a pattern for a struct fails to specify a sub-pattern
182for every one of the struct's fields. Ensure that each field from the struct's
183definition is mentioned in the pattern, or use `..` to ignore unwanted fields.
184
185For example:
186
7453a54e 187```compile_fail
62682a34
SL
188struct Dog {
189 name: String,
7453a54e 190 age: u32,
62682a34
SL
191}
192
193let d = Dog { name: "Rusty".to_string(), age: 8 };
194
195// This is incorrect.
196match d {
7453a54e
SL
197 Dog { age: x } => {}
198}
199```
200
201This is correct (explicit):
202
203```
204struct Dog {
205 name: String,
206 age: u32,
62682a34
SL
207}
208
7453a54e
SL
209let d = Dog { name: "Rusty".to_string(), age: 8 };
210
62682a34 211match d {
7453a54e 212 Dog { name: ref n, age: x } => {}
62682a34
SL
213}
214
215// This is also correct (ignore unused fields).
216match d {
7453a54e 217 Dog { age: x, .. } => {}
62682a34
SL
218}
219```
220"##,
221
222E0029: r##"
223In a match expression, only numbers and characters can be matched against a
224range. This is because the compiler checks that the range is non-empty at
225compile-time, and is unable to evaluate arbitrary comparison functions. If you
226want to capture values of an orderable type between two end-points, you can use
227a guard.
228
7453a54e 229```compile_fail
62682a34
SL
230// The ordering relation for strings can't be evaluated at compile time,
231// so this doesn't work:
232match string {
7453a54e
SL
233 "hello" ... "world" => {}
234 _ => {}
62682a34
SL
235}
236
237// This is a more general version, using a guard:
238match string {
7453a54e
SL
239 s if s >= "hello" && s <= "world" => {}
240 _ => {}
62682a34
SL
241}
242```
243"##,
244
62682a34
SL
245E0033: r##"
246This error indicates that a pointer to a trait type cannot be implicitly
247dereferenced by a pattern. Every trait defines a type, but because the
248size of trait implementors isn't fixed, this type has no compile-time size.
249Therefore, all accesses to trait types must be through pointers. If you
250encounter this error you should try to avoid dereferencing the pointer.
251
7453a54e 252```ignore
62682a34
SL
253let trait_obj: &SomeTrait = ...;
254
255// This tries to implicitly dereference to create an unsized local variable.
256let &invalid = trait_obj;
257
258// You can call methods without binding to the value being pointed at.
259trait_obj.method_one();
260trait_obj.method_two();
261```
262
263You can read more about trait objects in the Trait Object section of the
264Reference:
265
e9174d1e 266https://doc.rust-lang.org/reference.html#trait-objects
62682a34
SL
267"##,
268
269E0034: r##"
270The compiler doesn't know what method to call because more than one method
7453a54e 271has the same prototype. Erroneous code example:
62682a34 272
7453a54e 273```compile_fail
62682a34
SL
274struct Test;
275
276trait Trait1 {
277 fn foo();
278}
279
280trait Trait2 {
281 fn foo();
282}
283
284impl Trait1 for Test { fn foo() {} }
285impl Trait2 for Test { fn foo() {} }
286
287fn main() {
288 Test::foo() // error, which foo() to call?
289}
290```
291
292To avoid this error, you have to keep only one of them and remove the others.
293So let's take our example and fix it:
294
295```
296struct Test;
297
298trait Trait1 {
299 fn foo();
300}
301
302impl Trait1 for Test { fn foo() {} }
303
304fn main() {
305 Test::foo() // and now that's good!
306}
307```
308
309However, a better solution would be using fully explicit naming of type and
310trait:
311
312```
313struct Test;
314
315trait Trait1 {
316 fn foo();
317}
318
319trait Trait2 {
320 fn foo();
321}
322
323impl Trait1 for Test { fn foo() {} }
324impl Trait2 for Test { fn foo() {} }
325
326fn main() {
327 <Test as Trait1>::foo()
328}
329```
54a0048b
SL
330
331One last example:
332
333```
334trait F {
335 fn m(&self);
336}
337
338trait G {
339 fn m(&self);
340}
341
342struct X;
343
344impl F for X { fn m(&self) { println!("I am F"); } }
345impl G for X { fn m(&self) { println!("I am G"); } }
346
347fn main() {
348 let f = X;
349
350 F::m(&f); // it displays "I am F"
351 G::m(&f); // it displays "I am G"
352}
353```
62682a34
SL
354"##,
355
356E0035: r##"
7453a54e
SL
357You tried to give a type parameter where it wasn't needed. Erroneous code
358example:
62682a34 359
7453a54e 360```compile_fail
62682a34
SL
361struct Test;
362
363impl Test {
364 fn method(&self) {}
365}
366
367fn main() {
368 let x = Test;
369
370 x.method::<i32>(); // Error: Test::method doesn't need type parameter!
371}
372```
373
374To fix this error, just remove the type parameter:
375
376```
377struct Test;
378
379impl Test {
380 fn method(&self) {}
381}
382
383fn main() {
384 let x = Test;
385
386 x.method(); // OK, we're good!
387}
388```
389"##,
390
391E0036: r##"
392This error occurrs when you pass too many or not enough type parameters to
7453a54e 393a method. Erroneous code example:
62682a34 394
7453a54e 395```compile_fail
62682a34
SL
396struct Test;
397
398impl Test {
399 fn method<T>(&self, v: &[T]) -> usize {
400 v.len()
401 }
402}
403
404fn main() {
405 let x = Test;
54a0048b 406 let v = &[0];
62682a34
SL
407
408 x.method::<i32, i32>(v); // error: only one type parameter is expected!
409}
410```
411
412To fix it, just specify a correct number of type parameters:
413
414```
415struct Test;
416
417impl Test {
418 fn method<T>(&self, v: &[T]) -> usize {
419 v.len()
420 }
421}
422
423fn main() {
424 let x = Test;
54a0048b 425 let v = &[0];
62682a34
SL
426
427 x.method::<i32>(v); // OK, we're good!
428}
429```
430
431Please note on the last example that we could have called `method` like this:
432
7453a54e 433```ignore
62682a34
SL
434x.method(v);
435```
436"##,
437
438E0040: r##"
439It is not allowed to manually call destructors in Rust. It is also not
440necessary to do this since `drop` is called automatically whenever a value goes
441out of scope.
442
443Here's an example of this error:
444
7453a54e 445```compile_fail
62682a34
SL
446struct Foo {
447 x: i32,
448}
449
450impl Drop for Foo {
451 fn drop(&mut self) {
452 println!("kaboom");
453 }
454}
455
456fn main() {
457 let mut x = Foo { x: -7 };
458 x.drop(); // error: explicit use of destructor method
459}
460```
461"##,
462
c1a9b12d
SL
463E0044: r##"
464You can't use type parameters on foreign items. Example of erroneous code:
465
7453a54e 466```compile_fail
c1a9b12d
SL
467extern { fn some_func<T>(x: T); }
468```
469
470To fix this, replace the type parameter with the specializations that you
471need:
472
473```
474extern { fn some_func_i32(x: i32); }
475extern { fn some_func_i64(x: i64); }
476```
477"##,
478
62682a34
SL
479E0045: r##"
480Rust only supports variadic parameters for interoperability with C code in its
481FFI. As such, variadic parameters can only be used with functions which are
482using the C ABI. Examples of erroneous code:
483
7453a54e 484```compile_fail
62682a34 485extern "rust-call" { fn foo(x: u8, ...); }
7453a54e 486
62682a34 487// or
7453a54e 488
62682a34
SL
489fn foo(x: u8, ...) {}
490```
491
492To fix such code, put them in an extern "C" block:
493
7453a54e
SL
494```ignore
495extern "C" fn foo(x: u8, ...);
496```
497
498Or:
499
62682a34 500```
62682a34
SL
501extern "C" {
502 fn foo (x: u8, ...);
503}
504```
505"##,
506
d9579d0f 507E0046: r##"
e9174d1e
SL
508Items are missing in a trait implementation. Erroneous code example:
509
7453a54e 510```compile_fail
e9174d1e
SL
511trait Foo {
512 fn foo();
513}
514
515struct Bar;
516
517impl Foo for Bar {}
518// error: not all trait items implemented, missing: `foo`
519```
520
d9579d0f
AL
521When trying to make some type implement a trait `Foo`, you must, at minimum,
522provide implementations for all of `Foo`'s required methods (meaning the
523methods that do not have default implementations), as well as any required
e9174d1e
SL
524trait items like associated types or constants. Example:
525
526```
527trait Foo {
528 fn foo();
529}
530
531struct Bar;
532
533impl Foo for Bar {
534 fn foo() {} // ok!
535}
536```
d9579d0f
AL
537"##,
538
539E0049: r##"
540This error indicates that an attempted implementation of a trait method
541has the wrong number of type parameters.
542
543For example, the trait below has a method `foo` with a type parameter `T`,
544but the implementation of `foo` for the type `Bar` is missing this parameter:
545
7453a54e 546```compile_fail
d9579d0f
AL
547trait Foo {
548 fn foo<T: Default>(x: T) -> Self;
549}
550
551struct Bar;
552
553// error: method `foo` has 0 type parameters but its trait declaration has 1
554// type parameter
555impl Foo for Bar {
556 fn foo(x: bool) -> Self { Bar }
557}
558```
559"##,
560
561E0050: r##"
562This error indicates that an attempted implementation of a trait method
563has the wrong number of function parameters.
564
565For example, the trait below has a method `foo` with two function parameters
566(`&self` and `u8`), but the implementation of `foo` for the type `Bar` omits
567the `u8` parameter:
568
7453a54e 569```compile_fail
d9579d0f
AL
570trait Foo {
571 fn foo(&self, x: u8) -> bool;
572}
573
574struct Bar;
575
576// error: method `foo` has 1 parameter but the declaration in trait `Foo::foo`
577// has 2
578impl Foo for Bar {
579 fn foo(&self) -> bool { true }
580}
581```
582"##,
583
584E0053: r##"
62682a34
SL
585The parameters of any trait method must match between a trait implementation
586and the trait definition.
d9579d0f 587
62682a34 588Here are a couple examples of this error:
d9579d0f 589
7453a54e 590```compile_fail
62682a34
SL
591trait Foo {
592 fn foo(x: u16);
593 fn bar(&self);
594}
d9579d0f
AL
595
596struct Bar;
597
598impl Foo for Bar {
62682a34
SL
599 // error, expected u16, found i16
600 fn foo(x: i16) { }
601
602 // error, values differ in mutability
603 fn bar(&mut self) { }
d9579d0f 604}
62682a34
SL
605```
606"##,
607
608E0054: r##"
609It is not allowed to cast to a bool. If you are trying to cast a numeric type
610to a bool, you can compare it with zero instead:
d9579d0f 611
7453a54e
SL
612```compile_fail
613let x = 5;
614
615// Not allowed, won't compile
616let x_is_nonzero = x as bool;
617```
618
d9579d0f 619```
62682a34 620let x = 5;
d9579d0f 621
62682a34
SL
622// Ok
623let x_is_nonzero = x != 0;
d9579d0f 624```
62682a34 625"##,
d9579d0f 626
62682a34
SL
627E0055: r##"
628During a method call, a value is automatically dereferenced as many times as
629needed to make the value's type match the method's receiver. The catch is that
630the compiler will only attempt to dereference a number of times up to the
631recursion limit (which can be set via the `recursion_limit` attribute).
d9579d0f 632
62682a34
SL
633For a somewhat artificial example:
634
7453a54e 635```compile_fail
62682a34
SL
636#![recursion_limit="2"]
637
638struct Foo;
639
640impl Foo {
641 fn foo(&self) {}
642}
643
644fn main() {
645 let foo = Foo;
646 let ref_foo = &&Foo;
647
648 // error, reached the recursion limit while auto-dereferencing &&Foo
649 ref_foo.foo();
650}
651```
652
653One fix may be to increase the recursion limit. Note that it is possible to
654create an infinite recursion of dereferencing, in which case the only fix is to
655somehow break the recursion.
656"##,
657
658E0057: r##"
659When invoking closures or other implementations of the function traits `Fn`,
660`FnMut` or `FnOnce` using call notation, the number of parameters passed to the
661function must match its definition.
662
663An example using a closure:
664
7453a54e 665```compile_fail
62682a34
SL
666let f = |x| x * 3;
667let a = f(); // invalid, too few parameters
668let b = f(4); // this works!
669let c = f(2, 3); // invalid, too many parameters
670```
671
672A generic function must be treated similarly:
673
674```
675fn foo<F: Fn()>(f: F) {
676 f(); // this is valid, but f(3) would not work
d9579d0f 677}
62682a34
SL
678```
679"##,
680
681E0059: r##"
682The built-in function traits are generic over a tuple of the function arguments.
683If one uses angle-bracket notation (`Fn<(T,), Output=U>`) instead of parentheses
684(`Fn(T) -> U`) to denote the function trait, the type parameter should be a
685tuple. Otherwise function call notation cannot be used and the trait will not be
686implemented by closures.
d9579d0f 687
62682a34
SL
688The most likely source of this error is using angle-bracket notation without
689wrapping the function argument type into a tuple, for example:
690
7453a54e 691```compile_fail
62682a34 692fn foo<F: Fn<i32>>(f: F) -> F::Output { f(3) }
d9579d0f
AL
693```
694
62682a34
SL
695It can be fixed by adjusting the trait bound like this:
696
7453a54e 697```ignore
62682a34
SL
698fn foo<F: Fn<(i32,)>>(f: F) -> F::Output { f(3) }
699```
d9579d0f 700
62682a34
SL
701Note that `(T,)` always denotes the type of a 1-tuple containing an element of
702type `T`. The comma is necessary for syntactic disambiguation.
d9579d0f
AL
703"##,
704
62682a34
SL
705E0060: r##"
706External C functions are allowed to be variadic. However, a variadic function
707takes a minimum number of arguments. For example, consider C's variadic `printf`
708function:
d9579d0f 709
7453a54e 710```ignore
62682a34
SL
711extern crate libc;
712use libc::{ c_char, c_int };
d9579d0f 713
62682a34
SL
714extern "C" {
715 fn printf(_: *const c_char, ...) -> c_int;
716}
717```
718
719Using this declaration, it must be called with at least one argument, so
c1a9b12d 720simply calling `printf()` is invalid. But the following uses are allowed:
d9579d0f 721
7453a54e 722```ignore
62682a34
SL
723unsafe {
724 use std::ffi::CString;
725
726 printf(CString::new("test\n").unwrap().as_ptr());
727 printf(CString::new("number = %d\n").unwrap().as_ptr(), 3);
728 printf(CString::new("%d, %d\n").unwrap().as_ptr(), 10, 5);
729}
730```
731"##,
732
733E0061: r##"
734The number of arguments passed to a function must match the number of arguments
735specified in the function signature.
736
7453a54e 737For example, a function like:
62682a34
SL
738
739```
740fn f(a: u16, b: &str) {}
741```
742
7453a54e 743Must always be called with exactly two arguments, e.g. `f(2, "test")`.
62682a34
SL
744
745Note, that Rust does not have a notion of optional function arguments or
746variadic functions (except for its C-FFI).
d9579d0f
AL
747"##,
748
749E0062: r##"
750This error indicates that during an attempt to build a struct or struct-like
e9174d1e
SL
751enum variant, one of the fields was specified more than once. Erroneous code
752example:
753
7453a54e 754```compile_fail
e9174d1e
SL
755struct Foo {
756 x: i32
757}
758
759fn main() {
760 let x = Foo {
761 x: 0,
762 x: 0, // error: field `x` specified more than once
763 };
764}
765```
766
767Each field should be specified exactly one time. Example:
768
769```
770struct Foo {
771 x: i32
772}
773
774fn main() {
775 let x = Foo { x: 0 }; // ok!
776}
777```
d9579d0f
AL
778"##,
779
780E0063: r##"
781This error indicates that during an attempt to build a struct or struct-like
e9174d1e
SL
782enum variant, one of the fields was not provided. Erroneous code example:
783
7453a54e 784```compile_fail
e9174d1e
SL
785struct Foo {
786 x: i32,
787 y: i32
788}
789
790fn main() {
791 let x = Foo { x: 0 }; // error: missing field: `y`
792}
793```
794
795Each field should be specified exactly once. Example:
796
797```
798struct Foo {
799 x: i32,
800 y: i32
801}
802
803fn main() {
804 let x = Foo { x: 0, y: 0 }; // ok!
805}
806```
d9579d0f
AL
807"##,
808
809E0066: r##"
810Box placement expressions (like C++'s "placement new") do not yet support any
811place expression except the exchange heap (i.e. `std::boxed::HEAP`).
812Furthermore, the syntax is changing to use `in` instead of `box`. See [RFC 470]
813and [RFC 809] for more details.
814
815[RFC 470]: https://github.com/rust-lang/rfcs/pull/470
816[RFC 809]: https://github.com/rust-lang/rfcs/pull/809
817"##,
818
819E0067: r##"
62682a34
SL
820The left-hand side of a compound assignment expression must be an lvalue
821expression. An lvalue expression represents a memory location and includes
822item paths (ie, namespaced variables), dereferences, indexing expressions,
823and field references.
d9579d0f 824
7453a54e 825Let's start with some erroneous code examples:
e9174d1e 826
7453a54e 827```compile_fail
d9579d0f
AL
828use std::collections::LinkedList;
829
d9579d0f
AL
830// Bad: assignment to non-lvalue expression
831LinkedList::new() += 1;
62682a34
SL
832
833// ...
834
835fn some_func(i: &mut i32) {
836 i += 12; // Error : '+=' operation cannot be applied on a reference !
837}
e9174d1e 838```
62682a34 839
7453a54e 840And now some working examples:
e9174d1e 841
62682a34
SL
842```
843let mut i : i32 = 0;
844
845i += 12; // Good !
846
847// ...
848
849fn some_func(i: &mut i32) {
850 *i += 12; // Good !
851}
d9579d0f
AL
852```
853"##,
854
855E0069: r##"
856The compiler found a function whose body contains a `return;` statement but
857whose return type is not `()`. An example of this is:
858
7453a54e 859```compile_fail
d9579d0f
AL
860// error
861fn foo() -> u8 {
862 return;
863}
864```
865
866Since `return;` is just like `return ();`, there is a mismatch between the
867function's return type and the value being returned.
868"##,
869
62682a34
SL
870E0070: r##"
871The left-hand side of an assignment operator must be an lvalue expression. An
872lvalue expression represents a memory location and can be a variable (with
873optional namespacing), a dereference, an indexing expression or a field
874reference.
875
876More details can be found here:
9cc50fc6 877https://doc.rust-lang.org/reference.html#lvalues-rvalues-and-temporaries
62682a34 878
7453a54e 879Now, we can go further. Here are some erroneous code examples:
e9174d1e 880
7453a54e 881```compile_fail
62682a34
SL
882struct SomeStruct {
883 x: i32,
884 y: i32
885}
7453a54e 886
62682a34
SL
887const SOME_CONST : i32 = 12;
888
889fn some_other_func() {}
890
891fn some_function() {
892 SOME_CONST = 14; // error : a constant value cannot be changed!
893 1 = 3; // error : 1 isn't a valid lvalue!
894 some_other_func() = 4; // error : we can't assign value to a function!
895 SomeStruct.x = 12; // error : SomeStruct a structure name but it is used
896 // like a variable!
897}
898```
899
7453a54e 900And now let's give working examples:
62682a34
SL
901
902```
903struct SomeStruct {
904 x: i32,
905 y: i32
906}
907let mut s = SomeStruct {x: 0, y: 0};
908
909s.x = 3; // that's good !
910
911// ...
912
913fn some_func(x: &mut i32) {
914 *x = 12; // that's good !
915}
916```
917"##,
918
c1a9b12d 919E0071: r##"
e9174d1e
SL
920You tried to use structure-literal syntax to create an item that is
921not a struct-style structure or enum variant.
922
c1a9b12d
SL
923Example of erroneous code:
924
7453a54e 925```compile_fail
e9174d1e 926enum Foo { FirstValue(i32) };
c1a9b12d 927
54a0048b 928let u = Foo::FirstValue { value: 0 }; // error: Foo::FirstValue
c1a9b12d 929 // isn't a structure!
e9174d1e
SL
930// or even simpler, if the name doesn't refer to a structure at all.
931let t = u32 { value: 4 }; // error: `u32` does not name a structure.
c1a9b12d
SL
932```
933
e9174d1e
SL
934To fix this, ensure that the name was correctly spelled, and that
935the correct form of initializer was used.
c1a9b12d 936
e9174d1e 937For example, the code above can be fixed to:
c1a9b12d
SL
938
939```
c1a9b12d 940enum Foo {
e9174d1e 941 FirstValue(i32)
c1a9b12d
SL
942}
943
944fn main() {
e9174d1e 945 let u = Foo::FirstValue(0i32);
c1a9b12d 946
e9174d1e 947 let t = 4;
c1a9b12d
SL
948}
949```
950"##,
951
62682a34
SL
952E0073: r##"
953You cannot define a struct (or enum) `Foo` that requires an instance of `Foo`
954in order to make a new `Foo` value. This is because there would be no way a
955first instance of `Foo` could be made to initialize another instance!
956
957Here's an example of a struct that has this problem:
958
7453a54e 959```compile_fail
62682a34
SL
960struct Foo { x: Box<Foo> } // error
961```
962
963One fix is to use `Option`, like so:
964
965```
966struct Foo { x: Option<Box<Foo>> }
967```
968
969Now it's possible to create at least one instance of `Foo`: `Foo { x: None }`.
970"##,
971
c1a9b12d
SL
972E0074: r##"
973When using the `#[simd]` attribute on a tuple struct, the components of the
974tuple struct must all be of a concrete, nongeneric type so the compiler can
975reason about how to use SIMD with them. This error will occur if the types
976are generic.
977
7453a54e
SL
978This will cause an error:
979
980```compile_fail
981#![feature(simd)]
982
c1a9b12d 983#[simd]
7453a54e
SL
984struct Bad<T>(T, T, T);
985```
986
987This will not:
988
989```
990#![feature(simd)]
c1a9b12d
SL
991
992#[simd]
7453a54e 993struct Good(u32, u32, u32);
c1a9b12d
SL
994```
995"##,
996
997E0075: r##"
998The `#[simd]` attribute can only be applied to non empty tuple structs, because
999it doesn't make sense to try to use SIMD operations when there are no values to
1000operate on.
1001
7453a54e
SL
1002This will cause an error:
1003
1004```compile_fail
1005#![feature(simd)]
1006
c1a9b12d 1007#[simd]
7453a54e
SL
1008struct Bad;
1009```
1010
1011This will not:
1012
1013```
1014#![feature(simd)]
c1a9b12d
SL
1015
1016#[simd]
7453a54e 1017struct Good(u32);
c1a9b12d
SL
1018```
1019"##,
1020
1021E0076: r##"
1022When using the `#[simd]` attribute to automatically use SIMD operations in tuple
1023struct, the types in the struct must all be of the same type, or the compiler
1024will trigger this error.
1025
7453a54e
SL
1026This will cause an error:
1027
1028```compile_fail
1029#![feature(simd)]
c1a9b12d
SL
1030
1031#[simd]
7453a54e 1032struct Bad(u16, u32, u32);
c1a9b12d
SL
1033```
1034
7453a54e
SL
1035This will not:
1036
1037```
1038#![feature(simd)]
1039
1040#[simd]
1041struct Good(u32, u32, u32);
1042```
c1a9b12d
SL
1043"##,
1044
1045E0077: r##"
1046When using the `#[simd]` attribute on a tuple struct, the elements in the tuple
1047must be machine types so SIMD operations can be applied to them.
1048
7453a54e
SL
1049This will cause an error:
1050
1051```compile_fail
1052#![feature(simd)]
1053
c1a9b12d 1054#[simd]
7453a54e
SL
1055struct Bad(String);
1056```
1057
1058This will not:
1059
1060```
1061#![feature(simd)]
c1a9b12d
SL
1062
1063#[simd]
7453a54e 1064struct Good(u32, u32, u32);
c1a9b12d
SL
1065```
1066"##,
1067
e9174d1e
SL
1068E0079: r##"
1069Enum variants which contain no data can be given a custom integer
1070representation. This error indicates that the value provided is not an integer
1071literal and is therefore invalid.
1072
7453a54e 1073For example, in the following code:
e9174d1e 1074
7453a54e 1075```compile_fail
e9174d1e
SL
1076enum Foo {
1077 Q = "32"
1078}
1079```
1080
7453a54e 1081We try to set the representation to a string.
e9174d1e
SL
1082
1083There's no general fix for this; if you can work with an integer then just set
1084it to one:
1085
1086```
1087enum Foo {
1088 Q = 32
1089}
1090```
1091
7453a54e 1092However if you actually wanted a mapping between variants and non-integer
e9174d1e
SL
1093objects, it may be preferable to use a method with a match instead:
1094
1095```
1096enum Foo { Q }
1097impl Foo {
1098 fn get_str(&self) -> &'static str {
1099 match *self {
1100 Foo::Q => "32",
1101 }
1102 }
1103}
1104```
1105"##,
1106
1107E0080: r##"
1108This error indicates that the compiler was unable to sensibly evaluate an
1109integer expression provided as an enum discriminant. Attempting to divide by 0
1110or causing integer overflow are two ways to induce this error. For example:
1111
7453a54e 1112```compile_fail
e9174d1e
SL
1113enum Enum {
1114 X = (1 << 500),
1115 Y = (1 / 0)
1116}
1117```
1118
1119Ensure that the expressions given can be evaluated as the desired integer type.
1120See the FFI section of the Reference for more information about using a custom
1121integer type:
1122
1123https://doc.rust-lang.org/reference.html#ffi-attributes
1124"##,
1125
d9579d0f
AL
1126E0081: r##"
1127Enum discriminants are used to differentiate enum variants stored in memory.
1128This error indicates that the same value was used for two or more variants,
1129making them impossible to tell apart.
1130
7453a54e
SL
1131```compile_fail
1132// Bad.
d9579d0f 1133enum Enum {
7453a54e 1134 P = 3,
d9579d0f
AL
1135 X = 3,
1136 Y = 5
1137}
7453a54e 1138```
d9579d0f 1139
7453a54e
SL
1140```
1141// Good.
d9579d0f 1142enum Enum {
7453a54e 1143 P,
d9579d0f
AL
1144 X = 3,
1145 Y = 5
1146}
1147```
1148
1149Note that variants without a manually specified discriminant are numbered from
1150top to bottom starting from 0, so clashes can occur with seemingly unrelated
1151variants.
1152
7453a54e 1153```compile_fail
d9579d0f
AL
1154enum Bad {
1155 X,
1156 Y = 0
1157}
1158```
1159
54a0048b 1160Here `X` will have already been specified the discriminant 0 by the time `Y` is
d9579d0f
AL
1161encountered, so a conflict occurs.
1162"##,
1163
1164E0082: r##"
54a0048b
SL
1165When you specify enum discriminants with `=`, the compiler expects `isize`
1166values by default. Or you can add the `repr` attibute to the enum declaration
1167for an explicit choice of the discriminant type. In either cases, the
1168discriminant values must fall within a valid range for the expected type;
1169otherwise this error is raised. For example:
d9579d0f 1170
7453a54e 1171```compile_fail
d9579d0f
AL
1172#[repr(u8)]
1173enum Thing {
1174 A = 1024,
1175 B = 5
1176}
1177```
1178
1179Here, 1024 lies outside the valid range for `u8`, so the discriminant for `A` is
54a0048b
SL
1180invalid. Here is another, more subtle example which depends on target word size:
1181
1182```compile_fail
1183enum DependsOnPointerSize {
1184 A = 1 << 32
1185}
1186```
1187
1188Here, `1 << 32` is interpreted as an `isize` value. So it is invalid for 32 bit
1189target (`target_pointer_width = "32"`) but valid for 64 bit target.
d9579d0f 1190
54a0048b
SL
1191You may want to change representation types to fix this, or else change invalid
1192discriminant values so that they fit within the existing type.
d9579d0f
AL
1193"##,
1194
d9579d0f
AL
1195E0084: r##"
1196It is impossible to define an integer type to be used to represent zero-variant
1197enum values because there are no zero-variant enum values. There is no way to
1198construct an instance of the following type using only safe code:
1199
1200```
1201enum Empty {}
1202```
1203"##,
1204
62682a34
SL
1205E0087: r##"
1206Too many type parameters were supplied for a function. For example:
1207
7453a54e 1208```compile_fail
62682a34
SL
1209fn foo<T>() {}
1210
1211fn main() {
1212 foo::<f64, bool>(); // error, expected 1 parameter, found 2 parameters
1213}
1214```
1215
e9174d1e 1216The number of supplied parameters must exactly match the number of defined type
62682a34
SL
1217parameters.
1218"##,
1219
c1a9b12d
SL
1220E0088: r##"
1221You gave too many lifetime parameters. Erroneous code example:
1222
7453a54e 1223```compile_fail
c1a9b12d
SL
1224fn f() {}
1225
1226fn main() {
1227 f::<'static>() // error: too many lifetime parameters provided
1228}
1229```
1230
1231Please check you give the right number of lifetime parameters. Example:
1232
1233```
1234fn f() {}
1235
1236fn main() {
1237 f() // ok!
1238}
1239```
1240
1241It's also important to note that the Rust compiler can generally
1242determine the lifetime by itself. Example:
1243
1244```
1245struct Foo {
1246 value: String
1247}
1248
1249impl Foo {
1250 // it can be written like this
1251 fn get_value<'a>(&'a self) -> &'a str { &self.value }
1252 // but the compiler works fine with this too:
1253 fn without_lifetime(&self) -> &str { &self.value }
1254}
1255
1256fn main() {
1257 let f = Foo { value: "hello".to_owned() };
1258
1259 println!("{}", f.get_value());
1260 println!("{}", f.without_lifetime());
1261}
1262```
1263"##,
1264
62682a34
SL
1265E0089: r##"
1266Not enough type parameters were supplied for a function. For example:
1267
7453a54e 1268```compile_fail
62682a34
SL
1269fn foo<T, U>() {}
1270
1271fn main() {
1272 foo::<f64>(); // error, expected 2 parameters, found 1 parameter
1273}
1274```
1275
1276Note that if a function takes multiple type parameters but you want the compiler
1277to infer some of them, you can use type placeholders:
1278
7453a54e 1279```compile_fail
62682a34
SL
1280fn foo<T, U>(x: T) {}
1281
1282fn main() {
1283 let x: bool = true;
1284 foo::<f64>(x); // error, expected 2 parameters, found 1 parameter
1285 foo::<_, f64>(x); // same as `foo::<bool, f64>(x)`
1286}
1287```
1288"##,
1289
c1a9b12d
SL
1290E0091: r##"
1291You gave an unnecessary type parameter in a type alias. Erroneous code
1292example:
1293
7453a54e 1294```compile_fail
c1a9b12d
SL
1295type Foo<T> = u32; // error: type parameter `T` is unused
1296// or:
1297type Foo<A,B> = Box<A>; // error: type parameter `B` is unused
1298```
1299
1300Please check you didn't write too many type parameters. Example:
1301
1302```
1303type Foo = u32; // ok!
7453a54e 1304type Foo2<A> = Box<A>; // ok!
c1a9b12d
SL
1305```
1306"##,
1307
1308E0092: r##"
1309You tried to declare an undefined atomic operation function.
1310Erroneous code example:
1311
7453a54e 1312```compile_fail
c1a9b12d
SL
1313#![feature(intrinsics)]
1314
1315extern "rust-intrinsic" {
1316 fn atomic_foo(); // error: unrecognized atomic operation
1317 // function
1318}
1319```
1320
1321Please check you didn't make a mistake in the function's name. All intrinsic
1322functions are defined in librustc_trans/trans/intrinsic.rs and in
1323libcore/intrinsics.rs in the Rust source code. Example:
1324
1325```
1326#![feature(intrinsics)]
1327
1328extern "rust-intrinsic" {
1329 fn atomic_fence(); // ok!
1330}
1331```
1332"##,
1333
1334E0093: r##"
1335You declared an unknown intrinsic function. Erroneous code example:
1336
7453a54e 1337```compile_fail
c1a9b12d
SL
1338#![feature(intrinsics)]
1339
1340extern "rust-intrinsic" {
1341 fn foo(); // error: unrecognized intrinsic function: `foo`
1342}
1343
1344fn main() {
1345 unsafe {
1346 foo();
1347 }
1348}
1349```
1350
1351Please check you didn't make a mistake in the function's name. All intrinsic
1352functions are defined in librustc_trans/trans/intrinsic.rs and in
1353libcore/intrinsics.rs in the Rust source code. Example:
1354
1355```
1356#![feature(intrinsics)]
1357
1358extern "rust-intrinsic" {
1359 fn atomic_fence(); // ok!
1360}
1361
1362fn main() {
1363 unsafe {
1364 atomic_fence();
1365 }
1366}
1367```
1368"##,
1369
1370E0094: r##"
1371You gave an invalid number of type parameters to an intrinsic function.
1372Erroneous code example:
1373
7453a54e 1374```compile_fail
c1a9b12d
SL
1375#![feature(intrinsics)]
1376
1377extern "rust-intrinsic" {
1378 fn size_of<T, U>() -> usize; // error: intrinsic has wrong number
1379 // of type parameters
1380}
1381```
1382
1383Please check that you provided the right number of lifetime parameters
1384and verify with the function declaration in the Rust source code.
1385Example:
1386
1387```
1388#![feature(intrinsics)]
1389
1390extern "rust-intrinsic" {
1391 fn size_of<T>() -> usize; // ok!
1392}
1393```
1394"##,
1395
1396E0101: r##"
b039eaaf
SL
1397You hit this error because the compiler lacks the information to
1398determine a type for this expression. Erroneous code example:
c1a9b12d 1399
7453a54e 1400```compile_fail
c1a9b12d
SL
1401fn main() {
1402 let x = |_| {}; // error: cannot determine a type for this expression
1403}
1404```
1405
1406You have two possibilities to solve this situation:
1407 * Give an explicit definition of the expression
1408 * Infer the expression
1409
1410Examples:
1411
1412```
1413fn main() {
1414 let x = |_ : u32| {}; // ok!
1415 // or:
1416 let x = |_| {};
1417 x(0u32);
1418}
1419```
1420"##,
1421
e9174d1e
SL
1422E0102: r##"
1423You hit this error because the compiler lacks information to
1424determine a type for this variable. Erroneous code example:
1425
7453a54e 1426```compile_fail
e9174d1e
SL
1427fn demo(devil: fn () -> !) {
1428 let x: &_ = devil();
1429 // error: cannot determine a type for this local variable
1430}
1431
1432fn oh_no() -> ! { panic!("the devil is in the details") }
1433
1434fn main() {
1435 demo(oh_no);
1436}
1437```
1438
1439To solve this situation, constrain the type of the variable.
1440Examples:
1441
7453a54e
SL
1442```no_run
1443#![allow(unused_variables)]
1444
e9174d1e
SL
1445fn some_func(x: &u32) {
1446 // some code
1447}
1448
1449fn demo(devil: fn () -> !) {
1450 let x: &u32 = devil();
1451 // Here we defined the type at the variable creation
1452
1453 let x: &_ = devil();
1454 some_func(x);
1455 // Here, the type is determined by the function argument type
1456}
1457
1458fn oh_no() -> ! { panic!("the devil is in the details") }
1459
1460fn main() {
1461 demo(oh_no);
1462}
1463```
1464"##,
1465
d9579d0f
AL
1466E0106: r##"
1467This error indicates that a lifetime is missing from a type. If it is an error
1468inside a function signature, the problem may be with failing to adhere to the
1469lifetime elision rules (see below).
1470
1471Here are some simple examples of where you'll run into this error:
1472
7453a54e 1473```compile_fail
d9579d0f
AL
1474struct Foo { x: &bool } // error
1475struct Foo<'a> { x: &'a bool } // correct
1476
1477enum Bar { A(u8), B(&bool), } // error
1478enum Bar<'a> { A(u8), B(&'a bool), } // correct
1479
1480type MyStr = &str; // error
b039eaaf 1481type MyStr<'a> = &'a str; // correct
d9579d0f
AL
1482```
1483
1484Lifetime elision is a special, limited kind of inference for lifetimes in
1485function signatures which allows you to leave out lifetimes in certain cases.
1486For more background on lifetime elision see [the book][book-le].
1487
1488The lifetime elision rules require that any function signature with an elided
1489output lifetime must either have
1490
1491 - exactly one input lifetime
1492 - or, multiple input lifetimes, but the function must also be a method with a
1493 `&self` or `&mut self` receiver
1494
1495In the first case, the output lifetime is inferred to be the same as the unique
1496input lifetime. In the second case, the lifetime is instead inferred to be the
1497same as the lifetime on `&self` or `&mut self`.
1498
1499Here are some examples of elision errors:
1500
7453a54e 1501```compile_fail
d9579d0f 1502// error, no input lifetimes
7453a54e 1503fn foo() -> &str { }
d9579d0f
AL
1504
1505// error, `x` and `y` have distinct lifetimes inferred
7453a54e 1506fn bar(x: &str, y: &str) -> &str { }
d9579d0f
AL
1507
1508// error, `y`'s lifetime is inferred to be distinct from `x`'s
7453a54e 1509fn baz<'a>(x: &'a str, y: &str) -> &str { }
d9579d0f
AL
1510```
1511
e9174d1e 1512[book-le]: https://doc.rust-lang.org/nightly/book/lifetimes.html#lifetime-elision
d9579d0f
AL
1513"##,
1514
1515E0107: r##"
1516This error means that an incorrect number of lifetime parameters were provided
1517for a type (like a struct or enum) or trait.
1518
1519Some basic examples include:
1520
7453a54e 1521```compile_fail
d9579d0f
AL
1522struct Foo<'a>(&'a str);
1523enum Bar { A, B, C }
1524
1525struct Baz<'a> {
1526 foo: Foo, // error: expected 1, found 0
1527 bar: Bar<'a>, // error: expected 0, found 1
1528}
1529```
1530
1531Here's an example that is currently an error, but may work in a future version
1532of Rust:
1533
7453a54e 1534```compile_fail
d9579d0f
AL
1535struct Foo<'a>(&'a str);
1536
1537trait Quux { }
1538impl Quux for Foo { } // error: expected 1, found 0
1539```
1540
1541Lifetime elision in implementation headers was part of the lifetime elision
1542RFC. It is, however, [currently unimplemented][iss15872].
1543
1544[iss15872]: https://github.com/rust-lang/rust/issues/15872
1545"##,
1546
62682a34
SL
1547E0116: r##"
1548You can only define an inherent implementation for a type in the same crate
1549where the type was defined. For example, an `impl` block as below is not allowed
1550since `Vec` is defined in the standard library:
1551
7453a54e
SL
1552```compile_fail
1553impl Vec<u8> { } // error
62682a34
SL
1554```
1555
1556To fix this problem, you can do either of these things:
1557
1558 - define a trait that has the desired associated functions/types/constants and
1559 implement the trait for the type in question
1560 - define a new type wrapping the type and define an implementation on the new
1561 type
1562
1563Note that using the `type` keyword does not work here because `type` only
1564introduces a type alias:
1565
7453a54e 1566```compile_fail
62682a34
SL
1567type Bytes = Vec<u8>;
1568
7453a54e 1569impl Bytes { } // error, same as above
62682a34
SL
1570```
1571"##,
1572
c1a9b12d
SL
1573E0117: r##"
1574This error indicates a violation of one of Rust's orphan rules for trait
1575implementations. The rule prohibits any implementation of a foreign trait (a
1576trait defined in another crate) where
62682a34 1577
c1a9b12d
SL
1578 - the type that is implementing the trait is foreign
1579 - all of the parameters being passed to the trait (if there are any) are also
1580 foreign.
62682a34 1581
c1a9b12d 1582Here's one example of this error:
62682a34 1583
7453a54e 1584```compile_fail
c1a9b12d
SL
1585impl Drop for u32 {}
1586```
d9579d0f 1587
c1a9b12d
SL
1588To avoid this kind of error, ensure that at least one local type is referenced
1589by the `impl`:
d9579d0f 1590
7453a54e 1591```ignore
c1a9b12d
SL
1592pub struct Foo; // you define your type in your crate
1593
1594impl Drop for Foo { // and you can implement the trait on it!
1595 // code of trait implementation here
1596}
1597
1598impl From<Foo> for i32 { // or you use a type from your crate as
1599 // a type parameter
1600 fn from(i: Foo) -> i32 {
1601 0
1602 }
1603}
d9579d0f 1604```
d9579d0f 1605
c1a9b12d 1606Alternatively, define a trait locally and implement that instead:
d9579d0f
AL
1607
1608```
c1a9b12d
SL
1609trait Bar {
1610 fn get(&self) -> usize;
1611}
1612
1613impl Bar for u32 {
1614 fn get(&self) -> usize { 0 }
1615}
d9579d0f
AL
1616```
1617
c1a9b12d 1618For information on the design of the orphan rules, see [RFC 1023].
d9579d0f 1619
c1a9b12d
SL
1620[RFC 1023]: https://github.com/rust-lang/rfcs/pull/1023
1621"##,
62682a34 1622
e9174d1e 1623E0118: r##"
7453a54e
SL
1624You're trying to write an inherent implementation for something which isn't a
1625struct nor an enum. Erroneous code example:
1626
1627```compile_fail
1628impl (u8, u8) { // error: no base type found for inherent implementation
1629 fn get_state(&self) -> String {
1630 // ...
1631 }
1632}
1633```
1634
1635To fix this error, please implement a trait on the type or wrap it in a struct.
1636Example:
e9174d1e
SL
1637
1638```
7453a54e
SL
1639// we create a trait here
1640trait LiveLongAndProsper {
1641 fn get_state(&self) -> String;
1642}
1643
1644// and now you can implement it on (u8, u8)
1645impl LiveLongAndProsper for (u8, u8) {
1646 fn get_state(&self) -> String {
1647 "He's dead, Jim!".to_owned()
1648 }
e9174d1e
SL
1649}
1650```
1651
7453a54e
SL
1652Alternatively, you can create a newtype. A newtype is a wrapping tuple-struct.
1653For example, `NewType` is a newtype over `Foo` in `struct NewType(Foo)`.
1654Example:
e9174d1e
SL
1655
1656```
7453a54e
SL
1657struct TypeWrapper((u8, u8));
1658
1659impl TypeWrapper {
1660 fn get_state(&self) -> String {
1661 "Fascinating!".to_owned()
1662 }
1663}
e9174d1e
SL
1664```
1665"##,
1666
c1a9b12d
SL
1667E0119: r##"
1668There are conflicting trait implementations for the same type.
1669Example of erroneous code:
62682a34 1670
7453a54e 1671```compile_fail
c1a9b12d
SL
1672trait MyTrait {
1673 fn get(&self) -> usize;
62682a34 1674}
62682a34 1675
c1a9b12d
SL
1676impl<T> MyTrait for T {
1677 fn get(&self) -> usize { 0 }
1678}
62682a34 1679
c1a9b12d
SL
1680struct Foo {
1681 value: usize
1682}
62682a34 1683
9cc50fc6
SL
1684impl MyTrait for Foo { // error: conflicting implementations of trait
1685 // `MyTrait` for type `Foo`
c1a9b12d
SL
1686 fn get(&self) -> usize { self.value }
1687}
1688```
d9579d0f 1689
c1a9b12d
SL
1690When looking for the implementation for the trait, the compiler finds
1691both the `impl<T> MyTrait for T` where T is all types and the `impl
1692MyTrait for Foo`. Since a trait cannot be implemented multiple times,
1693this is an error. So, when you write:
d9579d0f 1694
c1a9b12d 1695```
7453a54e
SL
1696trait MyTrait {
1697 fn get(&self) -> usize;
1698}
1699
c1a9b12d
SL
1700impl<T> MyTrait for T {
1701 fn get(&self) -> usize { 0 }
1702}
1703```
62682a34 1704
c1a9b12d
SL
1705This makes the trait implemented on all types in the scope. So if you
1706try to implement it on another one after that, the implementations will
1707conflict. Example:
62682a34
SL
1708
1709```
c1a9b12d
SL
1710trait MyTrait {
1711 fn get(&self) -> usize;
62682a34
SL
1712}
1713
c1a9b12d
SL
1714impl<T> MyTrait for T {
1715 fn get(&self) -> usize { 0 }
1716}
62682a34 1717
c1a9b12d
SL
1718struct Foo;
1719
1720fn main() {
1721 let f = Foo;
1722
1723 f.get(); // the trait is implemented so we can use it
62682a34 1724}
c1a9b12d 1725```
62682a34
SL
1726"##,
1727
c1a9b12d
SL
1728E0120: r##"
1729An attempt was made to implement Drop on a trait, which is not allowed: only
1730structs and enums can implement Drop. An example causing this error:
62682a34 1731
7453a54e 1732```compile_fail
c1a9b12d
SL
1733trait MyTrait {}
1734
1735impl Drop for MyTrait {
1736 fn drop(&mut self) {}
1737}
1738```
1739
1740A workaround for this problem is to wrap the trait up in a struct, and implement
1741Drop on that. An example is shown below:
1742
1743```
1744trait MyTrait {}
1745struct MyWrapper<T: MyTrait> { foo: T }
1746
1747impl <T: MyTrait> Drop for MyWrapper<T> {
1748 fn drop(&mut self) {}
1749}
1750
1751```
1752
1753Alternatively, wrapping trait objects requires something like the following:
1754
1755```
1756trait MyTrait {}
1757
1758//or Box<MyTrait>, if you wanted an owned trait object
1759struct MyWrapper<'a> { foo: &'a MyTrait }
1760
1761impl <'a> Drop for MyWrapper<'a> {
1762 fn drop(&mut self) {}
1763}
1764```
1765"##,
1766
1767E0121: r##"
1768In order to be consistent with Rust's lack of global type inference, type
1769placeholders are disallowed by design in item signatures.
1770
1771Examples of this error include:
1772
7453a54e 1773```compile_fail
c1a9b12d
SL
1774fn foo() -> _ { 5 } // error, explicitly write out the return type instead
1775
1776static BAR: _ = "test"; // error, explicitly write out the type instead
1777```
1778"##,
1779
e9174d1e
SL
1780E0122: r##"
1781An attempt was made to add a generic constraint to a type alias. While Rust will
1782allow this with a warning, it will not currently enforce the constraint.
1783Consider the example below:
1784
1785```
1786trait Foo{}
1787
1788type MyType<R: Foo> = (R, ());
1789
1790fn main() {
1791 let t: MyType<u32>;
1792}
1793```
1794
1795We're able to declare a variable of type `MyType<u32>`, despite the fact that
1796`u32` does not implement `Foo`. As a result, one should avoid using generic
1797constraints in concert with type aliases.
1798"##,
1799
c1a9b12d
SL
1800E0124: r##"
1801You declared two fields of a struct with the same name. Erroneous code
1802example:
1803
7453a54e 1804```compile_fail
c1a9b12d
SL
1805struct Foo {
1806 field1: i32,
7453a54e 1807 field1: i32, // error: field is already declared
c1a9b12d
SL
1808}
1809```
1810
1811Please verify that the field names have been correctly spelled. Example:
1812
1813```
1814struct Foo {
1815 field1: i32,
7453a54e 1816 field2: i32, // ok!
c1a9b12d
SL
1817}
1818```
1819"##,
1820
1821E0128: r##"
1822Type parameter defaults can only use parameters that occur before them.
1823Erroneous code example:
1824
7453a54e
SL
1825```compile_fail
1826struct Foo<T=U, U=()> {
c1a9b12d
SL
1827 field1: T,
1828 filed2: U,
1829}
1830// error: type parameters with a default cannot use forward declared
1831// identifiers
1832```
1833
1834Since type parameters are evaluated in-order, you may be able to fix this issue
1835by doing:
1836
1837```
7453a54e 1838struct Foo<U=(), T=U> {
c1a9b12d
SL
1839 field1: T,
1840 filed2: U,
1841}
1842```
1843
1844Please also verify that this wasn't because of a name-clash and rename the type
1845parameter if so.
1846"##,
1847
1848E0130: r##"
1849You declared a pattern as an argument in a foreign function declaration.
1850Erroneous code example:
1851
7453a54e 1852```compile_fail
c1a9b12d
SL
1853extern {
1854 fn foo((a, b): (u32, u32)); // error: patterns aren't allowed in foreign
1855 // function declarations
1856}
1857```
1858
1859Please replace the pattern argument with a regular one. Example:
1860
1861```
1862struct SomeStruct {
1863 a: u32,
1864 b: u32,
1865}
1866
1867extern {
1868 fn foo(s: SomeStruct); // ok!
1869}
7453a54e
SL
1870```
1871
1872Or:
1873
1874```
c1a9b12d
SL
1875extern {
1876 fn foo(a: (u32, u32)); // ok!
1877}
1878```
1879"##,
1880
1881E0131: r##"
1882It is not possible to define `main` with type parameters, or even with function
1883parameters. When `main` is present, it must take no arguments and return `()`.
e9174d1e 1884Erroneous code example:
c1a9b12d 1885
7453a54e 1886```compile_fail
e9174d1e
SL
1887fn main<T>() { // error: main function is not allowed to have type parameters
1888}
c1a9b12d
SL
1889```
1890"##,
1891
e9174d1e
SL
1892E0132: r##"
1893It is not possible to declare type parameters on a function that has the `start`
1894attribute. Such a function must have the following type signature:
c1a9b12d 1895
7453a54e 1896```ignore
e9174d1e 1897fn(isize, *const *const u8) -> isize;
c1a9b12d
SL
1898```
1899"##,
1900
b039eaaf
SL
1901E0163: r##"
1902This error means that an attempt was made to match an enum variant as a
1903struct type when the variant isn't a struct type:
1904
7453a54e 1905```compile_fail
b039eaaf
SL
1906enum Foo { B(u32) }
1907
1908fn bar(foo: Foo) -> u32 {
1909 match foo {
7453a54e 1910 B{i} => i, // error E0163
b039eaaf
SL
1911 }
1912}
1913```
1914
1915Try using `()` instead:
1916
1917```
7453a54e
SL
1918enum Foo { B(u32) }
1919
b039eaaf
SL
1920fn bar(foo: Foo) -> u32 {
1921 match foo {
7453a54e 1922 Foo::B(i) => i,
b039eaaf
SL
1923 }
1924}
1925```
1926"##,
1927
1928E0164: r##"
b039eaaf
SL
1929This error means that an attempt was made to match a struct type enum
1930variant as a non-struct type:
1931
7453a54e
SL
1932```compile_fail
1933enum Foo { B { i: u32 } }
b039eaaf
SL
1934
1935fn bar(foo: Foo) -> u32 {
1936 match foo {
7453a54e 1937 Foo::B(i) => i, // error E0164
b039eaaf
SL
1938 }
1939}
1940```
1941
1942Try using `{}` instead:
1943
1944```
7453a54e
SL
1945enum Foo { B { i: u32 } }
1946
b039eaaf
SL
1947fn bar(foo: Foo) -> u32 {
1948 match foo {
7453a54e 1949 Foo::B{i} => i,
b039eaaf
SL
1950 }
1951}
1952```
1953"##,
1954
c1a9b12d
SL
1955E0166: r##"
1956This error means that the compiler found a return expression in a function
1957marked as diverging. A function diverges if it has `!` in the place of the
1958return type in its signature. For example:
1959
7453a54e 1960```compile_fail
c1a9b12d
SL
1961fn foo() -> ! { return; } // error
1962```
1963
1964For a function that diverges, every control path in the function must never
1965return, for example with a `loop` that never breaks or a call to another
1966diverging function (such as `panic!()`).
1967"##,
1968
1969E0172: r##"
1970This error means that an attempt was made to specify the type of a variable with
1971a combination of a concrete type and a trait. Consider the following example:
1972
7453a54e 1973```compile_fail
c1a9b12d
SL
1974fn foo(bar: i32+std::fmt::Display) {}
1975```
1976
1977The code is trying to specify that we want to receive a signed 32-bit integer
1978which also implements `Display`. This doesn't make sense: when we pass `i32`, a
1979concrete type, it implicitly includes all of the traits that it implements.
1980This includes `Display`, `Debug`, `Clone`, and a host of others.
1981
1982If `i32` implements the trait we desire, there's no need to specify the trait
1983separately. If it does not, then we need to `impl` the trait for `i32` before
1984passing it into `foo`. Either way, a fixed definition for `foo` will look like
1985the following:
1986
1987```
1988fn foo(bar: i32) {}
1989```
1990
1991To learn more about traits, take a look at the Book:
1992
1993https://doc.rust-lang.org/book/traits.html
1994"##,
1995
1996E0178: r##"
1997In types, the `+` type operator has low precedence, so it is often necessary
1998to use parentheses.
1999
2000For example:
2001
7453a54e 2002```compile_fail
c1a9b12d
SL
2003trait Foo {}
2004
2005struct Bar<'a> {
2006 w: &'a Foo + Copy, // error, use &'a (Foo + Copy)
2007 x: &'a Foo + 'a, // error, use &'a (Foo + 'a)
2008 y: &'a mut Foo + 'a, // error, use &'a mut (Foo + 'a)
2009 z: fn() -> Foo + 'a, // error, use fn() -> (Foo + 'a)
2010}
2011```
2012
2013More details can be found in [RFC 438].
2014
2015[RFC 438]: https://github.com/rust-lang/rfcs/pull/438
2016"##,
2017
2018E0184: r##"
2019Explicitly implementing both Drop and Copy for a type is currently disallowed.
2020This feature can make some sense in theory, but the current implementation is
2021incorrect and can lead to memory unsafety (see [issue #20126][iss20126]), so
2022it has been disabled for now.
2023
2024[iss20126]: https://github.com/rust-lang/rust/issues/20126
2025"##,
2026
2027E0185: r##"
2028An associated function for a trait was defined to be static, but an
2029implementation of the trait declared the same function to be a method (i.e. to
2030take a `self` parameter).
2031
2032Here's an example of this error:
2033
7453a54e 2034```compile_fail
c1a9b12d
SL
2035trait Foo {
2036 fn foo();
2037}
2038
2039struct Bar;
2040
2041impl Foo for Bar {
2042 // error, method `foo` has a `&self` declaration in the impl, but not in
2043 // the trait
2044 fn foo(&self) {}
2045}
2046"##,
2047
2048E0186: r##"
2049An associated function for a trait was defined to be a method (i.e. to take a
2050`self` parameter), but an implementation of the trait declared the same function
2051to be static.
2052
2053Here's an example of this error:
2054
7453a54e 2055```compile_fail
c1a9b12d
SL
2056trait Foo {
2057 fn foo(&self);
62682a34
SL
2058}
2059
2060struct Bar;
2061
2062impl Foo for Bar {
2063 // error, method `foo` has a `&self` declaration in the trait, but not in
2064 // the impl
2065 fn foo() {}
2066}
c1a9b12d
SL
2067```
2068"##,
2069
2070E0191: r##"
2071Trait objects need to have all associated types specified. Erroneous code
2072example:
2073
7453a54e 2074```compile_fail
c1a9b12d
SL
2075trait Trait {
2076 type Bar;
2077}
2078
2079type Foo = Trait; // error: the value of the associated type `Bar` (from
2080 // the trait `Trait`) must be specified
2081```
2082
2083Please verify you specified all associated types of the trait and that you
2084used the right trait. Example:
2085
2086```
2087trait Trait {
2088 type Bar;
2089}
2090
2091type Foo = Trait<Bar=i32>; // ok!
2092```
62682a34
SL
2093"##,
2094
2095E0192: r##"
2096Negative impls are only allowed for traits with default impls. For more
2097information see the [opt-in builtin traits RFC](https://github.com/rust-lang/
2098rfcs/blob/master/text/0019-opt-in-builtin-traits.md).
2099"##,
2100
e9174d1e
SL
2101E0193: r##"
2102`where` clauses must use generic type parameters: it does not make sense to use
2103them otherwise. An example causing this error:
2104
7453a54e 2105```compile_fail
e9174d1e
SL
2106trait Foo {
2107 fn bar(&self);
2108}
2109
2110#[derive(Copy,Clone)]
2111struct Wrapper<T> {
2112 Wrapped: T
2113}
2114
2115impl Foo for Wrapper<u32> where Wrapper<u32>: Clone {
2116 fn bar(&self) { }
2117}
2118```
2119
2120This use of a `where` clause is strange - a more common usage would look
2121something like the following:
2122
2123```
7453a54e
SL
2124trait Foo {
2125 fn bar(&self);
2126}
2127
2128#[derive(Copy,Clone)]
2129struct Wrapper<T> {
2130 Wrapped: T
2131}
e9174d1e
SL
2132impl <T> Foo for Wrapper<T> where Wrapper<T>: Clone {
2133 fn bar(&self) { }
2134}
2135```
2136
2137Here, we're saying that the implementation exists on Wrapper only when the
2138wrapped type `T` implements `Clone`. The `where` clause is important because
2139some types will not implement `Clone`, and thus will not get this method.
2140
2141In our erroneous example, however, we're referencing a single concrete type.
b039eaaf
SL
2142Since we know for certain that `Wrapper<u32>` implements `Clone`, there's no
2143reason to also specify it in a `where` clause.
e9174d1e
SL
2144"##,
2145
2146E0194: r##"
2147A type parameter was declared which shadows an existing one. An example of this
2148error:
2149
7453a54e 2150```compile_fail
e9174d1e
SL
2151trait Foo<T> {
2152 fn do_something(&self) -> T;
2153 fn do_something_else<T: Clone>(&self, bar: T);
2154}
2155```
2156
2157In this example, the trait `Foo` and the trait method `do_something_else` both
2158define a type parameter `T`. This is not allowed: if the method wishes to
2159define a type parameter, it must use a different name for it.
2160"##,
2161
c1a9b12d
SL
2162E0195: r##"
2163Your method's lifetime parameters do not match the trait declaration.
2164Erroneous code example:
2165
7453a54e 2166```compile_fail
c1a9b12d
SL
2167trait Trait {
2168 fn bar<'a,'b:'a>(x: &'a str, y: &'b str);
2169}
2170
2171struct Foo;
2172
2173impl Trait for Foo {
2174 fn bar<'a,'b>(x: &'a str, y: &'b str) {
2175 // error: lifetime parameters or bounds on method `bar`
2176 // do not match the trait declaration
2177 }
2178}
2179```
2180
2181The lifetime constraint `'b` for bar() implementation does not match the
2182trait declaration. Ensure lifetime declarations match exactly in both trait
2183declaration and implementation. Example:
2184
2185```
2186trait Trait {
2187 fn t<'a,'b:'a>(x: &'a str, y: &'b str);
2188}
2189
2190struct Foo;
2191
2192impl Trait for Foo {
2193 fn t<'a,'b:'a>(x: &'a str, y: &'b str) { // ok!
2194 }
2195}
2196```
2197"##,
2198
d9579d0f
AL
2199E0197: r##"
2200Inherent implementations (one that do not implement a trait but provide
2201methods associated with a type) are always safe because they are not
2202implementing an unsafe trait. Removing the `unsafe` keyword from the inherent
2203implementation will resolve this error.
2204
7453a54e 2205```compile_fail
d9579d0f
AL
2206struct Foo;
2207
2208// this will cause this error
2209unsafe impl Foo { }
2210// converting it to this will fix it
2211impl Foo { }
2212```
d9579d0f
AL
2213"##,
2214
2215E0198: r##"
2216A negative implementation is one that excludes a type from implementing a
2217particular trait. Not being able to use a trait is always a safe operation,
2218so negative implementations are always safe and never need to be marked as
2219unsafe.
2220
7453a54e
SL
2221```compile_fail
2222#![feature(optin_builtin_traits)]
2223
d9579d0f
AL
2224struct Foo;
2225
2226// unsafe is unnecessary
2227unsafe impl !Clone for Foo { }
d9579d0f 2228```
7453a54e
SL
2229
2230This will compile:
2231
2232```
2233#![feature(optin_builtin_traits)]
2234
2235struct Foo;
2236
2237trait Enterprise {}
2238
2239impl Enterprise for .. { }
2240
2241impl !Enterprise for Foo { }
2242```
2243
2244Please note that negative impls are only allowed for traits with default impls.
d9579d0f
AL
2245"##,
2246
2247E0199: r##"
2248Safe traits should not have unsafe implementations, therefore marking an
7453a54e
SL
2249implementation for a safe trait unsafe will cause a compiler error. Removing
2250the unsafe marker on the trait noted in the error will resolve this problem.
d9579d0f 2251
7453a54e 2252```compile_fail
d9579d0f
AL
2253struct Foo;
2254
2255trait Bar { }
2256
2257// this won't compile because Bar is safe
2258unsafe impl Bar for Foo { }
2259// this will compile
2260impl Bar for Foo { }
2261```
d9579d0f
AL
2262"##,
2263
2264E0200: r##"
2265Unsafe traits must have unsafe implementations. This error occurs when an
2266implementation for an unsafe trait isn't marked as unsafe. This may be resolved
2267by marking the unsafe implementation as unsafe.
2268
7453a54e 2269```compile_fail
d9579d0f
AL
2270struct Foo;
2271
2272unsafe trait Bar { }
2273
2274// this won't compile because Bar is unsafe and impl isn't unsafe
2275impl Bar for Foo { }
2276// this will compile
2277unsafe impl Bar for Foo { }
2278```
d9579d0f
AL
2279"##,
2280
2281E0201: r##"
c1a9b12d
SL
2282It is an error to define two associated items (like methods, associated types,
2283associated functions, etc.) with the same identifier.
d9579d0f 2284
62682a34 2285For example:
d9579d0f 2286
7453a54e 2287```compile_fail
d9579d0f
AL
2288struct Foo(u8);
2289
2290impl Foo {
62682a34 2291 fn bar(&self) -> bool { self.0 > 5 }
c1a9b12d 2292 fn bar() {} // error: duplicate associated function
62682a34
SL
2293}
2294
2295trait Baz {
c1a9b12d 2296 type Quux;
62682a34
SL
2297 fn baz(&self) -> bool;
2298}
2299
2300impl Baz for Foo {
c1a9b12d
SL
2301 type Quux = u32;
2302
62682a34 2303 fn baz(&self) -> bool { true }
d9579d0f
AL
2304
2305 // error: duplicate method
62682a34 2306 fn baz(&self) -> bool { self.0 > 5 }
c1a9b12d
SL
2307
2308 // error: duplicate associated type
2309 type Quux = u32;
d9579d0f
AL
2310}
2311```
54a0048b
SL
2312
2313Note, however, that items with the same name are allowed for inherent `impl`
2314blocks that don't overlap:
2315
2316```
2317struct Foo<T>(T);
2318
2319impl Foo<u8> {
2320 fn bar(&self) -> bool { self.0 > 5 }
2321}
2322
2323impl Foo<bool> {
2324 fn bar(&self) -> bool { self.0 }
2325}
2326```
d9579d0f
AL
2327"##,
2328
62682a34
SL
2329E0202: r##"
2330Inherent associated types were part of [RFC 195] but are not yet implemented.
2331See [the tracking issue][iss8995] for the status of this implementation.
2332
2333[RFC 195]: https://github.com/rust-lang/rfcs/pull/195
2334[iss8995]: https://github.com/rust-lang/rust/issues/8995
2335"##,
2336
d9579d0f
AL
2337E0204: r##"
2338An attempt to implement the `Copy` trait for a struct failed because one of the
2339fields does not implement `Copy`. To fix this, you must implement `Copy` for the
2340mentioned field. Note that this may not be possible, as in the example of
2341
7453a54e 2342```compile_fail
d9579d0f
AL
2343struct Foo {
2344 foo : Vec<u32>,
2345}
2346
2347impl Copy for Foo { }
2348```
2349
2350This fails because `Vec<T>` does not implement `Copy` for any `T`.
2351
2352Here's another example that will fail:
2353
7453a54e 2354```compile_fail
d9579d0f
AL
2355#[derive(Copy)]
2356struct Foo<'a> {
2357 ty: &'a mut bool,
2358}
2359```
2360
2361This fails because `&mut T` is not `Copy`, even when `T` is `Copy` (this
2362differs from the behavior for `&T`, which is always `Copy`).
2363"##,
2364
2365E0205: r##"
2366An attempt to implement the `Copy` trait for an enum failed because one of the
2367variants does not implement `Copy`. To fix this, you must implement `Copy` for
2368the mentioned variant. Note that this may not be possible, as in the example of
2369
7453a54e 2370```compile_fail
d9579d0f
AL
2371enum Foo {
2372 Bar(Vec<u32>),
2373 Baz,
2374}
2375
2376impl Copy for Foo { }
2377```
2378
2379This fails because `Vec<T>` does not implement `Copy` for any `T`.
2380
2381Here's another example that will fail:
2382
7453a54e 2383```compile_fail
d9579d0f
AL
2384#[derive(Copy)]
2385enum Foo<'a> {
2386 Bar(&'a mut bool),
2387 Baz
2388}
2389```
2390
2391This fails because `&mut T` is not `Copy`, even when `T` is `Copy` (this
2392differs from the behavior for `&T`, which is always `Copy`).
2393"##,
2394
2395E0206: r##"
2396You can only implement `Copy` for a struct or enum. Both of the following
2397examples will fail, because neither `i32` (primitive type) nor `&'static Bar`
2398(reference to `Bar`) is a struct or enum:
2399
7453a54e 2400```compile_fail
d9579d0f
AL
2401type Foo = i32;
2402impl Copy for Foo { } // error
2403
2404#[derive(Copy, Clone)]
2405struct Bar;
2406impl Copy for &'static Bar { } // error
2407```
2408"##,
2409
c1a9b12d
SL
2410E0207: r##"
2411You declared an unused type parameter when implementing a trait on an object.
2412Erroneous code example:
2413
7453a54e 2414```compile_fail
c1a9b12d
SL
2415trait MyTrait {
2416 fn get(&self) -> usize;
2417}
2418
2419struct Foo;
2420
2421impl<T> MyTrait for Foo {
2422 fn get(&self) -> usize {
2423 0
2424 }
2425}
2426```
2427
2428Please check your object definition and remove unused type
2429parameter(s). Example:
2430
2431```
2432trait MyTrait {
2433 fn get(&self) -> usize;
2434}
2435
2436struct Foo;
2437
2438impl MyTrait for Foo {
2439 fn get(&self) -> usize {
2440 0
2441 }
2442}
2443```
2444"##,
2445
2446E0210: r##"
2447This error indicates a violation of one of Rust's orphan rules for trait
2448implementations. The rule concerns the use of type parameters in an
2449implementation of a foreign trait (a trait defined in another crate), and
2450states that type parameters must be "covered" by a local type. To understand
2451what this means, it is perhaps easiest to consider a few examples.
2452
2453If `ForeignTrait` is a trait defined in some external crate `foo`, then the
2454following trait `impl` is an error:
2455
7453a54e 2456```compile_fail
c1a9b12d
SL
2457extern crate foo;
2458use foo::ForeignTrait;
2459
7453a54e 2460impl<T> ForeignTrait for T { } // error
c1a9b12d
SL
2461```
2462
2463To work around this, it can be covered with a local type, `MyType`:
2464
7453a54e 2465```ignore
c1a9b12d 2466struct MyType<T>(T);
7453a54e 2467impl<T> ForeignTrait for MyType<T> { } // Ok
c1a9b12d
SL
2468```
2469
7453a54e
SL
2470Please note that a type alias is not sufficient.
2471
c1a9b12d
SL
2472For another example of an error, suppose there's another trait defined in `foo`
2473named `ForeignTrait2` that takes two type parameters. Then this `impl` results
2474in the same rule violation:
2475
7453a54e 2476```compile_fail
c1a9b12d 2477struct MyType2;
7453a54e 2478impl<T> ForeignTrait2<T, MyType<T>> for MyType2 { } // error
c1a9b12d
SL
2479```
2480
2481The reason for this is that there are two appearances of type parameter `T` in
2482the `impl` header, both as parameters for `ForeignTrait2`. The first appearance
2483is uncovered, and so runs afoul of the orphan rule.
2484
2485Consider one more example:
2486
7453a54e
SL
2487```ignore
2488impl<T> ForeignTrait2<MyType<T>, T> for MyType2 { } // Ok
c1a9b12d
SL
2489```
2490
2491This only differs from the previous `impl` in that the parameters `T` and
2492`MyType<T>` for `ForeignTrait2` have been swapped. This example does *not*
2493violate the orphan rule; it is permitted.
2494
2495To see why that last example was allowed, you need to understand the general
2496rule. Unfortunately this rule is a bit tricky to state. Consider an `impl`:
2497
7453a54e 2498```ignore
c1a9b12d
SL
2499impl<P1, ..., Pm> ForeignTrait<T1, ..., Tn> for T0 { ... }
2500```
2501
2502where `P1, ..., Pm` are the type parameters of the `impl` and `T0, ..., Tn`
2503are types. One of the types `T0, ..., Tn` must be a local type (this is another
2504orphan rule, see the explanation for E0117). Let `i` be the smallest integer
2505such that `Ti` is a local type. Then no type parameter can appear in any of the
2506`Tj` for `j < i`.
2507
2508For information on the design of the orphan rules, see [RFC 1023].
2509
2510[RFC 1023]: https://github.com/rust-lang/rfcs/pull/1023
2511"##,
2512
2513E0211: r##"
b039eaaf
SL
2514You used a function or type which doesn't fit the requirements for where it was
2515used. Erroneous code examples:
c1a9b12d 2516
7453a54e 2517```compile_fail
c1a9b12d
SL
2518#![feature(intrinsics)]
2519
2520extern "rust-intrinsic" {
2521 fn size_of<T>(); // error: intrinsic has wrong type
2522}
b039eaaf
SL
2523
2524// or:
2525
2526fn main() -> i32 { 0 }
2527// error: main function expects type: `fn() {main}`: expected (), found i32
2528
2529// or:
2530
2531let x = 1u8;
2532match x {
2533 0u8...3i8 => (),
2534 // error: mismatched types in range: expected u8, found i8
2535 _ => ()
2536}
2537
2538// or:
2539
2540use std::rc::Rc;
2541struct Foo;
2542
2543impl Foo {
2544 fn x(self: Rc<Foo>) {}
2545 // error: mismatched self type: expected `Foo`: expected struct
2546 // `Foo`, found struct `alloc::rc::Rc`
2547}
c1a9b12d
SL
2548```
2549
b039eaaf 2550For the first code example, please check the function definition. Example:
c1a9b12d
SL
2551
2552```
2553#![feature(intrinsics)]
2554
2555extern "rust-intrinsic" {
b039eaaf
SL
2556 fn size_of<T>() -> usize; // ok!
2557}
2558```
2559
2560The second case example is a bit particular : the main function must always
2561have this definition:
2562
7453a54e 2563```compile_fail
b039eaaf
SL
2564fn main();
2565```
2566
2567They never take parameters and never return types.
2568
2569For the third example, when you match, all patterns must have the same type
2570as the type you're matching on. Example:
2571
2572```
2573let x = 1u8;
7453a54e 2574
b039eaaf
SL
2575match x {
2576 0u8...3u8 => (), // ok!
2577 _ => ()
2578}
2579```
2580
2581And finally, for the last example, only `Box<Self>`, `&Self`, `Self`,
2582or `&mut Self` work as explicit self parameters. Example:
2583
2584```
2585struct Foo;
2586
2587impl Foo {
2588 fn x(self: Box<Foo>) {} // ok!
c1a9b12d
SL
2589}
2590```
2591"##,
2592
e9174d1e
SL
2593E0214: r##"
2594A generic type was described using parentheses rather than angle brackets. For
2595example:
2596
7453a54e 2597```compile_fail
e9174d1e
SL
2598fn main() {
2599 let v: Vec(&str) = vec!["foo"];
2600}
2601```
2602
2603This is not currently supported: `v` should be defined as `Vec<&str>`.
2604Parentheses are currently only used with generic types when defining parameters
2605for `Fn`-family traits.
2606"##,
2607
c1a9b12d
SL
2608E0220: r##"
2609You used an associated type which isn't defined in the trait.
2610Erroneous code example:
2611
7453a54e 2612```compile_fail
c1a9b12d
SL
2613trait Trait {
2614 type Bar;
2615}
2616
2617type Foo = Trait<F=i32>; // error: associated type `F` not found for
2618 // `Trait`
2619```
2620
2621Please verify you used the right trait or you didn't misspell the
2622associated type name. Example:
2623
2624```
2625trait Trait {
2626 type Bar;
2627}
2628
2629type Foo = Trait<Bar=i32>; // ok!
2630```
2631"##,
2632
e9174d1e
SL
2633E0221: r##"
2634An attempt was made to retrieve an associated type, but the type was ambiguous.
2635For example:
2636
7453a54e 2637```compile_fail
e9174d1e
SL
2638trait T1 {}
2639trait T2 {}
2640
2641trait Foo {
2642 type A: T1;
2643}
2644
2645trait Bar : Foo {
2646 type A: T2;
2647 fn do_something() {
2648 let _: Self::A;
2649 }
2650}
2651```
2652
2653In this example, `Foo` defines an associated type `A`. `Bar` inherits that type
2654from `Foo`, and defines another associated type of the same name. As a result,
2655when we attempt to use `Self::A`, it's ambiguous whether we mean the `A` defined
2656by `Foo` or the one defined by `Bar`.
2657
2658There are two options to work around this issue. The first is simply to rename
2659one of the types. Alternatively, one can specify the intended type using the
2660following syntax:
2661
2662```
7453a54e
SL
2663trait T1 {}
2664trait T2 {}
2665
2666trait Foo {
2667 type A: T1;
2668}
2669
2670trait Bar : Foo {
2671 type A: T2;
2672 fn do_something() {
2673 let _: <Self as Bar>::A;
2674 }
e9174d1e
SL
2675}
2676```
2677"##,
2678
c1a9b12d
SL
2679E0223: r##"
2680An attempt was made to retrieve an associated type, but the type was ambiguous.
2681For example:
2682
7453a54e 2683```compile_fail
c1a9b12d
SL
2684trait MyTrait {type X; }
2685
2686fn main() {
2687 let foo: MyTrait::X;
2688}
2689```
2690
2691The problem here is that we're attempting to take the type of X from MyTrait.
2692Unfortunately, the type of X is not defined, because it's only made concrete in
2693implementations of the trait. A working version of this code might look like:
2694
2695```
2696trait MyTrait {type X; }
2697struct MyStruct;
2698
2699impl MyTrait for MyStruct {
2700 type X = u32;
2701}
2702
2703fn main() {
2704 let foo: <MyStruct as MyTrait>::X;
2705}
2706```
2707
2708This syntax specifies that we want the X type from MyTrait, as made concrete in
2709MyStruct. The reason that we cannot simply use `MyStruct::X` is that MyStruct
2710might implement two different traits with identically-named associated types.
2711This syntax allows disambiguation between the two.
2712"##,
2713
2714E0225: r##"
2715You attempted to use multiple types as bounds for a closure or trait object.
2716Rust does not currently support this. A simple example that causes this error:
2717
7453a54e 2718```compile_fail
c1a9b12d
SL
2719fn main() {
2720 let _: Box<std::io::Read+std::io::Write>;
2721}
2722```
2723
2724Builtin traits are an exception to this rule: it's possible to have bounds of
2725one non-builtin type, plus any number of builtin types. For example, the
2726following compiles correctly:
2727
2728```
2729fn main() {
2730 let _: Box<std::io::Read+Copy+Sync>;
2731}
2732```
2733"##,
2734
2735E0232: r##"
2736The attribute must have a value. Erroneous code example:
2737
7453a54e
SL
2738```compile_fail
2739#![feature(on_unimplemented)]
2740
c1a9b12d
SL
2741#[rustc_on_unimplemented] // error: this attribute must have a value
2742trait Bar {}
2743```
2744
2745Please supply the missing value of the attribute. Example:
2746
2747```
7453a54e
SL
2748#![feature(on_unimplemented)]
2749
c1a9b12d
SL
2750#[rustc_on_unimplemented = "foo"] // ok!
2751trait Bar {}
2752```
2753"##,
2754
d9579d0f
AL
2755E0243: r##"
2756This error indicates that not enough type parameters were found in a type or
2757trait.
2758
2759For example, the `Foo` struct below is defined to be generic in `T`, but the
2760type parameter is missing in the definition of `Bar`:
2761
7453a54e 2762```compile_fail
d9579d0f
AL
2763struct Foo<T> { x: T }
2764
2765struct Bar { x: Foo }
2766```
2767"##,
2768
2769E0244: r##"
2770This error indicates that too many type parameters were found in a type or
2771trait.
2772
2773For example, the `Foo` struct below has no type parameters, but is supplied
2774with two in the definition of `Bar`:
2775
7453a54e 2776```compile_fail
d9579d0f
AL
2777struct Foo { x: bool }
2778
2779struct Bar<S, T> { x: Foo<S, T> }
2780```
2781"##,
2782
e9174d1e
SL
2783E0248: r##"
2784This error indicates an attempt to use a value where a type is expected. For
2785example:
d9579d0f 2786
7453a54e 2787```compile_fail
e9174d1e
SL
2788enum Foo {
2789 Bar(u32)
2790}
2791
2792fn do_something(x: Foo::Bar) { }
2793```
2794
2795In this example, we're attempting to take a type of `Foo::Bar` in the
2796do_something function. This is not legal: `Foo::Bar` is a value of type `Foo`,
2797not a distinct static type. Likewise, it's not legal to attempt to
2798`impl Foo::Bar`: instead, you must `impl Foo` and then pattern match to specify
b039eaaf 2799behavior for specific enum variants.
e9174d1e
SL
2800"##,
2801
2802E0249: r##"
2803This error indicates a constant expression for the array length was found, but
2804it was not an integer (signed or unsigned) expression.
2805
2806Some examples of code that produces this error are:
2807
7453a54e 2808```compile_fail
e9174d1e
SL
2809const A: [u32; "hello"] = []; // error
2810const B: [u32; true] = []; // error
2811const C: [u32; 0.0] = []; // error
2812"##,
2813
2814E0250: r##"
2815There was an error while evaluating the expression for the length of a fixed-
2816size array type.
2817
2818Some examples of this error are:
2819
7453a54e 2820```compile_fail
e9174d1e 2821// divide by zero in the length expression
d9579d0f
AL
2822const A: [u32; 1/0] = [];
2823
2824// Rust currently will not evaluate the function `foo` at compile time
2825fn foo() -> usize { 12 }
2826const B: [u32; foo()] = [];
2827
2828// it is an error to try to add `u8` and `f64`
2829use std::{f64, u8};
2830const C: [u32; u8::MAX + f64::EPSILON] = [];
2831```
2832"##,
2833
62682a34
SL
2834E0318: r##"
2835Default impls for a trait must be located in the same crate where the trait was
2836defined. For more information see the [opt-in builtin traits RFC](https://github
2837.com/rust-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md).
2838"##,
2839
92a42be0
SL
2840E0321: r##"
2841A cross-crate opt-out trait was implemented on something which wasn't a struct
2842or enum type. Erroneous code example:
2843
7453a54e 2844```compile_fail
92a42be0
SL
2845#![feature(optin_builtin_traits)]
2846
2847struct Foo;
2848
2849impl !Sync for Foo {}
2850
2851unsafe impl Send for &'static Foo {
2852// error: cross-crate traits with a default impl, like `core::marker::Send`,
2853// can only be implemented for a struct/enum type, not
2854// `&'static Foo`
2855```
2856
2857Only structs and enums are permitted to impl Send, Sync, and other opt-out
2858trait, and the struct or enum must be local to the current crate. So, for
2859example, `unsafe impl Send for Rc<Foo>` is not allowed.
2860"##,
2861
d9579d0f
AL
2862E0322: r##"
2863The `Sized` trait is a special trait built-in to the compiler for types with a
2864constant size known at compile-time. This trait is automatically implemented
2865for types as needed by the compiler, and it is currently disallowed to
2866explicitly implement it for a type.
2867"##,
2868
e9174d1e
SL
2869E0323: r##"
2870An associated const was implemented when another trait item was expected.
2871Erroneous code example:
2872
7453a54e
SL
2873```compile_fail
2874#![feature(associated_consts)]
2875
e9174d1e
SL
2876trait Foo {
2877 type N;
2878}
2879
2880struct Bar;
2881
2882impl Foo for Bar {
2883 const N : u32 = 0;
2884 // error: item `N` is an associated const, which doesn't match its
2885 // trait `<Bar as Foo>`
2886}
2887```
2888
2889Please verify that the associated const wasn't misspelled and the correct trait
2890was implemented. Example:
2891
2892```
2893struct Bar;
2894
2895trait Foo {
2896 type N;
2897}
2898
2899impl Foo for Bar {
2900 type N = u32; // ok!
2901}
7453a54e
SL
2902```
2903
2904Or:
2905
2906```
2907#![feature(associated_consts)]
2908
2909struct Bar;
e9174d1e 2910
e9174d1e
SL
2911trait Foo {
2912 const N : u32;
2913}
2914
2915impl Foo for Bar {
2916 const N : u32 = 0; // ok!
2917}
2918```
2919"##,
2920
2921E0324: r##"
2922A method was implemented when another trait item was expected. Erroneous
2923code example:
2924
7453a54e 2925```compile_fail
e9174d1e
SL
2926struct Bar;
2927
2928trait Foo {
2929 const N : u32;
2930
2931 fn M();
2932}
2933
2934impl Foo for Bar {
2935 fn N() {}
2936 // error: item `N` is an associated method, which doesn't match its
2937 // trait `<Bar as Foo>`
2938}
2939```
2940
2941To fix this error, please verify that the method name wasn't misspelled and
2942verify that you are indeed implementing the correct trait items. Example:
2943
2944```
7453a54e
SL
2945#![feature(associated_consts)]
2946
e9174d1e
SL
2947struct Bar;
2948
2949trait Foo {
2950 const N : u32;
2951
2952 fn M();
2953}
2954
2955impl Foo for Bar {
2956 const N : u32 = 0;
2957
2958 fn M() {} // ok!
2959}
2960```
2961"##,
2962
2963E0325: r##"
2964An associated type was implemented when another trait item was expected.
2965Erroneous code example:
2966
7453a54e 2967```compile_fail
e9174d1e
SL
2968struct Bar;
2969
2970trait Foo {
2971 const N : u32;
2972}
2973
2974impl Foo for Bar {
2975 type N = u32;
2976 // error: item `N` is an associated type, which doesn't match its
2977 // trait `<Bar as Foo>`
2978}
2979```
2980
2981Please verify that the associated type name wasn't misspelled and your
2982implementation corresponds to the trait definition. Example:
2983
2984```
2985struct Bar;
2986
2987trait Foo {
2988 type N;
2989}
2990
2991impl Foo for Bar {
2992 type N = u32; // ok!
2993}
7453a54e
SL
2994```
2995
2996Or:
2997
2998```
2999#![feature(associated_consts)]
3000
3001struct Bar;
e9174d1e 3002
e9174d1e
SL
3003trait Foo {
3004 const N : u32;
3005}
3006
3007impl Foo for Bar {
3008 const N : u32 = 0; // ok!
3009}
3010```
3011"##,
3012
62682a34
SL
3013E0326: r##"
3014The types of any associated constants in a trait implementation must match the
3015types in the trait definition. This error indicates that there was a mismatch.
3016
3017Here's an example of this error:
3018
7453a54e 3019```compile_fail
62682a34
SL
3020trait Foo {
3021 const BAR: bool;
3022}
3023
3024struct Bar;
3025
3026impl Foo for Bar {
3027 const BAR: u32 = 5; // error, expected bool, found u32
3028}
3029```
3030"##,
3031
c1a9b12d
SL
3032E0327: r##"
3033You cannot use associated items other than constant items as patterns. This
3034includes method items. Example of erroneous code:
3035
7453a54e 3036```compile_fail
c1a9b12d
SL
3037enum B {}
3038
3039impl B {
3040 fn bb() -> i32 { 0 }
3041}
3042
3043fn main() {
3044 match 0 {
3045 B::bb => {} // error: associated items in match patterns must
3046 // be constants
3047 }
3048}
3049```
3050
3051Please check that you're not using a method as a pattern. Example:
3052
3053```
3054enum B {
3055 ba,
3056 bb
3057}
3058
3059fn main() {
3060 match B::ba {
3061 B::bb => {} // ok!
3062 _ => {}
3063 }
3064}
3065```
3066"##,
3067
e9174d1e
SL
3068E0329: r##"
3069An attempt was made to access an associated constant through either a generic
3070type parameter or `Self`. This is not supported yet. An example causing this
3071error is shown below:
3072
7453a54e
SL
3073```compile_fail
3074#![feature(associated_consts)]
3075
e9174d1e
SL
3076trait Foo {
3077 const BAR: f64;
3078}
3079
3080struct MyStruct;
3081
3082impl Foo for MyStruct {
3083 const BAR: f64 = 0f64;
3084}
3085
3086fn get_bar_bad<F: Foo>(t: F) -> f64 {
3087 F::BAR
3088}
3089```
3090
7453a54e
SL
3091Currently, the value of `BAR` for a particular type can only be accessed
3092through a concrete type, as shown below:
3093
3094```ignore
3095#![feature(associated_consts)]
3096
3097trait Foo {
3098 const BAR: f64;
3099}
3100
3101struct MyStruct;
e9174d1e 3102
e9174d1e
SL
3103fn get_bar_good() -> f64 {
3104 <MyStruct as Foo>::BAR
3105}
3106```
3107"##,
3108
3109E0366: r##"
3110An attempt was made to implement `Drop` on a concrete specialization of a
3111generic type. An example is shown below:
3112
7453a54e 3113```compile_fail
e9174d1e
SL
3114struct Foo<T> {
3115 t: T
3116}
3117
3118impl Drop for Foo<u32> {
3119 fn drop(&mut self) {}
3120}
3121```
3122
3123This code is not legal: it is not possible to specialize `Drop` to a subset of
3124implementations of a generic type. One workaround for this is to wrap the
3125generic type, as shown below:
3126
3127```
3128struct Foo<T> {
3129 t: T
3130}
3131
3132struct Bar {
3133 t: Foo<u32>
3134}
3135
3136impl Drop for Bar {
3137 fn drop(&mut self) {}
3138}
3139```
3140"##,
3141
3142E0367: r##"
3143An attempt was made to implement `Drop` on a specialization of a generic type.
3144An example is shown below:
3145
7453a54e 3146```compile_fail
e9174d1e
SL
3147trait Foo{}
3148
3149struct MyStruct<T> {
3150 t: T
3151}
3152
3153impl<T: Foo> Drop for MyStruct<T> {
3154 fn drop(&mut self) {}
3155}
3156```
3157
3158This code is not legal: it is not possible to specialize `Drop` to a subset of
3159implementations of a generic type. In order for this code to work, `MyStruct`
3160must also require that `T` implements `Foo`. Alternatively, another option is
3161to wrap the generic type in another that specializes appropriately:
3162
3163```
3164trait Foo{}
3165
3166struct MyStruct<T> {
3167 t: T
3168}
3169
3170struct MyStructWrapper<T: Foo> {
3171 t: MyStruct<T>
3172}
3173
3174impl <T: Foo> Drop for MyStructWrapper<T> {
3175 fn drop(&mut self) {}
3176}
3177```
3178"##,
3179
d9579d0f
AL
3180E0368: r##"
3181This error indicates that a binary assignment operator like `+=` or `^=` was
b039eaaf
SL
3182applied to a type that doesn't support it. For example:
3183
7453a54e 3184```compile_fail
b039eaaf 3185let mut x = 12f32; // error: binary operation `<<` cannot be applied to
7453a54e 3186 // type `f32`
d9579d0f 3187
b039eaaf 3188x <<= 2;
d9579d0f 3189```
b039eaaf
SL
3190
3191To fix this error, please check that this type implements this binary
3192operation. Example:
3193
54a0048b
SL
3194```
3195let mut x = 12u32; // the `u32` type does implement the `ShlAssign` trait
b039eaaf
SL
3196
3197x <<= 2; // ok!
d9579d0f
AL
3198```
3199
b039eaaf
SL
3200It is also possible to overload most operators for your own type by
3201implementing the `[OP]Assign` traits from `std::ops`.
3202
d9579d0f
AL
3203Another problem you might be facing is this: suppose you've overloaded the `+`
3204operator for some type `Foo` by implementing the `std::ops::Add` trait for
3205`Foo`, but you find that using `+=` does not work, as in this example:
3206
7453a54e 3207```compile_fail
d9579d0f
AL
3208use std::ops::Add;
3209
3210struct Foo(u32);
3211
3212impl Add for Foo {
3213 type Output = Foo;
3214
3215 fn add(self, rhs: Foo) -> Foo {
3216 Foo(self.0 + rhs.0)
3217 }
3218}
3219
3220fn main() {
3221 let mut x: Foo = Foo(5);
b039eaaf 3222 x += Foo(7); // error, `+= cannot be applied to the type `Foo`
d9579d0f
AL
3223}
3224```
3225
b039eaaf
SL
3226This is because `AddAssign` is not automatically implemented, so you need to
3227manually implement it for your type.
62682a34
SL
3228"##,
3229
e9174d1e
SL
3230E0369: r##"
3231A binary operation was attempted on a type which doesn't support it.
3232Erroneous code example:
3233
7453a54e 3234```compile_fail
e9174d1e
SL
3235let x = 12f32; // error: binary operation `<<` cannot be applied to
3236 // type `f32`
3237
3238x << 2;
3239```
3240
3241To fix this error, please check that this type implements this binary
3242operation. Example:
3243
3244```
3245let x = 12u32; // the `u32` type does implement it:
3246 // https://doc.rust-lang.org/stable/std/ops/trait.Shl.html
3247
3248x << 2; // ok!
3249```
3250
3251It is also possible to overload most operators for your own type by
3252implementing traits from `std::ops`.
3253"##,
3254
3255E0370: r##"
3256The maximum value of an enum was reached, so it cannot be automatically
3257set in the next enum value. Erroneous code example:
3258
7453a54e 3259```compile_fail
e9174d1e
SL
3260enum Foo {
3261 X = 0x7fffffffffffffff,
7453a54e
SL
3262 Y, // error: enum discriminant overflowed on value after
3263 // 9223372036854775807: i64; set explicitly via
3264 // Y = -9223372036854775808 if that is desired outcome
e9174d1e
SL
3265}
3266```
3267
3268To fix this, please set manually the next enum value or put the enum variant
3269with the maximum value at the end of the enum. Examples:
3270
3271```
3272enum Foo {
3273 X = 0x7fffffffffffffff,
3274 Y = 0, // ok!
3275}
7453a54e 3276```
e9174d1e 3277
7453a54e
SL
3278Or:
3279
3280```
e9174d1e
SL
3281enum Foo {
3282 Y = 0, // ok!
3283 X = 0x7fffffffffffffff,
3284}
3285```
3286"##,
3287
62682a34
SL
3288E0371: r##"
3289When `Trait2` is a subtrait of `Trait1` (for example, when `Trait2` has a
3290definition like `trait Trait2: Trait1 { ... }`), it is not allowed to implement
3291`Trait1` for `Trait2`. This is because `Trait2` already implements `Trait1` by
3292definition, so it is not useful to do this.
3293
3294Example:
3295
7453a54e 3296```compile_fail
62682a34
SL
3297trait Foo { fn foo(&self) { } }
3298trait Bar: Foo { }
3299trait Baz: Bar { }
3300
3301impl Bar for Baz { } // error, `Baz` implements `Bar` by definition
3302impl Foo for Baz { } // error, `Baz` implements `Bar` which implements `Foo`
3303impl Baz for Baz { } // error, `Baz` (trivially) implements `Baz`
3304impl Baz for Bar { } // Note: This is OK
3305```
3306"##,
3307
62682a34
SL
3308E0379: r##"
3309Trait methods cannot be declared `const` by design. For more information, see
3310[RFC 911].
3311
3312[RFC 911]: https://github.com/rust-lang/rfcs/pull/911
3313"##,
3314
3315E0380: r##"
3316Default impls are only allowed for traits with no methods or associated items.
3317For more information see the [opt-in builtin traits RFC](https://github.com/rust
3318-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md).
c1a9b12d
SL
3319"##,
3320
e9174d1e
SL
3321E0390: r##"
3322You tried to implement methods for a primitive type. Erroneous code example:
3323
7453a54e 3324```compile_fail
e9174d1e
SL
3325struct Foo {
3326 x: i32
3327}
3328
3329impl *mut Foo {}
3330// error: only a single inherent implementation marked with
3331// `#[lang = "mut_ptr"]` is allowed for the `*mut T` primitive
3332```
3333
3334This isn't allowed, but using a trait to implement a method is a good solution.
3335Example:
3336
3337```
3338struct Foo {
3339 x: i32
3340}
3341
3342trait Bar {
3343 fn bar();
3344}
3345
3346impl Bar for *mut Foo {
3347 fn bar() {} // ok!
3348}
3349```
3350"##,
3351
c1a9b12d
SL
3352E0391: r##"
3353This error indicates that some types or traits depend on each other
3354and therefore cannot be constructed.
3355
3356The following example contains a circular dependency between two traits:
3357
7453a54e 3358```compile_fail
c1a9b12d
SL
3359trait FirstTrait : SecondTrait {
3360
3361}
3362
3363trait SecondTrait : FirstTrait {
3364
3365}
3366```
3367"##,
3368
3369E0392: r##"
3370This error indicates that a type or lifetime parameter has been declared
7453a54e 3371but not actually used. Here is an example that demonstrates the error:
c1a9b12d 3372
7453a54e 3373```compile_fail
c1a9b12d
SL
3374enum Foo<T> {
3375 Bar
3376}
3377```
3378
3379If the type parameter was included by mistake, this error can be fixed
3380by simply removing the type parameter, as shown below:
3381
3382```
3383enum Foo {
3384 Bar
3385}
3386```
3387
3388Alternatively, if the type parameter was intentionally inserted, it must be
3389used. A simple fix is shown below:
3390
3391```
3392enum Foo<T> {
3393 Bar(T)
3394}
3395```
3396
3397This error may also commonly be found when working with unsafe code. For
3398example, when using raw pointers one may wish to specify the lifetime for
3399which the pointed-at data is valid. An initial attempt (below) causes this
3400error:
3401
7453a54e 3402```compile_fail
c1a9b12d
SL
3403struct Foo<'a, T> {
3404 x: *const T
3405}
3406```
3407
3408We want to express the constraint that Foo should not outlive `'a`, because
3409the data pointed to by `T` is only valid for that lifetime. The problem is
3410that there are no actual uses of `'a`. It's possible to work around this
3411by adding a PhantomData type to the struct, using it to tell the compiler
3412to act as if the struct contained a borrowed reference `&'a T`:
3413
3414```
3415use std::marker::PhantomData;
3416
3417struct Foo<'a, T: 'a> {
3418 x: *const T,
3419 phantom: PhantomData<&'a T>
3420}
3421```
3422
3423PhantomData can also be used to express information about unused type
3424parameters. You can read more about it in the API documentation:
3425
3426https://doc.rust-lang.org/std/marker/struct.PhantomData.html
e9174d1e
SL
3427"##,
3428
3429E0439: r##"
3430The length of the platform-intrinsic function `simd_shuffle`
3431wasn't specified. Erroneous code example:
3432
7453a54e
SL
3433```compile_fail
3434#![feature(platform_intrinsics)]
3435
e9174d1e
SL
3436extern "platform-intrinsic" {
3437 fn simd_shuffle<A,B>(a: A, b: A, c: [u32; 8]) -> B;
3438 // error: invalid `simd_shuffle`, needs length: `simd_shuffle`
3439}
3440```
3441
3442The `simd_shuffle` function needs the length of the array passed as
3443last parameter in its name. Example:
3444
3445```
7453a54e
SL
3446#![feature(platform_intrinsics)]
3447
e9174d1e
SL
3448extern "platform-intrinsic" {
3449 fn simd_shuffle8<A,B>(a: A, b: A, c: [u32; 8]) -> B;
3450}
3451```
3452"##,
3453
3454E0440: r##"
3455A platform-specific intrinsic function has the wrong number of type
3456parameters. Erroneous code example:
3457
7453a54e
SL
3458```compile_fail
3459#![feature(repr_simd)]
3460#![feature(platform_intrinsics)]
3461
e9174d1e
SL
3462#[repr(simd)]
3463struct f64x2(f64, f64);
3464
3465extern "platform-intrinsic" {
3466 fn x86_mm_movemask_pd<T>(x: f64x2) -> i32;
3467 // error: platform-specific intrinsic has wrong number of type
3468 // parameters
3469}
3470```
3471
3472Please refer to the function declaration to see if it corresponds
3473with yours. Example:
3474
3475```
7453a54e
SL
3476#![feature(repr_simd)]
3477#![feature(platform_intrinsics)]
3478
e9174d1e
SL
3479#[repr(simd)]
3480struct f64x2(f64, f64);
3481
3482extern "platform-intrinsic" {
3483 fn x86_mm_movemask_pd(x: f64x2) -> i32;
3484}
3485```
3486"##,
3487
3488E0441: r##"
3489An unknown platform-specific intrinsic function was used. Erroneous
3490code example:
3491
7453a54e
SL
3492```compile_fail
3493#![feature(repr_simd)]
3494#![feature(platform_intrinsics)]
3495
e9174d1e
SL
3496#[repr(simd)]
3497struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
3498
3499extern "platform-intrinsic" {
3500 fn x86_mm_adds_ep16(x: i16x8, y: i16x8) -> i16x8;
3501 // error: unrecognized platform-specific intrinsic function
3502}
3503```
3504
3505Please verify that the function name wasn't misspelled, and ensure
3506that it is declared in the rust source code (in the file
3507src/librustc_platform_intrinsics/x86.rs). Example:
3508
3509```
7453a54e
SL
3510#![feature(repr_simd)]
3511#![feature(platform_intrinsics)]
3512
e9174d1e
SL
3513#[repr(simd)]
3514struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
3515
3516extern "platform-intrinsic" {
3517 fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i16x8; // ok!
3518}
3519```
3520"##,
3521
3522E0442: r##"
3523Intrinsic argument(s) and/or return value have the wrong type.
3524Erroneous code example:
3525
7453a54e
SL
3526```compile_fail
3527#![feature(repr_simd)]
3528#![feature(platform_intrinsics)]
3529
e9174d1e
SL
3530#[repr(simd)]
3531struct i8x16(i8, i8, i8, i8, i8, i8, i8, i8,
3532 i8, i8, i8, i8, i8, i8, i8, i8);
3533#[repr(simd)]
3534struct i32x4(i32, i32, i32, i32);
3535#[repr(simd)]
3536struct i64x2(i64, i64);
3537
3538extern "platform-intrinsic" {
3539 fn x86_mm_adds_epi16(x: i8x16, y: i32x4) -> i64x2;
3540 // error: intrinsic arguments/return value have wrong type
3541}
3542```
3543
3544To fix this error, please refer to the function declaration to give
3545it the awaited types. Example:
3546
3547```
7453a54e
SL
3548#![feature(repr_simd)]
3549#![feature(platform_intrinsics)]
3550
e9174d1e
SL
3551#[repr(simd)]
3552struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
3553
3554extern "platform-intrinsic" {
3555 fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i16x8; // ok!
3556}
3557```
3558"##,
3559
3560E0443: r##"
3561Intrinsic argument(s) and/or return value have the wrong type.
3562Erroneous code example:
3563
7453a54e
SL
3564```compile_fail
3565#![feature(repr_simd)]
3566#![feature(platform_intrinsics)]
3567
e9174d1e
SL
3568#[repr(simd)]
3569struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
3570#[repr(simd)]
3571struct i64x8(i64, i64, i64, i64, i64, i64, i64, i64);
3572
3573extern "platform-intrinsic" {
3574 fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i64x8;
3575 // error: intrinsic argument/return value has wrong type
3576}
3577```
3578
3579To fix this error, please refer to the function declaration to give
3580it the awaited types. Example:
3581
3582```
7453a54e
SL
3583#![feature(repr_simd)]
3584#![feature(platform_intrinsics)]
3585
e9174d1e
SL
3586#[repr(simd)]
3587struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
3588
3589extern "platform-intrinsic" {
3590 fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i16x8; // ok!
3591}
3592```
3593"##,
3594
3595E0444: r##"
3596A platform-specific intrinsic function has wrong number of arguments.
3597Erroneous code example:
3598
7453a54e
SL
3599```compile_fail
3600#![feature(repr_simd)]
3601#![feature(platform_intrinsics)]
3602
e9174d1e
SL
3603#[repr(simd)]
3604struct f64x2(f64, f64);
3605
3606extern "platform-intrinsic" {
3607 fn x86_mm_movemask_pd(x: f64x2, y: f64x2, z: f64x2) -> i32;
3608 // error: platform-specific intrinsic has invalid number of arguments
3609}
3610```
3611
3612Please refer to the function declaration to see if it corresponds
3613with yours. Example:
3614
3615```
7453a54e
SL
3616#![feature(repr_simd)]
3617#![feature(platform_intrinsics)]
3618
e9174d1e
SL
3619#[repr(simd)]
3620struct f64x2(f64, f64);
3621
3622extern "platform-intrinsic" {
3623 fn x86_mm_movemask_pd(x: f64x2) -> i32; // ok!
3624}
3625```
3626"##,
d9579d0f 3627
b039eaaf
SL
3628E0516: r##"
3629The `typeof` keyword is currently reserved but unimplemented.
3630Erroneous code example:
3631
7453a54e 3632```compile_fail
b039eaaf
SL
3633fn main() {
3634 let x: typeof(92) = 92;
3635}
3636```
3637
3638Try using type inference instead. Example:
3639
3640```
3641fn main() {
3642 let x = 92;
3643}
3644```
3645"##,
3646
54a0048b
SL
3647E0520: r##"
3648A non-default implementation was already made on this type so it cannot be
3649specialized further. Erroneous code example:
3650
3651```compile_fail
3652#![feature(specialization)]
3653
3654trait SpaceLlama {
3655 fn fly(&self);
3656}
3657
3658// applies to all T
3659impl<T> SpaceLlama for T {
3660 default fn fly(&self) {}
3661}
3662
3663// non-default impl
3664// applies to all `Clone` T and overrides the previous impl
3665impl<T: Clone> SpaceLlama for T {
3666 fn fly(&self) {}
3667}
3668
3669// since `i32` is clone, this conflicts with the previous implementation
3670impl SpaceLlama for i32 {
3671 default fn fly(&self) {}
3672 // error: item `fly` is provided by an `impl` that specializes
3673 // another, but the item in the parent `impl` is not marked
3674 // `default` and so it cannot be specialized.
3675}
3676```
3677
3678Specialization only allows you to override `default` functions in
3679implementations.
3680
3681To fix this error, you need to mark all the parent implementations as default.
3682Example:
3683
3684```
3685#![feature(specialization)]
3686
3687trait SpaceLlama {
3688 fn fly(&self);
3689}
3690
3691// applies to all T
3692impl<T> SpaceLlama for T {
3693 default fn fly(&self) {} // This is a parent implementation.
3694}
3695
3696// applies to all `Clone` T; overrides the previous impl
3697impl<T: Clone> SpaceLlama for T {
3698 default fn fly(&self) {} // This is a parent implementation but was
3699 // previously not a default one, causing the error
3700}
3701
3702// applies to i32, overrides the previous two impls
3703impl SpaceLlama for i32 {
3704 fn fly(&self) {} // And now that's ok!
3705}
3706```
3707"##,
3708
d9579d0f
AL
3709}
3710
1a4d82fc 3711register_diagnostics! {
e9174d1e
SL
3712// E0068,
3713// E0085,
3714// E0086,
1a4d82fc 3715 E0090,
e9174d1e 3716 E0103, // @GuillaumeGomez: I was unable to get this error, try your best!
1a4d82fc 3717 E0104,
e9174d1e
SL
3718// E0123,
3719// E0127,
3720// E0129,
3721// E0141,
3722// E0159, // use of trait `{}` as struct constructor
1a4d82fc 3723 E0167,
e9174d1e
SL
3724// E0168,
3725// E0173, // manual implementations of unboxed closure traits are experimental
1a4d82fc 3726 E0174, // explicit use of unboxed closure methods are experimental
1a4d82fc
JJ
3727 E0182,
3728 E0183,
e9174d1e 3729// E0187, // can't infer the kind of the closure
b039eaaf 3730// E0188, // can not cast an immutable reference to a mutable pointer
e9174d1e
SL
3731// E0189, // deprecated: can only cast a boxed pointer to a boxed object
3732// E0190, // deprecated: can only cast a &-pointer to an &-object
85aaf69f 3733 E0196, // cannot determine a type for this closure
85aaf69f
SL
3734 E0203, // type parameter has more than one relaxed default bound,
3735 // and only one is supported
85aaf69f 3736 E0208,
e9174d1e 3737// E0209, // builtin traits can only be implemented on structs or enums
85aaf69f 3738 E0212, // cannot extract an associated type from a higher-ranked trait bound
e9174d1e 3739// E0213, // associated types are not accepted in this context
c1a9b12d
SL
3740// E0215, // angle-bracket notation is not stable with `Fn`
3741// E0216, // parenthetical notation is only stable with `Fn`
e9174d1e
SL
3742// E0217, // ambiguous associated type, defined in multiple supertraits
3743// E0218, // no associated type defined
3744// E0219, // associated type defined in higher-ranked supertrait
c1a9b12d
SL
3745// E0222, // Error code E0045 (variadic function must have C calling
3746 // convention) duplicate
85aaf69f 3747 E0224, // at least one non-builtin train is required for an object type
85aaf69f
SL
3748 E0226, // only a single explicit lifetime bound is permitted
3749 E0227, // ambiguous lifetime bound, explicit lifetime bound required
3750 E0228, // explicit lifetime bound required
85aaf69f
SL
3751 E0230, // there is no type parameter on trait
3752 E0231, // only named substitution parameters are allowed
e9174d1e
SL
3753// E0233,
3754// E0234,
3755// E0235, // structure constructor specifies a structure of type but
54a0048b
SL
3756// E0236, // no lang item for range syntax
3757// E0237, // no lang item for range syntax
85aaf69f 3758 E0238, // parenthesized parameters may only be used with a trait
e9174d1e
SL
3759// E0239, // `next` method of `Iterator` trait has unexpected type
3760// E0240,
3761// E0241,
85aaf69f 3762 E0242, // internal error looking up a definition
85aaf69f 3763 E0245, // not a trait
e9174d1e 3764// E0246, // invalid recursive type
7453a54e 3765// E0247,
e9174d1e 3766// E0319, // trait impls for defaulted traits allowed just for structs/enums
c34b1796 3767 E0320, // recursive overflow during dropck
d9579d0f 3768 E0328, // cannot implement Unsize explicitly
9cc50fc6 3769// E0372, // coherence not object safe
d9579d0f
AL
3770 E0374, // the trait `CoerceUnsized` may only be implemented for a coercion
3771 // between structures with one field being coerced, none found
3772 E0375, // the trait `CoerceUnsized` may only be implemented for a coercion
3773 // between structures with one field being coerced, but multiple
3774 // fields need coercions
3775 E0376, // the trait `CoerceUnsized` may only be implemented for a coercion
3776 // between structures
62682a34 3777 E0377, // the trait `CoerceUnsized` may only be implemented for a coercion
d9579d0f 3778 // between structures with the same definition
c1a9b12d 3779 E0393, // the type parameter `{}` must be explicitly specified in an object
62682a34 3780 // type because its default value `{}` references the type `Self`"
c1a9b12d
SL
3781 E0399, // trait items need to be implemented because the associated
3782 // type `{}` was overridden
b039eaaf 3783 E0436, // functional record update requires a struct
54a0048b
SL
3784 E0513, // no type for local variable ..
3785 E0521 // redundant default implementations of trait
1a4d82fc 3786}