]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/error_codes.rs
Rollup merge of #64404 - GuillaumeGomez:err-E0495, r=Dylan-DPC
[rust.git] / src / librustc_passes / error_codes.rs
1 syntax::register_diagnostics! {
2 /*
3 E0014: r##"
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:
8
9 ```compile_fail
10 const FOO: i32 = { let x = 0; x }; // 'x' isn't a constant nor a function!
11 ```
12
13 To avoid it, you have to replace the non-constant value:
14
15 ```
16 const FOO: i32 = { const X : i32 = 0; X };
17 // or even:
18 const FOO2: i32 = { 0 }; // but brackets are useless here
19 ```
20 "##,
21 */
22
23 E0130: r##"
24 You declared a pattern as an argument in a foreign function declaration.
25 Erroneous code example:
26
27 ```compile_fail
28 extern {
29     fn foo((a, b): (u32, u32)); // error: patterns aren't allowed in foreign
30                                 //        function declarations
31 }
32 ```
33
34 Please replace the pattern argument with a regular one. Example:
35
36 ```
37 struct SomeStruct {
38     a: u32,
39     b: u32,
40 }
41
42 extern {
43     fn foo(s: SomeStruct); // ok!
44 }
45 ```
46
47 Or:
48
49 ```
50 extern {
51     fn foo(a: (u32, u32)); // ok!
52 }
53 ```
54 "##,
55
56 // This shouldn't really ever trigger since the repeated value error comes first
57 E0136: r##"
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.
60 "##,
61
62 E0137: r##"
63 More than one function was declared with the `#[main]` attribute.
64
65 Erroneous code example:
66
67 ```compile_fail,E0137
68 #![feature(main)]
69
70 #[main]
71 fn foo() {}
72
73 #[main]
74 fn f() {} // error: multiple functions with a `#[main]` attribute
75 ```
76
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:
80
81 ```
82 #![feature(main)]
83
84 #[main]
85 fn f() {} // ok!
86 ```
87 "##,
88
89 E0138: r##"
90 More than one function was declared with the `#[start]` attribute.
91
92 Erroneous code example:
93
94 ```compile_fail,E0138
95 #![feature(start)]
96
97 #[start]
98 fn foo(argc: isize, argv: *const *const u8) -> isize {}
99
100 #[start]
101 fn f(argc: isize, argv: *const *const u8) -> isize {}
102 // error: multiple 'start' functions
103 ```
104
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:
108
109 ```
110 #![feature(start)]
111
112 #[start]
113 fn foo(argc: isize, argv: *const *const u8) -> isize { 0 } // ok!
114 ```
115 "##,
116
117 E0197: r##"
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.
122
123 ```compile_fail,E0197
124 struct Foo;
125
126 // this will cause this error
127 unsafe impl Foo { }
128 // converting it to this will fix it
129 impl Foo { }
130 ```
131 "##,
132
133 E0198: r##"
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
137 unsafe.
138
139 ```compile_fail
140 #![feature(optin_builtin_traits)]
141
142 struct Foo;
143
144 // unsafe is unnecessary
145 unsafe impl !Clone for Foo { }
146 ```
147
148 This will compile:
149
150 ```ignore (ignore auto_trait future compatibility warning)
151 #![feature(optin_builtin_traits)]
152
153 struct Foo;
154
155 auto trait Enterprise {}
156
157 impl !Enterprise for Foo { }
158 ```
159
160 Please note that negative impls are only allowed for auto traits.
161 "##,
162
163 E0267: r##"
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:
166
167 ```compile_fail,E0267
168 let w = || { break; }; // error: `break` inside of a closure
169 ```
170
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:
174
175 ```
176 let w = || {
177     for _ in 0..10 {
178         break;
179     }
180 };
181
182 w();
183 ```
184 "##,
185
186 E0268: r##"
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:
190
191 ```compile_fail,E0268
192 fn some_func() {
193     break; // error: `break` outside of a loop
194 }
195 ```
196
197 Please verify that you are using `break` and `continue` only in loops. Example:
198
199 ```
200 fn some_func() {
201     for _ in 0..10 {
202         break; // ok!
203     }
204 }
205 ```
206 "##,
207
208 E0379: r##"
209 Trait methods cannot be declared `const` by design. For more information, see
210 [RFC 911].
211
212 [RFC 911]: https://github.com/rust-lang/rfcs/pull/911
213 "##,
214
215 E0380: r##"
216 Auto traits cannot have methods or associated items.
217 For more information see the [opt-in builtin traits RFC][RFC 19].
218
219 [RFC 19]: https://github.com/rust-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md
220 "##,
221
222 E0449: r##"
223 A visibility qualifier was used when it was unnecessary. Erroneous code
224 examples:
225
226 ```compile_fail,E0449
227 struct Bar;
228
229 trait Foo {
230     fn foo();
231 }
232
233 pub impl Bar {} // error: unnecessary visibility qualifier
234
235 pub impl Foo for Bar { // error: unnecessary visibility qualifier
236     pub fn foo() {} // error: unnecessary visibility qualifier
237 }
238 ```
239
240 To fix this error, please remove the visibility qualifier when it is not
241 required. Example:
242
243 ```
244 struct Bar;
245
246 trait Foo {
247     fn foo();
248 }
249
250 // Directly implemented methods share the visibility of the type itself,
251 // so `pub` is unnecessary here
252 impl Bar {}
253
254 // Trait methods share the visibility of the trait, so `pub` is
255 // unnecessary in either case
256 impl Foo for Bar {
257     fn foo() {}
258 }
259 ```
260 "##,
261
262 E0512: r##"
263 Transmute with two differently sized types was attempted. Erroneous code
264 example:
265
266 ```compile_fail,E0512
267 fn takes_u8(_: u8) {}
268
269 fn main() {
270     unsafe { takes_u8(::std::mem::transmute(0u16)); }
271     // error: cannot transmute between types of different sizes,
272     //        or dependently-sized types
273 }
274 ```
275
276 Please use types with same size or use the expected type directly. Example:
277
278 ```
279 fn takes_u8(_: u8) {}
280
281 fn main() {
282     unsafe { takes_u8(::std::mem::transmute(0i8)); } // ok!
283     // or:
284     unsafe { takes_u8(0u8); } // ok!
285 }
286 ```
287 "##,
288
289 E0561: r##"
290 A non-ident or non-wildcard pattern has been used as a parameter of a function
291 pointer type.
292
293 Erroneous code example:
294
295 ```compile_fail,E0561
296 type A1 = fn(mut param: u8); // error!
297 type A2 = fn(&param: u32); // error!
298 ```
299
300 When using an alias over a function type, you cannot e.g. denote a parameter as
301 being mutable.
302
303 To fix the issue, remove patterns (`_` is allowed though). Example:
304
305 ```
306 type A1 = fn(param: u8); // ok!
307 type A2 = fn(_: u32); // ok!
308 ```
309
310 You can also omit the parameter name:
311
312 ```
313 type A3 = fn(i16); // ok!
314 ```
315 "##,
316
317 E0571: r##"
318 A `break` statement with an argument appeared in a non-`loop` loop.
319
320 Example of erroneous code:
321
322 ```compile_fail,E0571
323 # let mut i = 1;
324 # fn satisfied(n: usize) -> bool { n % 23 == 0 }
325 let result = while true {
326     if satisfied(i) {
327         break 2*i; // error: `break` with value from a `while` loop
328     }
329     i += 1;
330 };
331 ```
332
333 The `break` statement can take an argument (which will be the value of the loop
334 expression if the `break` statement is executed) in `loop` loops, but not
335 `for`, `while`, or `while let` loops.
336
337 Make sure `break value;` statements only occur in `loop` loops:
338
339 ```
340 # let mut i = 1;
341 # fn satisfied(n: usize) -> bool { n % 23 == 0 }
342 let result = loop { // ok!
343     if satisfied(i) {
344         break 2*i;
345     }
346     i += 1;
347 };
348 ```
349 "##,
350
351 E0590: r##"
352 `break` or `continue` must include a label when used in the condition of a
353 `while` loop.
354
355 Example of erroneous code:
356
357 ```compile_fail
358 while break {}
359 ```
360
361 To fix this, add a label specifying which loop is being broken out of:
362 ```
363 'foo: while break 'foo {}
364 ```
365 "##,
366
367 E0591: r##"
368 Per [RFC 401][rfc401], if you have a function declaration `foo`:
369
370 ```
371 // For the purposes of this explanation, all of these
372 // different kinds of `fn` declarations are equivalent:
373 struct S;
374 fn foo(x: S) { /* ... */ }
375 # #[cfg(for_demonstration_only)]
376 extern "C" { fn foo(x: S); }
377 # #[cfg(for_demonstration_only)]
378 impl S { fn foo(self) { /* ... */ } }
379 ```
380
381 the type of `foo` is **not** `fn(S)`, as one might expect.
382 Rather, it is a unique, zero-sized marker type written here as `typeof(foo)`.
383 However, `typeof(foo)` can be _coerced_ to a function pointer `fn(S)`,
384 so you rarely notice this:
385
386 ```
387 # struct S;
388 # fn foo(_: S) {}
389 let x: fn(S) = foo; // OK, coerces
390 ```
391
392 The reason that this matter is that the type `fn(S)` is not specific to
393 any particular function: it's a function _pointer_. So calling `x()` results
394 in a virtual call, whereas `foo()` is statically dispatched, because the type
395 of `foo` tells us precisely what function is being called.
396
397 As noted above, coercions mean that most code doesn't have to be
398 concerned with this distinction. However, you can tell the difference
399 when using **transmute** to convert a fn item into a fn pointer.
400
401 This is sometimes done as part of an FFI:
402
403 ```compile_fail,E0591
404 extern "C" fn foo(userdata: Box<i32>) {
405     /* ... */
406 }
407
408 # fn callback(_: extern "C" fn(*mut i32)) {}
409 # use std::mem::transmute;
410 # unsafe {
411 let f: extern "C" fn(*mut i32) = transmute(foo);
412 callback(f);
413 # }
414 ```
415
416 Here, transmute is being used to convert the types of the fn arguments.
417 This pattern is incorrect because, because the type of `foo` is a function
418 **item** (`typeof(foo)`), which is zero-sized, and the target type (`fn()`)
419 is a function pointer, which is not zero-sized.
420 This pattern should be rewritten. There are a few possible ways to do this:
421
422 - change the original fn declaration to match the expected signature,
423   and do the cast in the fn body (the preferred option)
424 - cast the fn item fo a fn pointer before calling transmute, as shown here:
425
426     ```
427     # extern "C" fn foo(_: Box<i32>) {}
428     # use std::mem::transmute;
429     # unsafe {
430     let f: extern "C" fn(*mut i32) = transmute(foo as extern "C" fn(_));
431     let f: extern "C" fn(*mut i32) = transmute(foo as usize); // works too
432     # }
433     ```
434
435 The same applies to transmutes to `*mut fn()`, which were observed in practice.
436 Note though that use of this type is generally incorrect.
437 The intention is typically to describe a function pointer, but just `fn()`
438 alone suffices for that. `*mut fn()` is a pointer to a fn pointer.
439 (Since these values are typically just passed to C code, however, this rarely
440 makes a difference in practice.)
441
442 [rfc401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
443 "##,
444
445 E0601: r##"
446 No `main` function was found in a binary crate. To fix this error, add a
447 `main` function. For example:
448
449 ```
450 fn main() {
451     // Your program will start here.
452     println!("Hello world!");
453 }
454 ```
455
456 If you don't know the basics of Rust, you can go look to the Rust Book to get
457 started: https://doc.rust-lang.org/book/
458 "##,
459
460 E0642: r##"
461 Trait methods currently cannot take patterns as arguments.
462
463 Example of erroneous code:
464
465 ```compile_fail,E0642
466 trait Foo {
467     fn foo((x, y): (i32, i32)); // error: patterns aren't allowed
468                                 //        in trait methods
469 }
470 ```
471
472 You can instead use a single name for the argument:
473
474 ```
475 trait Foo {
476     fn foo(x_and_y: (i32, i32)); // ok!
477 }
478 ```
479 "##,
480
481 E0695: r##"
482 A `break` statement without a label appeared inside a labeled block.
483
484 Example of erroneous code:
485
486 ```compile_fail,E0695
487 # #![feature(label_break_value)]
488 loop {
489     'a: {
490         break;
491     }
492 }
493 ```
494
495 Make sure to always label the `break`:
496
497 ```
498 # #![feature(label_break_value)]
499 'l: loop {
500     'a: {
501         break 'l;
502     }
503 }
504 ```
505
506 Or if you want to `break` the labeled block:
507
508 ```
509 # #![feature(label_break_value)]
510 loop {
511     'a: {
512         break 'a;
513     }
514     break;
515 }
516 ```
517 "##,
518
519 E0670: r##"
520 Rust 2015 does not permit the use of `async fn`.
521
522 Example of erroneous code:
523
524 ```compile_fail,E0670
525 async fn foo() {}
526 ```
527
528 Switch to the Rust 2018 edition to use `async fn`.
529 "##,
530
531 ;
532     E0226, // only a single explicit lifetime bound is permitted
533     E0472, // asm! is unsupported on this target
534     E0567, // auto traits can not have generic parameters
535     E0568, // auto traits can not have super traits
536     E0666, // nested `impl Trait` is illegal
537     E0667, // `impl Trait` in projections
538     E0696, // `continue` pointing to a labeled block
539     E0706, // `async fn` in trait
540 }