]> git.proxmox.com Git - rustc.git/blob - src/librustc_const_eval/diagnostics.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc_const_eval / diagnostics.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![allow(non_snake_case)]
12
13 // Error messages for EXXXX errors.
14 // Each message should start and end with a new line, and be wrapped to 80 characters.
15 // In vim you can `:set tw=80` and use `gq` to wrap paragraphs. Use `:set tw=0` to disable.
16 register_long_diagnostics! {
17
18 E0001: r##"
19 This error suggests that the expression arm corresponding to the noted pattern
20 will never be reached as for all possible values of the expression being
21 matched, one of the preceding patterns will match.
22
23 This means that perhaps some of the preceding patterns are too general, this
24 one is too specific or the ordering is incorrect.
25
26 For example, the following `match` block has too many arms:
27
28 ```compile_fail
29 match foo {
30 Some(bar) => {/* ... */}
31 None => {/* ... */}
32 _ => {/* ... */} // All possible cases have already been handled
33 }
34 ```
35
36 `match` blocks have their patterns matched in order, so, for example, putting
37 a wildcard arm above a more specific arm will make the latter arm irrelevant.
38
39 Ensure the ordering of the match arm is correct and remove any superfluous
40 arms.
41 "##,
42
43 E0002: r##"
44 This error indicates that an empty match expression is invalid because the type
45 it is matching on is non-empty (there exist values of this type). In safe code
46 it is impossible to create an instance of an empty type, so empty match
47 expressions are almost never desired. This error is typically fixed by adding
48 one or more cases to the match expression.
49
50 An example of an empty type is `enum Empty { }`. So, the following will work:
51
52 ```
53 enum Empty {}
54
55 fn foo(x: Empty) {
56 match x {
57 // empty
58 }
59 }
60 ```
61
62 However, this won't:
63
64 ```compile_fail
65 enum Empty {}
66
67 fn foo(x: Option<String>) {
68 match x {
69 // empty
70 }
71 }
72 ```
73 "##,
74
75
76 E0003: r##"
77 Not-a-Number (NaN) values cannot be compared for equality and hence can never
78 match the input to a match expression. So, the following will not compile:
79
80 ```compile_fail
81 const NAN: f32 = 0.0 / 0.0;
82
83 let number = 0.1f32;
84
85 match number {
86 NAN => { /* ... */ },
87 _ => {}
88 }
89 ```
90
91 To match against NaN values, you should instead use the `is_nan()` method in a
92 guard, like so:
93
94 ```
95 let number = 0.1f32;
96
97 match number {
98 x if x.is_nan() => { /* ... */ }
99 _ => {}
100 }
101 ```
102 "##,
103
104
105 E0004: r##"
106 This error indicates that the compiler cannot guarantee a matching pattern for
107 one or more possible inputs to a match expression. Guaranteed matches are
108 required in order to assign values to match expressions, or alternatively,
109 determine the flow of execution. Erroneous code example:
110
111 ```compile_fail
112 enum Terminator {
113 HastaLaVistaBaby,
114 TalkToMyHand,
115 }
116
117 let x = Terminator::HastaLaVistaBaby;
118
119 match x { // error: non-exhaustive patterns: `HastaLaVistaBaby` not covered
120 Terminator::TalkToMyHand => {}
121 }
122 ```
123
124 If you encounter this error you must alter your patterns so that every possible
125 value of the input type is matched. For types with a small number of variants
126 (like enums) you should probably cover all cases explicitly. Alternatively, the
127 underscore `_` wildcard pattern can be added after all other patterns to match
128 "anything else". Example:
129
130 ```
131 enum Terminator {
132 HastaLaVistaBaby,
133 TalkToMyHand,
134 }
135
136 let x = Terminator::HastaLaVistaBaby;
137
138 match x {
139 Terminator::TalkToMyHand => {}
140 Terminator::HastaLaVistaBaby => {}
141 }
142
143 // or:
144
145 match x {
146 Terminator::TalkToMyHand => {}
147 _ => {}
148 }
149 ```
150 "##,
151
152 E0005: r##"
153 Patterns used to bind names must be irrefutable, that is, they must guarantee
154 that a name will be extracted in all cases. Erroneous code example:
155
156 ```compile_fail
157 let x = Some(1);
158 let Some(y) = x;
159 // error: refutable pattern in local binding: `None` not covered
160 ```
161
162 If you encounter this error you probably need to use a `match` or `if let` to
163 deal with the possibility of failure. Example:
164
165 ```compile_fail
166 let x = Some(1);
167
168 match x {
169 Some(y) => {
170 // do something
171 },
172 None => {}
173 }
174
175 // or:
176
177 if let Some(y) = x {
178 // do something
179 }
180 ```
181 "##,
182
183 E0007: r##"
184 This error indicates that the bindings in a match arm would require a value to
185 be moved into more than one location, thus violating unique ownership. Code
186 like the following is invalid as it requires the entire `Option<String>` to be
187 moved into a variable called `op_string` while simultaneously requiring the
188 inner `String` to be moved into a variable called `s`.
189
190 ```compile_fail
191 let x = Some("s".to_string());
192
193 match x {
194 op_string @ Some(s) => {},
195 None => {},
196 }
197 ```
198
199 See also the error E0303.
200 "##,
201
202 E0008: r##"
203 Names bound in match arms retain their type in pattern guards. As such, if a
204 name is bound by move in a pattern, it should also be moved to wherever it is
205 referenced in the pattern guard code. Doing so however would prevent the name
206 from being available in the body of the match arm. Consider the following:
207
208 ```compile_fail
209 match Some("hi".to_string()) {
210 Some(s) if s.len() == 0 => {}, // use s.
211 _ => {},
212 }
213 ```
214
215 The variable `s` has type `String`, and its use in the guard is as a variable of
216 type `String`. The guard code effectively executes in a separate scope to the
217 body of the arm, so the value would be moved into this anonymous scope and
218 therefore become unavailable in the body of the arm. Although this example seems
219 innocuous, the problem is most clear when considering functions that take their
220 argument by value.
221
222 ```compile_fail
223 match Some("hi".to_string()) {
224 Some(s) if { drop(s); false } => (),
225 Some(s) => {}, // use s.
226 _ => {},
227 }
228 ```
229
230 The value would be dropped in the guard then become unavailable not only in the
231 body of that arm but also in all subsequent arms! The solution is to bind by
232 reference when using guards or refactor the entire expression, perhaps by
233 putting the condition inside the body of the arm.
234 "##,
235
236 E0009: r##"
237 In a pattern, all values that don't implement the `Copy` trait have to be bound
238 the same way. The goal here is to avoid binding simultaneously by-move and
239 by-ref.
240
241 This limitation may be removed in a future version of Rust.
242
243 Erroneous code example:
244
245 ```compile_fail
246 struct X { x: (), }
247
248 let x = Some((X { x: () }, X { x: () }));
249 match x {
250 Some((y, ref z)) => {},
251 None => panic!()
252 }
253 ```
254
255 You have two solutions:
256
257 Solution #1: Bind the pattern's values the same way.
258
259 ```
260 struct X { x: (), }
261
262 let x = Some((X { x: () }, X { x: () }));
263 match x {
264 Some((ref y, ref z)) => {},
265 // or Some((y, z)) => {}
266 None => panic!()
267 }
268 ```
269
270 Solution #2: Implement the `Copy` trait for the `X` structure.
271
272 However, please keep in mind that the first solution should be preferred.
273
274 ```
275 #[derive(Clone, Copy)]
276 struct X { x: (), }
277
278 let x = Some((X { x: () }, X { x: () }));
279 match x {
280 Some((y, ref z)) => {},
281 None => panic!()
282 }
283 ```
284 "##,
285
286 E0158: r##"
287 `const` and `static` mean different things. A `const` is a compile-time
288 constant, an alias for a literal value. This property means you can match it
289 directly within a pattern.
290
291 The `static` keyword, on the other hand, guarantees a fixed location in memory.
292 This does not always mean that the value is constant. For example, a global
293 mutex can be declared `static` as well.
294
295 If you want to match against a `static`, consider using a guard instead:
296
297 ```
298 static FORTY_TWO: i32 = 42;
299
300 match Some(42) {
301 Some(x) if x == FORTY_TWO => {}
302 _ => {}
303 }
304 ```
305 "##,
306
307 E0162: r##"
308 An if-let pattern attempts to match the pattern, and enters the body if the
309 match was successful. If the match is irrefutable (when it cannot fail to
310 match), use a regular `let`-binding instead. For instance:
311
312 ```compile_fail
313 struct Irrefutable(i32);
314 let irr = Irrefutable(0);
315
316 // This fails to compile because the match is irrefutable.
317 if let Irrefutable(x) = irr {
318 // This body will always be executed.
319 foo(x);
320 }
321 ```
322
323 Try this instead:
324
325 ```ignore
326 struct Irrefutable(i32);
327 let irr = Irrefutable(0);
328
329 let Irrefutable(x) = irr;
330 foo(x);
331 ```
332 "##,
333
334 E0165: r##"
335 A while-let pattern attempts to match the pattern, and enters the body if the
336 match was successful. If the match is irrefutable (when it cannot fail to
337 match), use a regular `let`-binding inside a `loop` instead. For instance:
338
339 ```compile_fail
340 struct Irrefutable(i32);
341 let irr = Irrefutable(0);
342
343 // This fails to compile because the match is irrefutable.
344 while let Irrefutable(x) = irr {
345 ...
346 }
347
348 Try this instead:
349
350 ```
351 struct Irrefutable(i32);
352 let irr = Irrefutable(0);
353
354 loop {
355 let Irrefutable(x) = irr;
356 ...
357 }
358 ```
359 "##,
360
361 E0170: r##"
362 Enum variants are qualified by default. For example, given this type:
363
364 ```
365 enum Method {
366 GET,
367 POST,
368 }
369 ```
370
371 You would match it using:
372
373 ```
374 enum Method {
375 GET,
376 POST,
377 }
378
379 let m = Method::GET;
380
381 match m {
382 Method::GET => {},
383 Method::POST => {},
384 }
385 ```
386
387 If you don't qualify the names, the code will bind new variables named "GET" and
388 "POST" instead. This behavior is likely not what you want, so `rustc` warns when
389 that happens.
390
391 Qualified names are good practice, and most code works well with them. But if
392 you prefer them unqualified, you can import the variants into scope:
393
394 ```ignore
395 use Method::*;
396 enum Method { GET, POST }
397 ```
398
399 If you want others to be able to import variants from your module directly, use
400 `pub use`:
401
402 ```ignore
403 pub use Method::*;
404 enum Method { GET, POST }
405 ```
406 "##,
407
408
409 E0297: r##"
410 Patterns used to bind names must be irrefutable. That is, they must guarantee
411 that a name will be extracted in all cases. Instead of pattern matching the
412 loop variable, consider using a `match` or `if let` inside the loop body. For
413 instance:
414
415 ```compile_fail
416 let xs : Vec<Option<i32>> = vec!(Some(1), None);
417
418 // This fails because `None` is not covered.
419 for Some(x) in xs {
420 // ...
421 }
422 ```
423
424 Match inside the loop instead:
425
426 ```
427 let xs : Vec<Option<i32>> = vec!(Some(1), None);
428
429 for item in xs {
430 match item {
431 Some(x) => {},
432 None => {},
433 }
434 }
435 ```
436
437 Or use `if let`:
438
439 ```
440 let xs : Vec<Option<i32>> = vec!(Some(1), None);
441
442 for item in xs {
443 if let Some(x) = item {
444 // ...
445 }
446 }
447 ```
448 "##,
449
450 E0301: r##"
451 Mutable borrows are not allowed in pattern guards, because matching cannot have
452 side effects. Side effects could alter the matched object or the environment
453 on which the match depends in such a way, that the match would not be
454 exhaustive. For instance, the following would not match any arm if mutable
455 borrows were allowed:
456
457 ```compile_fail
458 match Some(()) {
459 None => { },
460 option if option.take().is_none() => {
461 /* impossible, option is `Some` */
462 },
463 Some(_) => { } // When the previous match failed, the option became `None`.
464 }
465 ```
466 "##,
467
468 E0302: r##"
469 Assignments are not allowed in pattern guards, because matching cannot have
470 side effects. Side effects could alter the matched object or the environment
471 on which the match depends in such a way, that the match would not be
472 exhaustive. For instance, the following would not match any arm if assignments
473 were allowed:
474
475 ```compile_fail
476 match Some(()) {
477 None => { },
478 option if { option = None; false } { },
479 Some(_) => { } // When the previous match failed, the option became `None`.
480 }
481 ```
482 "##,
483
484 E0303: r##"
485 In certain cases it is possible for sub-bindings to violate memory safety.
486 Updates to the borrow checker in a future version of Rust may remove this
487 restriction, but for now patterns must be rewritten without sub-bindings.
488
489 ```ignore
490 // Before.
491 match Some("hi".to_string()) {
492 ref op_string_ref @ Some(s) => {},
493 None => {},
494 }
495
496 // After.
497 match Some("hi".to_string()) {
498 Some(ref s) => {
499 let op_string_ref = &Some(s);
500 // ...
501 },
502 None => {},
503 }
504 ```
505
506 The `op_string_ref` binding has type `&Option<&String>` in both cases.
507
508 See also https://github.com/rust-lang/rust/issues/14587
509 "##,
510
511 E0306: r##"
512 In an array literal `[x; N]`, `N` is the number of elements in the array. This
513 must be an unsigned integer. Erroneous code example:
514
515 ```compile_fail
516 let x = [0i32; true]; // error: expected positive integer for repeat count,
517 // found boolean
518 ```
519
520 Working example:
521
522 ```
523 let x = [0i32; 2];
524 ```
525 "##,
526
527 E0307: r##"
528 The length of an array is part of its type. For this reason, this length must
529 be a compile-time constant. Erroneous code example:
530
531 ```compile_fail
532 let len = 10;
533 let x = [0i32; len]; // error: expected constant integer for repeat count,
534 // found variable
535 ```
536 "##,
537
538 }
539
540
541 register_diagnostics! {
542 E0298, // mismatched types between arms
543 E0299, // mismatched types between arms
544 E0471, // constant evaluation error: ..
545 }