]> git.lizzy.rs Git - rust.git/blob - src/librustc/diagnostics.rs
rollup merge of #24894: bguiz/diagnostic-E0267
[rust.git] / src / librustc / 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 one
24 is too specific or the ordering is incorrect.
25 "##,
26
27 E0002: r##"
28 This error indicates that an empty match expression is illegal because the type
29 it is matching on is non-empty (there exist values of this type). In safe code
30 it is impossible to create an instance of an empty type, so empty match
31 expressions are almost never desired.  This error is typically fixed by adding
32 one or more cases to the match expression.
33
34 An example of an empty type is `enum Empty { }`.
35 "##,
36
37 E0003: r##"
38 Not-a-Number (NaN) values cannot be compared for equality and hence can never
39 match the input to a match expression. To match against NaN values, you should
40 instead use the `is_nan` method in a guard, as in: x if x.is_nan() => ...
41 "##,
42
43 E0004: r##"
44 This error indicates that the compiler cannot guarantee a matching pattern for
45 one or more possible inputs to a match expression. Guaranteed matches are
46 required in order to assign values to match expressions, or alternatively,
47 determine the flow of execution.
48
49 If you encounter this error you must alter your patterns so that every possible
50 value of the input type is matched. For types with a small number of variants
51 (like enums) you should probably cover all cases explicitly. Alternatively, the
52 underscore `_` wildcard pattern can be added after all other patterns to match
53 "anything else".
54 "##,
55
56 // FIXME: Remove duplication here?
57 E0005: r##"
58 Patterns used to bind names must be irrefutable, that is, they must guarantee
59 that a name will be extracted in all cases. If you encounter this error you
60 probably need to use a `match` or `if let` to deal with the possibility of
61 failure.
62 "##,
63
64 E0006: r##"
65 Patterns used to bind names must be irrefutable, that is, they must guarantee
66 that a name will be extracted in all cases. If you encounter this error you
67 probably need to use a `match` or `if let` to deal with the possibility of
68 failure.
69 "##,
70
71 E0007: r##"
72 This error indicates that the bindings in a match arm would require a value to
73 be moved into more than one location, thus violating unique ownership. Code like
74 the following is invalid as it requires the entire Option<String> to be moved
75 into a variable called `op_string` while simultaneously requiring the inner
76 String to be moved into a variable called `s`.
77
78 ```
79 let x = Some("s".to_string());
80 match x {
81     op_string @ Some(s) => ...
82     None => ...
83 }
84 ```
85
86 See also Error 303.
87 "##,
88
89 E0008: r##"
90 Names bound in match arms retain their type in pattern guards. As such, if a
91 name is bound by move in a pattern, it should also be moved to wherever it is
92 referenced in the pattern guard code. Doing so however would prevent the name
93 from being available in the body of the match arm. Consider the following:
94
95 ```
96 match Some("hi".to_string()) {
97     Some(s) if s.len() == 0 => // use s.
98     ...
99 }
100 ```
101
102 The variable `s` has type String, and its use in the guard is as a variable of
103 type String. The guard code effectively executes in a separate scope to the body
104 of the arm, so the value would be moved into this anonymous scope and therefore
105 become unavailable in the body of the arm. Although this example seems
106 innocuous, the problem is most clear when considering functions that take their
107 argument by value.
108
109 ```
110 match Some("hi".to_string()) {
111     Some(s) if { drop(s); false } => (),
112     Some(s) => // use s.
113     ...
114 }
115 ```
116
117 The value would be dropped in the guard then become unavailable not only in the
118 body of that arm but also in all subsequent arms! The solution is to bind by
119 reference when using guards or refactor the entire expression, perhaps by
120 putting the condition inside the body of the arm.
121 "##,
122
123 E0009: r##"
124 In a pattern, all values that don't implement the `Copy` trait have to be bound
125 the same way. The goal here is to avoid binding simultaneously by-move and
126 by-ref.
127
128 This limitation may be removed in a future version of Rust.
129
130 Wrong example:
131
132 ```
133 struct X { x: (), }
134
135 let x = Some((X { x: () }, X { x: () }));
136 match x {
137     Some((y, ref z)) => {},
138     None => panic!()
139 }
140 ```
141
142 You have two solutions:
143 1. Bind the pattern's values the same way:
144
145 ```
146 struct X { x: (), }
147
148 let x = Some((X { x: () }, X { x: () }));
149 match x {
150     Some((ref y, ref z)) => {},
151     // or Some((y, z)) => {}
152     None => panic!()
153 }
154 ```
155
156 2. Implement the `Copy` trait for the X structure (however, please
157 keep in mind that the first solution should be preferred!):
158
159 ```
160 #[derive(Clone, Copy)]
161 struct X { x: (), }
162
163 let x = Some((X { x: () }, X { x: () }));
164 match x {
165     Some((y, ref z)) => {},
166     None => panic!()
167 }
168 ```
169 "##,
170
171 E0013: r##"
172 Static and const variables can refer to other const variables. But a const
173 variable cannot refer to a static variable. For example, `Y` cannot refer to `X`
174 here:
175
176 ```
177 static X: i32 = 42;
178 const Y: i32 = X;
179 ```
180
181 To fix this, the value can be extracted as a const and then used:
182
183 ```
184 const A: i32 = 42;
185 static X: i32 = A;
186 const Y: i32 = A;
187 ```
188 "##,
189
190 E0015: r##"
191 The only function calls allowed in static or constant expressions are enum
192 variant constructors or struct constructors (for unit or tuple structs). This
193 is because Rust currently does not support compile-time function execution.
194 "##,
195
196 E0018: r##"
197 The value of static and const variables must be known at compile time. You
198 can't cast a pointer as an integer because we can't know what value the
199 address will take.
200
201 However, pointers to other constants' addresses are allowed in constants,
202 example:
203
204 ```
205 const X: u32 = 50;
206 const Y: *const u32 = &X;
207 ```
208
209 Therefore, casting one of these non-constant pointers to an integer results
210 in a non-constant integer which lead to this error. Example:
211
212 ```
213 const X: u32 = 50;
214 const Y: *const u32 = &X;
215 println!("{:?}", Y);
216 ```
217 "##,
218
219 E0020: r##"
220 This error indicates that an attempt was made to divide by zero (or take the
221 remainder of a zero divisor) in a static or constant expression.
222 "##,
223
224 E0133: r##"
225 Using unsafe functionality, such as dereferencing raw pointers and calling
226 functions via FFI or marked as unsafe, is potentially dangerous and disallowed
227 by safety checks. As such, those safety checks can be temporarily relaxed by
228 wrapping the unsafe instructions inside an `unsafe` block. For instance:
229
230 unsafe fn f() { return; }
231
232 fn main() {
233     unsafe { f(); }
234 }
235
236 See also http://doc.rust-lang.org/book/unsafe.html
237 "##,
238
239 E0152: r##"
240 Lang items are already implemented in the standard library. Unless you are
241 writing a free-standing application (e.g. a kernel), you do not need to provide
242 them yourself.
243
244 You can build a free-standing crate by adding `#![no_std]` to the crate
245 attributes:
246
247 ```
248 #![feature(no_std)]
249 #![no_std]
250 ```
251
252 See also https://doc.rust-lang.org/book/no-stdlib.html
253 "##,
254
255 E0158: r##"
256 `const` and `static` mean different things. A `const` is a compile-time
257 constant, an alias for a literal value. This property means you can match it
258 directly within a pattern.
259
260 The `static` keyword, on the other hand, guarantees a fixed location in memory.
261 This does not always mean that the value is constant. For example, a global
262 mutex can be declared `static` as well.
263
264 If you want to match against a `static`, consider using a guard instead:
265
266 ```
267 static FORTY_TWO: i32 = 42;
268 match Some(42) {
269     Some(x) if x == FORTY_TWO => ...
270     ...
271 }
272 ```
273 "##,
274
275 E0161: r##"
276 In Rust, you can only move a value when its size is known at compile time.
277
278 To work around this restriction, consider "hiding" the value behind a reference:
279 either `&x` or `&mut x`. Since a reference has a fixed size, this lets you move
280 it around as usual.
281 "##,
282
283 E0162: r##"
284 An if-let pattern attempts to match the pattern, and enters the body if the
285 match was succesful. If the match is irrefutable (when it cannot fail to match),
286 use a regular `let`-binding instead. For instance:
287
288 ```
289 struct Irrefutable(i32);
290 let irr = Irrefutable(0);
291
292 // This fails to compile because the match is irrefutable.
293 if let Irrefutable(x) = irr {
294     // This body will always be executed.
295     foo(x);
296 }
297
298 // Try this instead:
299 let Irrefutable(x) = irr;
300 foo(x);
301 ```
302 "##,
303
304 E0165: r##"
305 A while-let pattern attempts to match the pattern, and enters the body if the
306 match was succesful. If the match is irrefutable (when it cannot fail to match),
307 use a regular `let`-binding inside a `loop` instead. For instance:
308
309 ```
310 struct Irrefutable(i32);
311 let irr = Irrefutable(0);
312
313 // This fails to compile because the match is irrefutable.
314 while let Irrefutable(x) = irr {
315     ...
316 }
317
318 // Try this instead:
319 loop {
320     let Irrefutable(x) = irr;
321     ...
322 }
323 ```
324 "##,
325
326 E0170: r##"
327 Enum variants are qualified by default. For example, given this type:
328
329 ```
330 enum Method {
331     GET,
332     POST
333 }
334 ```
335
336 you would match it using:
337
338 ```
339 match m {
340     Method::GET => ...
341     Method::POST => ...
342 }
343 ```
344
345 If you don't qualify the names, the code will bind new variables named "GET" and
346 "POST" instead. This behavior is likely not what you want, so rustc warns when
347 that happens.
348
349 Qualified names are good practice, and most code works well with them. But if
350 you prefer them unqualified, you can import the variants into scope:
351
352 ```
353 use Method::*;
354 enum Method { GET, POST }
355 ```
356 "##,
357
358 E0265: r##"
359 This error indicates that a static or constant references itself.
360 All statics and constants need to resolve to a value in an acyclic manner.
361
362 For example, neither of the following can be sensibly compiled:
363
364 ```
365 const X: u32 = X;
366 ```
367
368 ```
369 const X: u32 = Y;
370 const Y: u32 = X;
371 ```
372 "##,
373
374 E0267: r##"
375 This error indicates the use of loop keyword (break or continue) inside a
376 closure but outside of any loop. Break and continue can be used as normal
377 inside closures as long as they are also contained within a loop. To halt the
378 execution of a closure you should instead use a return statement.
379 "##,
380
381 E0268: r##"
382 This error indicates the use of loop keyword (break or continue) outside of a
383 loop. Without a loop to break out of or continue in, no sensible action can be
384 taken.
385 "##,
386
387 E0296: r##"
388 This error indicates that the given recursion limit could not be parsed. Ensure
389 that the value provided is a positive integer between quotes, like so:
390
391 ```
392 #![recursion_limit="1000"]
393 ```
394 "##,
395
396 E0297: r##"
397 Patterns used to bind names must be irrefutable. That is, they must guarantee
398 that a name will be extracted in all cases. Instead of pattern matching the
399 loop variable, consider using a `match` or `if let` inside the loop body. For
400 instance:
401
402 ```
403 // This fails because `None` is not covered.
404 for Some(x) in xs {
405     ...
406 }
407
408 // Match inside the loop instead:
409 for item in xs {
410     match item {
411         Some(x) => ...
412         None => ...
413     }
414 }
415
416 // Or use `if let`:
417 for item in xs {
418     if let Some(x) = item {
419         ...
420     }
421 }
422 ```
423 "##,
424
425 E0301: r##"
426 Mutable borrows are not allowed in pattern guards, because matching cannot have
427 side effects. Side effects could alter the matched object or the environment
428 on which the match depends in such a way, that the match would not be
429 exhaustive. For instance, the following would not match any arm if mutable
430 borrows were allowed:
431
432 ```
433 match Some(()) {
434     None => { },
435     option if option.take().is_none() => { /* impossible, option is `Some` */ },
436     Some(_) => { } // When the previous match failed, the option became `None`.
437 }
438 ```
439 "##,
440
441 E0302: r##"
442 Assignments are not allowed in pattern guards, because matching cannot have
443 side effects. Side effects could alter the matched object or the environment
444 on which the match depends in such a way, that the match would not be
445 exhaustive. For instance, the following would not match any arm if assignments
446 were allowed:
447
448 ```
449 match Some(()) {
450     None => { },
451     option if { option = None; false } { },
452     Some(_) => { } // When the previous match failed, the option became `None`.
453 }
454 ```
455 "##,
456
457 E0303: r##"
458 In certain cases it is possible for sub-bindings to violate memory safety.
459 Updates to the borrow checker in a future version of Rust may remove this
460 restriction, but for now patterns must be rewritten without sub-bindings.
461
462 ```
463 // Code like this...
464 match Some(5) {
465     ref op_num @ Some(num) => ...
466     None => ...
467 }
468
469 // After.
470 match Some("hi".to_string()) {
471     Some(ref s) => {
472         let op_string_ref = &Some(&s);
473         ...
474     }
475     None => ...
476 }
477 ```
478
479 The `op_string_ref` binding has type &Option<&String> in both cases.
480
481 See also https://github.com/rust-lang/rust/issues/14587
482 "##,
483
484 E0306: r##"
485 In an array literal `[x; N]`, `N` is the number of elements in the array. This
486 number cannot be negative.
487 "##,
488
489 E0307: r##"
490 The length of an array is part of its type. For this reason, this length must be
491 a compile-time constant.
492 "##
493
494 }
495
496 register_diagnostics! {
497     E0010,
498     E0011,
499     E0012,
500     E0014,
501     E0016,
502     E0017,
503     E0019,
504     E0022,
505     E0079, // enum variant: expected signed integer constant
506     E0080, // enum variant: constant evaluation error
507     E0109,
508     E0110,
509     E0134,
510     E0135,
511     E0136,
512     E0137,
513     E0138,
514     E0139,
515     E0261, // use of undeclared lifetime name
516     E0262, // illegal lifetime parameter name
517     E0263, // lifetime name declared twice in same scope
518     E0264, // unknown external lang item
519     E0266, // expected item
520     E0269, // not all control paths return a value
521     E0270, // computation may converge in a function marked as diverging
522     E0271, // type mismatch resolving
523     E0272, // rustc_on_unimplemented attribute refers to non-existent type parameter
524     E0273, // rustc_on_unimplemented must have named format arguments
525     E0274, // rustc_on_unimplemented must have a value
526     E0275, // overflow evaluating requirement
527     E0276, // requirement appears on impl method but not on corresponding trait method
528     E0277, // trait is not implemented for type
529     E0278, // requirement is not satisfied
530     E0279, // requirement is not satisfied
531     E0280, // requirement is not satisfied
532     E0281, // type implements trait but other trait is required
533     E0282, // unable to infer enough type information about
534     E0283, // cannot resolve type
535     E0284, // cannot resolve type
536     E0285, // overflow evaluation builtin bounds
537     E0298, // mismatched types between arms
538     E0299, // mismatched types between arms
539     E0300, // unexpanded macro
540     E0304, // expected signed integer constant
541     E0305, // expected constant
542     E0308,
543     E0309, // thing may not live long enough
544     E0310, // thing may not live long enough
545     E0311, // thing may not live long enough
546     E0312, // lifetime of reference outlives lifetime of borrowed content
547     E0313, // lifetime of borrowed pointer outlives lifetime of captured variable
548     E0314, // closure outlives stack frame
549     E0315, // cannot invoke closure outside of its lifetime
550     E0316, // nested quantification of lifetimes
551     E0370  // discriminant overflow
552 }
553
554 __build_diagnostic_array! { DIAGNOSTICS }