]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/error_codes.rs
Rollup merge of #65133 - davidtwco:issue-65035-static-with-generic-in-foreign-mod...
[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 E0567: r##"
318 Generics have been used on an auto trait.
319
320 Erroneous code example:
321
322 ```compile_fail,E0567
323 #![feature(optin_builtin_traits)]
324
325 auto trait Generic<T> {} // error!
326
327 fn main() {}
328 ```
329
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
332 parameters.
333
334 To fix this issue, just remove the generics:
335
336 ```
337 #![feature(optin_builtin_traits)]
338
339 auto trait Generic {} // ok!
340
341 fn main() {}
342 ```
343 "##,
344
345 E0571: r##"
346 A `break` statement with an argument appeared in a non-`loop` loop.
347
348 Example of erroneous code:
349
350 ```compile_fail,E0571
351 # let mut i = 1;
352 # fn satisfied(n: usize) -> bool { n % 23 == 0 }
353 let result = while true {
354     if satisfied(i) {
355         break 2*i; // error: `break` with value from a `while` loop
356     }
357     i += 1;
358 };
359 ```
360
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.
364
365 Make sure `break value;` statements only occur in `loop` loops:
366
367 ```
368 # let mut i = 1;
369 # fn satisfied(n: usize) -> bool { n % 23 == 0 }
370 let result = loop { // ok!
371     if satisfied(i) {
372         break 2*i;
373     }
374     i += 1;
375 };
376 ```
377 "##,
378
379 E0590: r##"
380 `break` or `continue` must include a label when used in the condition of a
381 `while` loop.
382
383 Example of erroneous code:
384
385 ```compile_fail
386 while break {}
387 ```
388
389 To fix this, add a label specifying which loop is being broken out of:
390 ```
391 'foo: while break 'foo {}
392 ```
393 "##,
394
395 E0591: r##"
396 Per [RFC 401][rfc401], if you have a function declaration `foo`:
397
398 ```
399 // For the purposes of this explanation, all of these
400 // different kinds of `fn` declarations are equivalent:
401 struct S;
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) { /* ... */ } }
407 ```
408
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:
413
414 ```
415 # struct S;
416 # fn foo(_: S) {}
417 let x: fn(S) = foo; // OK, coerces
418 ```
419
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.
424
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.
428
429 This is sometimes done as part of an FFI:
430
431 ```compile_fail,E0591
432 extern "C" fn foo(userdata: Box<i32>) {
433     /* ... */
434 }
435
436 # fn callback(_: extern "C" fn(*mut i32)) {}
437 # use std::mem::transmute;
438 # unsafe {
439 let f: extern "C" fn(*mut i32) = transmute(foo);
440 callback(f);
441 # }
442 ```
443
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:
449
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:
453
454     ```
455     # extern "C" fn foo(_: Box<i32>) {}
456     # use std::mem::transmute;
457     # unsafe {
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
460     # }
461     ```
462
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.)
469
470 [rfc401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
471 "##,
472
473 E0601: r##"
474 No `main` function was found in a binary crate. To fix this error, add a
475 `main` function. For example:
476
477 ```
478 fn main() {
479     // Your program will start here.
480     println!("Hello world!");
481 }
482 ```
483
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/
486 "##,
487
488 E0642: r##"
489 Trait methods currently cannot take patterns as arguments.
490
491 Example of erroneous code:
492
493 ```compile_fail,E0642
494 trait Foo {
495     fn foo((x, y): (i32, i32)); // error: patterns aren't allowed
496                                 //        in trait methods
497 }
498 ```
499
500 You can instead use a single name for the argument:
501
502 ```
503 trait Foo {
504     fn foo(x_and_y: (i32, i32)); // ok!
505 }
506 ```
507 "##,
508
509 E0695: r##"
510 A `break` statement without a label appeared inside a labeled block.
511
512 Example of erroneous code:
513
514 ```compile_fail,E0695
515 # #![feature(label_break_value)]
516 loop {
517     'a: {
518         break;
519     }
520 }
521 ```
522
523 Make sure to always label the `break`:
524
525 ```
526 # #![feature(label_break_value)]
527 'l: loop {
528     'a: {
529         break 'l;
530     }
531 }
532 ```
533
534 Or if you want to `break` the labeled block:
535
536 ```
537 # #![feature(label_break_value)]
538 loop {
539     'a: {
540         break 'a;
541     }
542     break;
543 }
544 ```
545 "##,
546
547 E0670: r##"
548 Rust 2015 does not permit the use of `async fn`.
549
550 Example of erroneous code:
551
552 ```compile_fail,E0670
553 async fn foo() {}
554 ```
555
556 Switch to the Rust 2018 edition to use `async fn`.
557 "##,
558
559 ;
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
567 }