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!
57 Inherent implementations (one that do not implement a trait but provide
58 methods associated with a type) are always safe because they are not
59 implementing an unsafe trait. Removing the `unsafe` keyword from the inherent
60 implementation will resolve this error.
65 // this will cause this error
67 // converting it to this will fix it
73 A negative implementation is one that excludes a type from implementing a
74 particular trait. Not being able to use a trait is always a safe operation,
75 so negative implementations are always safe and never need to be marked as
79 #![feature(optin_builtin_traits)]
83 // unsafe is unnecessary
84 unsafe impl !Clone for Foo { }
89 ```ignore (ignore auto_trait future compatibility warning)
90 #![feature(optin_builtin_traits)]
94 auto trait Enterprise {}
96 impl !Enterprise for Foo { }
99 Please note that negative impls are only allowed for auto traits.
103 This error indicates the use of a loop keyword (`break` or `continue`) inside a
104 closure but outside of any loop. Erroneous code example:
106 ```compile_fail,E0267
107 let w = || { break; }; // error: `break` inside of a closure
110 `break` and `continue` keywords can be used as normal inside closures as long as
111 they are also contained within a loop. To halt the execution of a closure you
112 should instead use a return statement. Example:
126 This error indicates the use of a loop keyword (`break` or `continue`) outside
127 of a loop. Without a loop to break out of or continue in, no sensible action can
128 be taken. Erroneous code example:
130 ```compile_fail,E0268
132 break; // error: `break` outside of a loop
136 Please verify that you are using `break` and `continue` only in loops. Example:
148 Trait methods cannot be declared `const` by design. For more information, see
151 [RFC 911]: https://github.com/rust-lang/rfcs/pull/911
155 Auto traits cannot have methods or associated items.
156 For more information see the [opt-in builtin traits RFC][RFC 19].
158 [RFC 19]: https://github.com/rust-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md
162 A visibility qualifier was used when it was unnecessary. Erroneous code
165 ```compile_fail,E0449
172 pub impl Bar {} // error: unnecessary visibility qualifier
174 pub impl Foo for Bar { // error: unnecessary visibility qualifier
175 pub fn foo() {} // error: unnecessary visibility qualifier
179 To fix this error, please remove the visibility qualifier when it is not
189 // Directly implemented methods share the visibility of the type itself,
190 // so `pub` is unnecessary here
193 // Trait methods share the visibility of the trait, so `pub` is
194 // unnecessary in either case
203 `break` or `continue` must include a label when used in the condition of a
206 Example of erroneous code:
212 To fix this, add a label specifying which loop is being broken out of:
214 'foo: while break 'foo {}
219 A `break` statement with an argument appeared in a non-`loop` loop.
221 Example of erroneous code:
223 ```compile_fail,E0571
225 # fn satisfied(n: usize) -> bool { n % 23 == 0 }
226 let result = while true {
228 break 2*i; // error: `break` with value from a `while` loop
234 The `break` statement can take an argument (which will be the value of the loop
235 expression if the `break` statement is executed) in `loop` loops, but not
236 `for`, `while`, or `while let` loops.
238 Make sure `break value;` statements only occur in `loop` loops:
242 # fn satisfied(n: usize) -> bool { n % 23 == 0 }
243 let result = loop { // ok!
253 Trait methods currently cannot take patterns as arguments.
255 Example of erroneous code:
257 ```compile_fail,E0642
259 fn foo((x, y): (i32, i32)); // error: patterns aren't allowed
264 You can instead use a single name for the argument:
268 fn foo(x_and_y: (i32, i32)); // ok!
274 A `break` statement without a label appeared inside a labeled block.
276 Example of erroneous code:
278 ```compile_fail,E0695
279 # #![feature(label_break_value)]
287 Make sure to always label the `break`:
290 # #![feature(label_break_value)]
298 Or if you want to `break` the labeled block:
301 # #![feature(label_break_value)]
312 Rust 2015 does not permit the use of `async fn`.
314 Example of erroneous code:
316 ```compile_fail,E0670
320 Switch to the Rust 2018 edition to use `async fn`.
323 // This shouldn't really ever trigger since the repeated value error comes first
325 A binary can only have one entry point, and by default that entry point is the
326 function `main()`. If there are multiple such functions, please rename one.
330 More than one function was declared with the `#[main]` attribute.
332 Erroneous code example:
334 ```compile_fail,E0137
341 fn f() {} // error: multiple functions with a `#[main]` attribute
344 This error indicates that the compiler found multiple functions with the
345 `#[main]` attribute. This is an error because there must be a unique entry
346 point into a Rust program. Example:
357 More than one function was declared with the `#[start]` attribute.
359 Erroneous code example:
361 ```compile_fail,E0138
365 fn foo(argc: isize, argv: *const *const u8) -> isize {}
368 fn f(argc: isize, argv: *const *const u8) -> isize {}
369 // error: multiple 'start' functions
372 This error indicates that the compiler found multiple functions with the
373 `#[start]` attribute. This is an error because there must be a unique entry
374 point into a Rust program. Example:
380 fn foo(argc: isize, argv: *const *const u8) -> isize { 0 } // ok!
385 No `main` function was found in a binary crate. To fix this error, add a
386 `main` function. For example:
390 // Your program will start here.
391 println!("Hello world!");
395 If you don't know the basics of Rust, you can go look to the Rust Book to get
396 started: https://doc.rust-lang.org/book/
400 Per [RFC 401][rfc401], if you have a function declaration `foo`:
403 // For the purposes of this explanation, all of these
404 // different kinds of `fn` declarations are equivalent:
406 fn foo(x: S) { /* ... */ }
407 # #[cfg(for_demonstration_only)]
408 extern "C" { fn foo(x: S); }
409 # #[cfg(for_demonstration_only)]
410 impl S { fn foo(self) { /* ... */ } }
413 the type of `foo` is **not** `fn(S)`, as one might expect.
414 Rather, it is a unique, zero-sized marker type written here as `typeof(foo)`.
415 However, `typeof(foo)` can be _coerced_ to a function pointer `fn(S)`,
416 so you rarely notice this:
421 let x: fn(S) = foo; // OK, coerces
424 The reason that this matter is that the type `fn(S)` is not specific to
425 any particular function: it's a function _pointer_. So calling `x()` results
426 in a virtual call, whereas `foo()` is statically dispatched, because the type
427 of `foo` tells us precisely what function is being called.
429 As noted above, coercions mean that most code doesn't have to be
430 concerned with this distinction. However, you can tell the difference
431 when using **transmute** to convert a fn item into a fn pointer.
433 This is sometimes done as part of an FFI:
435 ```compile_fail,E0591
436 extern "C" fn foo(userdata: Box<i32>) {
440 # fn callback(_: extern "C" fn(*mut i32)) {}
441 # use std::mem::transmute;
443 let f: extern "C" fn(*mut i32) = transmute(foo);
448 Here, transmute is being used to convert the types of the fn arguments.
449 This pattern is incorrect because, because the type of `foo` is a function
450 **item** (`typeof(foo)`), which is zero-sized, and the target type (`fn()`)
451 is a function pointer, which is not zero-sized.
452 This pattern should be rewritten. There are a few possible ways to do this:
454 - change the original fn declaration to match the expected signature,
455 and do the cast in the fn body (the preferred option)
456 - cast the fn item fo a fn pointer before calling transmute, as shown here:
459 # extern "C" fn foo(_: Box<i32>) {}
460 # use std::mem::transmute;
462 let f: extern "C" fn(*mut i32) = transmute(foo as extern "C" fn(_));
463 let f: extern "C" fn(*mut i32) = transmute(foo as usize); // works too
467 The same applies to transmutes to `*mut fn()`, which were observed in practice.
468 Note though that use of this type is generally incorrect.
469 The intention is typically to describe a function pointer, but just `fn()`
470 alone suffices for that. `*mut fn()` is a pointer to a fn pointer.
471 (Since these values are typically just passed to C code, however, this rarely
472 makes a difference in practice.)
474 [rfc401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
478 Transmute with two differently sized types was attempted. Erroneous code
481 ```compile_fail,E0512
482 fn takes_u8(_: u8) {}
485 unsafe { takes_u8(::std::mem::transmute(0u16)); }
486 // error: cannot transmute between types of different sizes,
487 // or dependently-sized types
491 Please use types with same size or use the expected type directly. Example:
494 fn takes_u8(_: u8) {}
497 unsafe { takes_u8(::std::mem::transmute(0i8)); } // ok!
499 unsafe { takes_u8(0u8); } // ok!
505 E0226, // only a single explicit lifetime bound is permitted
506 E0472, // asm! is unsupported on this target
507 E0561, // patterns aren't allowed in function pointer types
508 E0567, // auto traits can not have generic parameters
509 E0568, // auto traits can not have super traits
510 E0666, // nested `impl Trait` is illegal
511 E0667, // `impl Trait` in projections
512 E0696, // `continue` pointing to a labeled block
513 E0706, // `async fn` in trait