1 syntax::register_diagnostics! {
4 Constants can only be initialized by a constant value or, in a future
5 version of Rust, a call to a const function. This error indicates the use
6 of a path (like a::b, or x) denoting something other than one of these
7 allowed items. Erroneous code xample:
10 const FOO: i32 = { let x = 0; x }; // 'x' isn't a constant nor a function!
13 To avoid it, you have to replace the non-constant value:
16 const FOO: i32 = { const X : i32 = 0; X };
18 const FOO2: i32 = { 0 }; // but brackets are useless here
24 You declared a pattern as an argument in a foreign function declaration.
25 Erroneous code example:
29 fn foo((a, b): (u32, u32)); // error: patterns aren't allowed in foreign
30 // function declarations
34 Please replace the pattern argument with a regular one. Example:
43 fn foo(s: SomeStruct); // ok!
51 fn foo(a: (u32, u32)); // ok!
56 // This shouldn't really ever trigger since the repeated value error comes first
58 A binary can only have one entry point, and by default that entry point is the
59 function `main()`. If there are multiple such functions, please rename one.
63 More than one function was declared with the `#[main]` attribute.
65 Erroneous code example:
74 fn f() {} // error: multiple functions with a `#[main]` attribute
77 This error indicates that the compiler found multiple functions with the
78 `#[main]` attribute. This is an error because there must be a unique entry
79 point into a Rust program. Example:
90 More than one function was declared with the `#[start]` attribute.
92 Erroneous code example:
98 fn foo(argc: isize, argv: *const *const u8) -> isize {}
101 fn f(argc: isize, argv: *const *const u8) -> isize {}
102 // error: multiple 'start' functions
105 This error indicates that the compiler found multiple functions with the
106 `#[start]` attribute. This is an error because there must be a unique entry
107 point into a Rust program. Example:
113 fn foo(argc: isize, argv: *const *const u8) -> isize { 0 } // ok!
118 Inherent implementations (one that do not implement a trait but provide
119 methods associated with a type) are always safe because they are not
120 implementing an unsafe trait. Removing the `unsafe` keyword from the inherent
121 implementation will resolve this error.
123 ```compile_fail,E0197
126 // this will cause this error
128 // converting it to this will fix it
134 A negative implementation is one that excludes a type from implementing a
135 particular trait. Not being able to use a trait is always a safe operation,
136 so negative implementations are always safe and never need to be marked as
140 #![feature(optin_builtin_traits)]
144 // unsafe is unnecessary
145 unsafe impl !Clone for Foo { }
150 ```ignore (ignore auto_trait future compatibility warning)
151 #![feature(optin_builtin_traits)]
155 auto trait Enterprise {}
157 impl !Enterprise for Foo { }
160 Please note that negative impls are only allowed for auto traits.
164 This error indicates the use of a loop keyword (`break` or `continue`) inside a
165 closure but outside of any loop. Erroneous code example:
167 ```compile_fail,E0267
168 let w = || { break; }; // error: `break` inside of a closure
171 `break` and `continue` keywords can be used as normal inside closures as long as
172 they are also contained within a loop. To halt the execution of a closure you
173 should instead use a return statement. Example:
187 This error indicates the use of a loop keyword (`break` or `continue`) outside
188 of a loop. Without a loop to break out of or continue in, no sensible action can
189 be taken. Erroneous code example:
191 ```compile_fail,E0268
193 break; // error: `break` outside of a loop
197 Please verify that you are using `break` and `continue` only in loops. Example:
209 Trait methods cannot be declared `const` by design. For more information, see
212 [RFC 911]: https://github.com/rust-lang/rfcs/pull/911
216 Auto traits cannot have methods or associated items.
217 For more information see the [opt-in builtin traits RFC][RFC 19].
219 [RFC 19]: https://github.com/rust-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md
223 A visibility qualifier was used when it was unnecessary. Erroneous code
226 ```compile_fail,E0449
233 pub impl Bar {} // error: unnecessary visibility qualifier
235 pub impl Foo for Bar { // error: unnecessary visibility qualifier
236 pub fn foo() {} // error: unnecessary visibility qualifier
240 To fix this error, please remove the visibility qualifier when it is not
250 // Directly implemented methods share the visibility of the type itself,
251 // so `pub` is unnecessary here
254 // Trait methods share the visibility of the trait, so `pub` is
255 // unnecessary in either case
263 Transmute with two differently sized types was attempted. Erroneous code
266 ```compile_fail,E0512
267 fn takes_u8(_: u8) {}
270 unsafe { takes_u8(::std::mem::transmute(0u16)); }
271 // error: cannot transmute between types of different sizes,
272 // or dependently-sized types
276 Please use types with same size or use the expected type directly. Example:
279 fn takes_u8(_: u8) {}
282 unsafe { takes_u8(::std::mem::transmute(0i8)); } // ok!
284 unsafe { takes_u8(0u8); } // ok!
290 A non-ident or non-wildcard pattern has been used as a parameter of a function
293 Erroneous code example:
295 ```compile_fail,E0561
296 type A1 = fn(mut param: u8); // error!
297 type A2 = fn(¶m: u32); // error!
300 When using an alias over a function type, you cannot e.g. denote a parameter as
303 To fix the issue, remove patterns (`_` is allowed though). Example:
306 type A1 = fn(param: u8); // ok!
307 type A2 = fn(_: u32); // ok!
310 You can also omit the parameter name:
313 type A3 = fn(i16); // ok!
318 Generics have been used on an auto trait.
320 Erroneous code example:
322 ```compile_fail,E0567
323 #![feature(optin_builtin_traits)]
325 auto trait Generic<T> {} // error!
330 Since an auto trait is implemented on all existing types, the
331 compiler would not be able to infer the types of the trait's generic
334 To fix this issue, just remove the generics:
337 #![feature(optin_builtin_traits)]
339 auto trait Generic {} // ok!
346 A `break` statement with an argument appeared in a non-`loop` loop.
348 Example of erroneous code:
350 ```compile_fail,E0571
352 # fn satisfied(n: usize) -> bool { n % 23 == 0 }
353 let result = while true {
355 break 2*i; // error: `break` with value from a `while` loop
361 The `break` statement can take an argument (which will be the value of the loop
362 expression if the `break` statement is executed) in `loop` loops, but not
363 `for`, `while`, or `while let` loops.
365 Make sure `break value;` statements only occur in `loop` loops:
369 # fn satisfied(n: usize) -> bool { n % 23 == 0 }
370 let result = loop { // ok!
380 `break` or `continue` must include a label when used in the condition of a
383 Example of erroneous code:
389 To fix this, add a label specifying which loop is being broken out of:
391 'foo: while break 'foo {}
396 Per [RFC 401][rfc401], if you have a function declaration `foo`:
399 // For the purposes of this explanation, all of these
400 // different kinds of `fn` declarations are equivalent:
402 fn foo(x: S) { /* ... */ }
403 # #[cfg(for_demonstration_only)]
404 extern "C" { fn foo(x: S); }
405 # #[cfg(for_demonstration_only)]
406 impl S { fn foo(self) { /* ... */ } }
409 the type of `foo` is **not** `fn(S)`, as one might expect.
410 Rather, it is a unique, zero-sized marker type written here as `typeof(foo)`.
411 However, `typeof(foo)` can be _coerced_ to a function pointer `fn(S)`,
412 so you rarely notice this:
417 let x: fn(S) = foo; // OK, coerces
420 The reason that this matter is that the type `fn(S)` is not specific to
421 any particular function: it's a function _pointer_. So calling `x()` results
422 in a virtual call, whereas `foo()` is statically dispatched, because the type
423 of `foo` tells us precisely what function is being called.
425 As noted above, coercions mean that most code doesn't have to be
426 concerned with this distinction. However, you can tell the difference
427 when using **transmute** to convert a fn item into a fn pointer.
429 This is sometimes done as part of an FFI:
431 ```compile_fail,E0591
432 extern "C" fn foo(userdata: Box<i32>) {
436 # fn callback(_: extern "C" fn(*mut i32)) {}
437 # use std::mem::transmute;
439 let f: extern "C" fn(*mut i32) = transmute(foo);
444 Here, transmute is being used to convert the types of the fn arguments.
445 This pattern is incorrect because, because the type of `foo` is a function
446 **item** (`typeof(foo)`), which is zero-sized, and the target type (`fn()`)
447 is a function pointer, which is not zero-sized.
448 This pattern should be rewritten. There are a few possible ways to do this:
450 - change the original fn declaration to match the expected signature,
451 and do the cast in the fn body (the preferred option)
452 - cast the fn item fo a fn pointer before calling transmute, as shown here:
455 # extern "C" fn foo(_: Box<i32>) {}
456 # use std::mem::transmute;
458 let f: extern "C" fn(*mut i32) = transmute(foo as extern "C" fn(_));
459 let f: extern "C" fn(*mut i32) = transmute(foo as usize); // works too
463 The same applies to transmutes to `*mut fn()`, which were observed in practice.
464 Note though that use of this type is generally incorrect.
465 The intention is typically to describe a function pointer, but just `fn()`
466 alone suffices for that. `*mut fn()` is a pointer to a fn pointer.
467 (Since these values are typically just passed to C code, however, this rarely
468 makes a difference in practice.)
470 [rfc401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
474 No `main` function was found in a binary crate. To fix this error, add a
475 `main` function. For example:
479 // Your program will start here.
480 println!("Hello world!");
484 If you don't know the basics of Rust, you can go look to the Rust Book to get
485 started: https://doc.rust-lang.org/book/
489 Trait methods currently cannot take patterns as arguments.
491 Example of erroneous code:
493 ```compile_fail,E0642
495 fn foo((x, y): (i32, i32)); // error: patterns aren't allowed
500 You can instead use a single name for the argument:
504 fn foo(x_and_y: (i32, i32)); // ok!
510 A `break` statement without a label appeared inside a labeled block.
512 Example of erroneous code:
514 ```compile_fail,E0695
515 # #![feature(label_break_value)]
523 Make sure to always label the `break`:
526 # #![feature(label_break_value)]
534 Or if you want to `break` the labeled block:
537 # #![feature(label_break_value)]
548 Rust 2015 does not permit the use of `async fn`.
550 Example of erroneous code:
552 ```compile_fail,E0670
556 Switch to the Rust 2018 edition to use `async fn`.
560 E0226, // only a single explicit lifetime bound is permitted
561 E0472, // asm! is unsupported on this target
562 E0568, // auto traits can not have super traits
563 E0666, // nested `impl Trait` is illegal
564 E0667, // `impl Trait` in projections
565 E0696, // `continue` pointing to a labeled block
566 E0706, // `async fn` in trait