|
10 | 10 |
|
11 | 11 | #![allow(non_snake_case)]
|
12 | 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. |
13 | 16 | register_long_diagnostics! {
|
14 |
| - E0001: r##" |
15 |
| - This error suggests that the expression arm corresponding to the noted pattern |
16 |
| - will never be reached as for all possible values of the expression being matched, |
17 |
| - one of the preceding patterns will match. |
18 | 17 |
|
19 |
| - This means that perhaps some of the preceding patterns are too general, this |
20 |
| - one is too specific or the ordering is incorrect. |
| 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 { }`. |
21 | 35 | "##,
|
22 | 36 |
|
23 |
| - E0003: r##" |
24 |
| - Not-a-Number (NaN) values can not be compared for equality and hence can never match |
25 |
| - the input to a match expression. To match against NaN values, you should instead use |
26 |
| - the `is_nan` method in a guard, as in: x if x.is_nan() => ... |
| 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() => ... |
27 | 41 | "##,
|
28 | 42 |
|
29 |
| - E0004: r##" |
30 |
| - This error indicates that the compiler can not guarantee a matching pattern for one |
31 |
| - or more possible inputs to a match expression. Guaranteed matches are required in order |
32 |
| - to assign values to match expressions, or alternatively, determine the flow of execution. |
| 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. |
33 | 48 |
|
34 |
| - If you encounter this error you must alter your patterns so that every possible value of |
35 |
| - the input type is matched. For types with a small number of variants (like enums) you |
36 |
| - should probably cover all cases explicitly. Alternatively, the underscore `_` wildcard |
37 |
| - pattern can be added after all other patterns to match "anything else". |
| 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". |
38 | 54 | "##,
|
39 | 55 |
|
40 |
| - // FIXME: Remove duplication here? |
41 |
| - E0005: r##" |
42 |
| - Patterns used to bind names must be irrefutable, that is, they must guarantee that a |
43 |
| - name will be extracted in all cases. If you encounter this error you probably need |
44 |
| - to use a `match` or `if let` to deal with the possibility of failure. |
| 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. |
45 | 61 | "##,
|
46 | 62 |
|
47 |
| - E0006: r##" |
48 |
| - Patterns used to bind names must be irrefutable, that is, they must guarantee that a |
49 |
| - name will be extracted in all cases. If you encounter this error you probably need |
50 |
| - to use a `match` or `if let` to deal with the possibility of failure. |
| 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 | +E0303: r##" |
| 116 | +In certain cases it is possible for sub-bindings to violate memory safety. |
| 117 | +Updates to the borrow checker in a future version of Rust may remove this |
| 118 | +restriction, but for now patterns must be rewritten without sub-bindings. |
| 119 | +
|
| 120 | +// Code like this... |
| 121 | +match Some(5) { |
| 122 | + ref op_num @ Some(num) => ... |
| 123 | + None => ... |
| 124 | +} |
| 125 | +
|
| 126 | +// ... should be updated to code like this. |
| 127 | +match Some(5) { |
| 128 | + Some(num) => { |
| 129 | + let op_num = &Some(num); |
| 130 | + ... |
| 131 | + } |
| 132 | + None => ... |
| 133 | +} |
| 134 | +
|
| 135 | +See also https://github.com/rust-lang/rust/issues/14587 |
51 | 136 | "##
|
| 137 | + |
52 | 138 | }
|
53 | 139 |
|
54 | 140 | register_diagnostics! {
|
55 |
| - E0002, |
56 |
| - E0007, |
57 |
| - E0008, |
58 | 141 | E0009,
|
59 | 142 | E0010,
|
60 | 143 | E0011,
|
@@ -117,7 +200,6 @@ register_diagnostics! {
|
117 | 200 | E0300, // unexpanded macro
|
118 | 201 | E0301, // cannot mutable borrow in a pattern guard
|
119 | 202 | E0302, // cannot assign in a pattern guard
|
120 |
| - E0303, // pattern bindings are not allowed after an `@` |
121 | 203 | E0304, // expected signed integer constant
|
122 | 204 | E0305, // expected constant
|
123 | 205 | E0306, // expected positive integer for repeat count
|
|
0 commit comments