]> git.proxmox.com Git - rustc.git/blame - src/librustc/diagnostics.rs
Imported Upstream version 1.0.0~beta.3
[rustc.git] / src / librustc / diagnostics.rs
CommitLineData
1a4d82fc
JJ
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
9346a6ac
AL
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.
85aaf69f 16register_long_diagnostics! {
1a4d82fc 17
9346a6ac
AL
18E0001: r##"
19This error suggests that the expression arm corresponding to the noted pattern
20will never be reached as for all possible values of the expression being
21matched, one of the preceding patterns will match.
22
23This means that perhaps some of the preceding patterns are too general, this one
24is too specific or the ordering is incorrect.
25"##,
26
27E0002: r##"
28This error indicates that an empty match expression is illegal because the type
29it is matching on is non-empty (there exist values of this type). In safe code
30it is impossible to create an instance of an empty type, so empty match
31expressions are almost never desired. This error is typically fixed by adding
32one or more cases to the match expression.
33
34An example of an empty type is `enum Empty { }`.
35"##,
36
37E0003: r##"
38Not-a-Number (NaN) values cannot be compared for equality and hence can never
39match the input to a match expression. To match against NaN values, you should
40instead use the `is_nan` method in a guard, as in: x if x.is_nan() => ...
41"##,
42
43E0004: r##"
44This error indicates that the compiler cannot guarantee a matching pattern for
45one or more possible inputs to a match expression. Guaranteed matches are
46required in order to assign values to match expressions, or alternatively,
47determine the flow of execution.
48
49If you encounter this error you must alter your patterns so that every possible
50value 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
52underscore `_` wildcard pattern can be added after all other patterns to match
53"anything else".
54"##,
55
56// FIXME: Remove duplication here?
57E0005: r##"
58Patterns used to bind names must be irrefutable, that is, they must guarantee that a
59name will be extracted in all cases. If you encounter this error you probably need
60to use a `match` or `if let` to deal with the possibility of failure.
61"##,
62
63E0006: r##"
64Patterns used to bind names must be irrefutable, that is, they must guarantee that a
65name will be extracted in all cases. If you encounter this error you probably need
66to use a `match` or `if let` to deal with the possibility of failure.
67"##,
68
69E0007: r##"
70This error indicates that the bindings in a match arm would require a value to
71be moved into more than one location, thus violating unique ownership. Code like
72the following is invalid as it requires the entire Option<String> to be moved
73into a variable called `op_string` while simultaneously requiring the inner
74String to be moved into a variable called `s`.
75
76let x = Some("s".to_string());
77match x {
78 op_string @ Some(s) => ...
79 None => ...
80}
81
82See also Error 303.
83"##,
84
85E0008: r##"
86Names bound in match arms retain their type in pattern guards. As such, if a
87name is bound by move in a pattern, it should also be moved to wherever it is
88referenced in the pattern guard code. Doing so however would prevent the name
89from being available in the body of the match arm. Consider the following:
90
91match Some("hi".to_string()) {
92 Some(s) if s.len() == 0 => // use s.
93 ...
94}
95
96The variable `s` has type String, and its use in the guard is as a variable of
97type String. The guard code effectively executes in a separate scope to the body
98of the arm, so the value would be moved into this anonymous scope and therefore
99become unavailable in the body of the arm. Although this example seems
100innocuous, the problem is most clear when considering functions that take their
101argument by value.
102
103match Some("hi".to_string()) {
104 Some(s) if { drop(s); false } => (),
105 Some(s) => // use s.
106 ...
107}
108
109The value would be dropped in the guard then become unavailable not only in the
110body of that arm but also in all subsequent arms! The solution is to bind by
111reference when using guards or refactor the entire expression, perhaps by
112putting the condition inside the body of the arm.
113"##,
114
115E0162: r##"
116An if-let pattern attempts to match the pattern, and enters the body if the
117match was succesful. If the match is irrefutable (when it cannot fail to match),
118use a regular `let`-binding instead. For instance:
119
120struct Irrefutable(i32);
121let irr = Irrefutable(0);
122
123// This fails to compile because the match is irrefutable.
124if let Irrefutable(x) = irr {
125 // This body will always be executed.
126 foo(x);
127}
128
129// Try this instead:
130let Irrefutable(x) = irr;
131foo(x);
85aaf69f
SL
132"##,
133
9346a6ac
AL
134E0165: r##"
135A while-let pattern attempts to match the pattern, and enters the body if the
136match was succesful. If the match is irrefutable (when it cannot fail to match),
137use a regular `let`-binding inside a `loop` instead. For instance:
138
139struct Irrefutable(i32);
140let irr = Irrefutable(0);
141
142// This fails to compile because the match is irrefutable.
143while let Irrefutable(x) = irr {
144 ...
145}
146
147// Try this instead:
148loop {
149 let Irrefutable(x) = irr;
150 ...
151}
85aaf69f
SL
152"##,
153
9346a6ac
AL
154E0297: r##"
155Patterns used to bind names must be irrefutable. That is, they must guarantee
156that a name will be extracted in all cases. Instead of pattern matching the
157loop variable, consider using a `match` or `if let` inside the loop body. For
158instance:
159
160// This fails because `None` is not covered.
161for Some(x) in xs {
162 ...
163}
164
165// Match inside the loop instead:
166for item in xs {
167 match item {
168 Some(x) => ...
169 None => ...
170 }
171}
85aaf69f 172
9346a6ac
AL
173// Or use `if let`:
174for item in xs {
175 if let Some(x) = item {
176 ...
177 }
178}
85aaf69f
SL
179"##,
180
9346a6ac
AL
181E0301: r##"
182Mutable borrows are not allowed in pattern guards, because matching cannot have
183side effects. Side effects could alter the matched object or the environment
184on which the match depends in such a way, that the match would not be
185exhaustive. For instance, the following would not match any arm if mutable
186borrows were allowed:
187
188match Some(()) {
189 None => { },
190 option if option.take().is_none() => { /* impossible, option is `Some` */ },
191 Some(_) => { } // When the previous match failed, the option became `None`.
192}
193"##,
194
195E0302: r##"
196Assignments are not allowed in pattern guards, because matching cannot have
197side effects. Side effects could alter the matched object or the environment
198on which the match depends in such a way, that the match would not be
199exhaustive. For instance, the following would not match any arm if assignments
200were allowed:
201
202match Some(()) {
203 None => { },
204 option if { option = None; false } { },
205 Some(_) => { } // When the previous match failed, the option became `None`.
206}
85aaf69f
SL
207"##,
208
9346a6ac
AL
209E0303: r##"
210In certain cases it is possible for sub-bindings to violate memory safety.
211Updates to the borrow checker in a future version of Rust may remove this
212restriction, but for now patterns must be rewritten without sub-bindings.
213
214// Code like this...
215match Some(5) {
216 ref op_num @ Some(num) => ...
217 None => ...
218}
219
220// ... should be updated to code like this.
221match Some(5) {
222 Some(num) => {
223 let op_num = &Some(num);
224 ...
225 }
226 None => ...
227}
228
229See also https://github.com/rust-lang/rust/issues/14587
85aaf69f 230"##
9346a6ac 231
85aaf69f 232}
1a4d82fc
JJ
233
234register_diagnostics! {
1a4d82fc
JJ
235 E0009,
236 E0010,
237 E0011,
238 E0012,
239 E0013,
240 E0014,
241 E0015,
242 E0016,
243 E0017,
244 E0018,
245 E0019,
246 E0020,
247 E0022,
c34b1796
AL
248 E0079, // enum variant: expected signed integer constant
249 E0080, // enum variant: constant evaluation error
1a4d82fc
JJ
250 E0109,
251 E0110,
252 E0133,
253 E0134,
254 E0135,
255 E0136,
256 E0137,
257 E0138,
258 E0139,
1a4d82fc 259 E0152,
1a4d82fc
JJ
260 E0158,
261 E0161,
1a4d82fc 262 E0170,
85aaf69f
SL
263 E0261, // use of undeclared lifetime name
264 E0262, // illegal lifetime parameter name
265 E0263, // lifetime name declared twice in same scope
266 E0264, // unknown external lang item
267 E0265, // recursive constant
268 E0266, // expected item
269 E0267, // thing inside of a closure
270 E0268, // thing outside of a loop
271 E0269, // not all control paths return a value
272 E0270, // computation may converge in a function marked as diverging
273 E0271, // type mismatch resolving
274 E0272, // rustc_on_unimplemented attribute refers to non-existent type parameter
275 E0273, // rustc_on_unimplemented must have named format arguments
276 E0274, // rustc_on_unimplemented must have a value
277 E0275, // overflow evaluating requirement
278 E0276, // requirement appears on impl method but not on corresponding trait method
279 E0277, // trait is not implemented for type
280 E0278, // requirement is not satisfied
281 E0279, // requirement is not satisfied
282 E0280, // requirement is not satisfied
283 E0281, // type implements trait but other trait is required
284 E0282, // unable to infer enough type information about
285 E0283, // cannot resolve type
286 E0284, // cannot resolve type
287 E0285, // overflow evaluation builtin bounds
288 E0296, // malformed recursion limit attribute
85aaf69f
SL
289 E0298, // mismatched types between arms
290 E0299, // mismatched types between arms
291 E0300, // unexpanded macro
85aaf69f
SL
292 E0304, // expected signed integer constant
293 E0305, // expected constant
294 E0306, // expected positive integer for repeat count
295 E0307, // expected constant integer for repeat count
296 E0308,
297 E0309, // thing may not live long enough
298 E0310, // thing may not live long enough
299 E0311, // thing may not live long enough
300 E0312, // lifetime of reference outlives lifetime of borrowed content
301 E0313, // lifetime of borrowed pointer outlives lifetime of captured variable
302 E0314, // closure outlives stack frame
303 E0315, // cannot invoke closure outside of its lifetime
c34b1796
AL
304 E0316, // nested quantification of lifetimes
305 E0370 // discriminant overflow
1a4d82fc 306}
85aaf69f
SL
307
308__build_diagnostic_array! { DIAGNOSTICS }