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