]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/error_codes.rs
Auto merge of #65169 - tmandry:rollup-qxjj8xp, r=tmandry
[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 E0571: r##"
290 A `break` statement with an argument appeared in a non-`loop` loop.
291
292 Example of erroneous code:
293
294 ```compile_fail,E0571
295 # let mut i = 1;
296 # fn satisfied(n: usize) -> bool { n % 23 == 0 }
297 let result = while true {
298     if satisfied(i) {
299         break 2*i; // error: `break` with value from a `while` loop
300     }
301     i += 1;
302 };
303 ```
304
305 The `break` statement can take an argument (which will be the value of the loop
306 expression if the `break` statement is executed) in `loop` loops, but not
307 `for`, `while`, or `while let` loops.
308
309 Make sure `break value;` statements only occur in `loop` loops:
310
311 ```
312 # let mut i = 1;
313 # fn satisfied(n: usize) -> bool { n % 23 == 0 }
314 let result = loop { // ok!
315     if satisfied(i) {
316         break 2*i;
317     }
318     i += 1;
319 };
320 ```
321 "##,
322
323 E0590: r##"
324 `break` or `continue` must include a label when used in the condition of a
325 `while` loop.
326
327 Example of erroneous code:
328
329 ```compile_fail
330 while break {}
331 ```
332
333 To fix this, add a label specifying which loop is being broken out of:
334 ```
335 'foo: while break 'foo {}
336 ```
337 "##,
338
339 E0591: r##"
340 Per [RFC 401][rfc401], if you have a function declaration `foo`:
341
342 ```
343 // For the purposes of this explanation, all of these
344 // different kinds of `fn` declarations are equivalent:
345 struct S;
346 fn foo(x: S) { /* ... */ }
347 # #[cfg(for_demonstration_only)]
348 extern "C" { fn foo(x: S); }
349 # #[cfg(for_demonstration_only)]
350 impl S { fn foo(self) { /* ... */ } }
351 ```
352
353 the type of `foo` is **not** `fn(S)`, as one might expect.
354 Rather, it is a unique, zero-sized marker type written here as `typeof(foo)`.
355 However, `typeof(foo)` can be _coerced_ to a function pointer `fn(S)`,
356 so you rarely notice this:
357
358 ```
359 # struct S;
360 # fn foo(_: S) {}
361 let x: fn(S) = foo; // OK, coerces
362 ```
363
364 The reason that this matter is that the type `fn(S)` is not specific to
365 any particular function: it's a function _pointer_. So calling `x()` results
366 in a virtual call, whereas `foo()` is statically dispatched, because the type
367 of `foo` tells us precisely what function is being called.
368
369 As noted above, coercions mean that most code doesn't have to be
370 concerned with this distinction. However, you can tell the difference
371 when using **transmute** to convert a fn item into a fn pointer.
372
373 This is sometimes done as part of an FFI:
374
375 ```compile_fail,E0591
376 extern "C" fn foo(userdata: Box<i32>) {
377     /* ... */
378 }
379
380 # fn callback(_: extern "C" fn(*mut i32)) {}
381 # use std::mem::transmute;
382 # unsafe {
383 let f: extern "C" fn(*mut i32) = transmute(foo);
384 callback(f);
385 # }
386 ```
387
388 Here, transmute is being used to convert the types of the fn arguments.
389 This pattern is incorrect because, because the type of `foo` is a function
390 **item** (`typeof(foo)`), which is zero-sized, and the target type (`fn()`)
391 is a function pointer, which is not zero-sized.
392 This pattern should be rewritten. There are a few possible ways to do this:
393
394 - change the original fn declaration to match the expected signature,
395   and do the cast in the fn body (the preferred option)
396 - cast the fn item fo a fn pointer before calling transmute, as shown here:
397
398     ```
399     # extern "C" fn foo(_: Box<i32>) {}
400     # use std::mem::transmute;
401     # unsafe {
402     let f: extern "C" fn(*mut i32) = transmute(foo as extern "C" fn(_));
403     let f: extern "C" fn(*mut i32) = transmute(foo as usize); // works too
404     # }
405     ```
406
407 The same applies to transmutes to `*mut fn()`, which were observed in practice.
408 Note though that use of this type is generally incorrect.
409 The intention is typically to describe a function pointer, but just `fn()`
410 alone suffices for that. `*mut fn()` is a pointer to a fn pointer.
411 (Since these values are typically just passed to C code, however, this rarely
412 makes a difference in practice.)
413
414 [rfc401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
415 "##,
416
417 E0601: r##"
418 No `main` function was found in a binary crate. To fix this error, add a
419 `main` function. For example:
420
421 ```
422 fn main() {
423     // Your program will start here.
424     println!("Hello world!");
425 }
426 ```
427
428 If you don't know the basics of Rust, you can go look to the Rust Book to get
429 started: https://doc.rust-lang.org/book/
430 "##,
431
432 E0642: r##"
433 Trait methods currently cannot take patterns as arguments.
434
435 Example of erroneous code:
436
437 ```compile_fail,E0642
438 trait Foo {
439     fn foo((x, y): (i32, i32)); // error: patterns aren't allowed
440                                 //        in trait methods
441 }
442 ```
443
444 You can instead use a single name for the argument:
445
446 ```
447 trait Foo {
448     fn foo(x_and_y: (i32, i32)); // ok!
449 }
450 ```
451 "##,
452
453 E0695: r##"
454 A `break` statement without a label appeared inside a labeled block.
455
456 Example of erroneous code:
457
458 ```compile_fail,E0695
459 # #![feature(label_break_value)]
460 loop {
461     'a: {
462         break;
463     }
464 }
465 ```
466
467 Make sure to always label the `break`:
468
469 ```
470 # #![feature(label_break_value)]
471 'l: loop {
472     'a: {
473         break 'l;
474     }
475 }
476 ```
477
478 Or if you want to `break` the labeled block:
479
480 ```
481 # #![feature(label_break_value)]
482 loop {
483     'a: {
484         break 'a;
485     }
486     break;
487 }
488 ```
489 "##,
490
491 E0670: r##"
492 Rust 2015 does not permit the use of `async fn`.
493
494 Example of erroneous code:
495
496 ```compile_fail,E0670
497 async fn foo() {}
498 ```
499
500 Switch to the Rust 2018 edition to use `async fn`.
501 "##,
502
503 ;
504     E0226, // only a single explicit lifetime bound is permitted
505     E0472, // asm! is unsupported on this target
506     E0561, // patterns aren't allowed in function pointer types
507     E0567, // auto traits can not have generic parameters
508     E0568, // auto traits can not have super traits
509     E0666, // nested `impl Trait` is illegal
510     E0667, // `impl Trait` in projections
511     E0696, // `continue` pointing to a labeled block
512     E0706, // `async fn` in trait
513 }