]> git.lizzy.rs Git - rust.git/blob - src/librustc/diagnostics.rs
222ebb09f915b7956979b3b5b0dbc4c411fd331f
[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 that a
59 name will be extracted in all cases. If you encounter this error you probably need
60 to use a `match` or `if let` to deal with the possibility of failure.
61 "##,
62
63 E0006: r##"
64 Patterns used to bind names must be irrefutable, that is, they must guarantee that a
65 name will be extracted in all cases. If you encounter this error you probably need
66 to use a `match` or `if let` to deal with the possibility of failure.
67 "##,
68
69 E0007: r##"
70 This error indicates that the bindings in a match arm would require a value to
71 be moved into more than one location, thus violating unique ownership. Code like
72 the following is invalid as it requires the entire Option<String> to be moved
73 into a variable called `op_string` while simultaneously requiring the inner
74 String to be moved into a variable called `s`.
75
76 let x = Some("s".to_string());
77 match x {
78     op_string @ Some(s) => ...
79     None => ...
80 }
81
82 See also Error 303.
83 "##,
84
85 E0008: r##"
86 Names bound in match arms retain their type in pattern guards. As such, if a
87 name is bound by move in a pattern, it should also be moved to wherever it is
88 referenced in the pattern guard code. Doing so however would prevent the name
89 from being available in the body of the match arm. Consider the following:
90
91 match Some("hi".to_string()) {
92     Some(s) if s.len() == 0 => // use s.
93     ...
94 }
95
96 The variable `s` has type String, and its use in the guard is as a variable of
97 type String. The guard code effectively executes in a separate scope to the body
98 of the arm, so the value would be moved into this anonymous scope and therefore
99 become unavailable in the body of the arm. Although this example seems
100 innocuous, the problem is most clear when considering functions that take their
101 argument by value.
102
103 match Some("hi".to_string()) {
104     Some(s) if { drop(s); false } => (),
105     Some(s) => // use s.
106     ...
107 }
108
109 The value would be dropped in the guard then become unavailable not only in the
110 body of that arm but also in all subsequent arms! The solution is to bind by
111 reference when using guards or refactor the entire expression, perhaps by
112 putting the condition inside the body of the arm.
113 "##,
114
115 E0152: r##"
116 Lang items are already implemented in the standard library. Unless you are
117 writing a free-standing application (e.g. a kernel), you do not need to provide
118 them yourself.
119
120 You can build a free-standing crate by adding `#![no_std]` to the crate
121 attributes:
122
123 #![feature(no_std)]
124 #![no_std]
125
126 See also https://doc.rust-lang.org/book/no-stdlib.html
127 "##,
128
129 E0158: r##"
130 `const` and `static` mean different things. A `const` is a compile-time
131 constant, an alias for a literal value. This property means you can match it
132 directly within a pattern.
133
134 The `static` keyword, on the other hand, guarantees a fixed location in memory.
135 This does not always mean that the value is constant. For example, a global
136 mutex can be declared `static` as well.
137
138 If you want to match against a `static`, consider using a guard instead:
139
140 static FORTY_TWO: i32 = 42;
141 match Some(42) {
142     Some(x) if x == FORTY_TWO => ...
143     ...
144 }
145 "##,
146
147 E0161: r##"
148 In Rust, you can only move a value when its size is known at compile time.
149
150 To work around this restriction, consider "hiding" the value behind a reference:
151 either `&x` or `&mut x`. Since a reference has a fixed size, this lets you move
152 it around as usual.
153 "##,
154
155 E0162: r##"
156 An if-let pattern attempts to match the pattern, and enters the body if the
157 match was succesful. If the match is irrefutable (when it cannot fail to match),
158 use a regular `let`-binding instead. For instance:
159
160 struct Irrefutable(i32);
161 let irr = Irrefutable(0);
162
163 // This fails to compile because the match is irrefutable.
164 if let Irrefutable(x) = irr {
165     // This body will always be executed.
166     foo(x);
167 }
168
169 // Try this instead:
170 let Irrefutable(x) = irr;
171 foo(x);
172 "##,
173
174 E0165: r##"
175 A while-let pattern attempts to match the pattern, and enters the body if the
176 match was succesful. If the match is irrefutable (when it cannot fail to match),
177 use a regular `let`-binding inside a `loop` instead. For instance:
178
179 struct Irrefutable(i32);
180 let irr = Irrefutable(0);
181
182 // This fails to compile because the match is irrefutable.
183 while let Irrefutable(x) = irr {
184     ...
185 }
186
187 // Try this instead:
188 loop {
189     let Irrefutable(x) = irr;
190     ...
191 }
192 "##,
193
194 E0170: r##"
195 Enum variants are qualified by default. For example, given this type:
196
197 enum Method {
198     GET,
199     POST
200 }
201
202 you would match it using:
203
204 match m {
205     Method::GET => ...
206     Method::POST => ...
207 }
208
209 If you don't qualify the names, the code will bind new variables named "GET" and
210 "POST" instead. This behavior is likely not what you want, so rustc warns when
211 that happens.
212
213 Qualified names are good practice, and most code works well with them. But if
214 you prefer them unqualified, you can import the variants into scope:
215
216 use Method::*;
217 enum Method { GET, POST }
218 "##,
219
220 E0297: r##"
221 Patterns used to bind names must be irrefutable. That is, they must guarantee
222 that a name will be extracted in all cases. Instead of pattern matching the
223 loop variable, consider using a `match` or `if let` inside the loop body. For
224 instance:
225
226 // This fails because `None` is not covered.
227 for Some(x) in xs {
228     ...
229 }
230
231 // Match inside the loop instead:
232 for item in xs {
233     match item {
234         Some(x) => ...
235         None => ...
236     }
237 }
238
239 // Or use `if let`:
240 for item in xs {
241     if let Some(x) = item {
242         ...
243     }
244 }
245 "##,
246
247 E0301: r##"
248 Mutable borrows are not allowed in pattern guards, because matching cannot have
249 side effects. Side effects could alter the matched object or the environment
250 on which the match depends in such a way, that the match would not be
251 exhaustive. For instance, the following would not match any arm if mutable
252 borrows were allowed:
253
254 match Some(()) {
255     None => { },
256     option if option.take().is_none() => { /* impossible, option is `Some` */ },
257     Some(_) => { } // When the previous match failed, the option became `None`.
258 }
259 "##,
260
261 E0302: r##"
262 Assignments are not allowed in pattern guards, because matching cannot have
263 side effects. Side effects could alter the matched object or the environment
264 on which the match depends in such a way, that the match would not be
265 exhaustive. For instance, the following would not match any arm if assignments
266 were allowed:
267
268 match Some(()) {
269     None => { },
270     option if { option = None; false } { },
271     Some(_) => { } // When the previous match failed, the option became `None`.
272 }
273 "##,
274
275 E0303: r##"
276 In certain cases it is possible for sub-bindings to violate memory safety.
277 Updates to the borrow checker in a future version of Rust may remove this
278 restriction, but for now patterns must be rewritten without sub-bindings.
279
280 // Code like this...
281 match Some(5) {
282     ref op_num @ Some(num) => ...
283     None => ...
284 }
285
286 // ... should be updated to code like this.
287 match Some(5) {
288     Some(num) => {
289         let op_num = &Some(num);
290         ...
291     }
292     None => ...
293 }
294
295 See also https://github.com/rust-lang/rust/issues/14587
296 "##
297
298 }
299
300 register_diagnostics! {
301     E0009,
302     E0010,
303     E0011,
304     E0012,
305     E0013,
306     E0014,
307     E0015,
308     E0016,
309     E0017,
310     E0018,
311     E0019,
312     E0020,
313     E0022,
314     E0079, // enum variant: expected signed integer constant
315     E0080, // enum variant: constant evaluation error
316     E0109,
317     E0110,
318     E0133,
319     E0134,
320     E0135,
321     E0136,
322     E0137,
323     E0138,
324     E0139,
325     E0261, // use of undeclared lifetime name
326     E0262, // illegal lifetime parameter name
327     E0263, // lifetime name declared twice in same scope
328     E0264, // unknown external lang item
329     E0265, // recursive constant
330     E0266, // expected item
331     E0267, // thing inside of a closure
332     E0268, // thing outside of a loop
333     E0269, // not all control paths return a value
334     E0270, // computation may converge in a function marked as diverging
335     E0271, // type mismatch resolving
336     E0272, // rustc_on_unimplemented attribute refers to non-existent type parameter
337     E0273, // rustc_on_unimplemented must have named format arguments
338     E0274, // rustc_on_unimplemented must have a value
339     E0275, // overflow evaluating requirement
340     E0276, // requirement appears on impl method but not on corresponding trait method
341     E0277, // trait is not implemented for type
342     E0278, // requirement is not satisfied
343     E0279, // requirement is not satisfied
344     E0280, // requirement is not satisfied
345     E0281, // type implements trait but other trait is required
346     E0282, // unable to infer enough type information about
347     E0283, // cannot resolve type
348     E0284, // cannot resolve type
349     E0285, // overflow evaluation builtin bounds
350     E0296, // malformed recursion limit attribute
351     E0298, // mismatched types between arms
352     E0299, // mismatched types between arms
353     E0300, // unexpanded macro
354     E0304, // expected signed integer constant
355     E0305, // expected constant
356     E0306, // expected positive integer for repeat count
357     E0307, // expected constant integer for repeat count
358     E0308,
359     E0309, // thing may not live long enough
360     E0310, // thing may not live long enough
361     E0311, // thing may not live long enough
362     E0312, // lifetime of reference outlives lifetime of borrowed content
363     E0313, // lifetime of borrowed pointer outlives lifetime of captured variable
364     E0314, // closure outlives stack frame
365     E0315, // cannot invoke closure outside of its lifetime
366     E0316, // nested quantification of lifetimes
367     E0370  // discriminant overflow
368 }
369
370 __build_diagnostic_array! { DIAGNOSTICS }