Skip to content

Commit c773442

Browse files
committed
Auto merge of #24431 - ruud-v-a:explain, r=alexcrichton
This resolves part of #24407.
2 parents 16e1fce + 33dca5e commit c773442

File tree

1 file changed

+94
-5
lines changed

1 file changed

+94
-5
lines changed

src/librustc/diagnostics.rs

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,100 @@ reference when using guards or refactor the entire expression, perhaps by
112112
putting the condition inside the body of the arm.
113113
"##,
114114

115+
E0162: r##"
116+
An if-let pattern attempts to match the pattern, and enters the body if the
117+
match was succesful. If the match is irrefutable (when it cannot fail to match),
118+
use a regular `let`-binding instead. For instance:
119+
120+
struct Irrefutable(i32);
121+
let irr = Irrefutable(0);
122+
123+
// This fails to compile because the match is irrefutable.
124+
if let Irrefutable(x) = irr {
125+
// This body will always be executed.
126+
foo(x);
127+
}
128+
129+
// Try this instead:
130+
let Irrefutable(x) = irr;
131+
foo(x);
132+
"##,
133+
134+
E0165: r##"
135+
A while-let pattern attempts to match the pattern, and enters the body if the
136+
match was succesful. If the match is irrefutable (when it cannot fail to match),
137+
use a regular `let`-binding inside a `loop` instead. For instance:
138+
139+
struct Irrefutable(i32);
140+
let irr = Irrefutable(0);
141+
142+
// This fails to compile because the match is irrefutable.
143+
while let Irrefutable(x) = irr {
144+
...
145+
}
146+
147+
// Try this instead:
148+
loop {
149+
let Irrefutable(x) = irr;
150+
...
151+
}
152+
"##,
153+
154+
E0297: 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. Instead of pattern matching the
157+
loop variable, consider using a `match` or `if let` inside the loop body. For
158+
instance:
159+
160+
// This fails because `None` is not covered.
161+
for Some(x) in xs {
162+
...
163+
}
164+
165+
// Match inside the loop instead:
166+
for item in xs {
167+
match item {
168+
Some(x) => ...
169+
None => ...
170+
}
171+
}
172+
173+
// Or use `if let`:
174+
for item in xs {
175+
if let Some(x) = item {
176+
...
177+
}
178+
}
179+
"##,
180+
181+
E0301: r##"
182+
Mutable borrows are not allowed in pattern guards, because matching cannot have
183+
side effects. Side effects could alter the matched object or the environment
184+
on which the match depends in such a way, that the match would not be
185+
exhaustive. For instance, the following would not match any arm if mutable
186+
borrows were allowed:
187+
188+
match 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+
195+
E0302: r##"
196+
Assignments are not allowed in pattern guards, because matching cannot have
197+
side effects. Side effects could alter the matched object or the environment
198+
on which the match depends in such a way, that the match would not be
199+
exhaustive. For instance, the following would not match any arm if assignments
200+
were allowed:
201+
202+
match Some(()) {
203+
None => { },
204+
option if { option = None; false } { },
205+
Some(_) => { } // When the previous match failed, the option became `None`.
206+
}
207+
"##,
208+
115209
E0303: r##"
116210
In certain cases it is possible for sub-bindings to violate memory safety.
117211
Updates to the borrow checker in a future version of Rust may remove this
@@ -165,8 +259,6 @@ register_diagnostics! {
165259
E0152,
166260
E0158,
167261
E0161,
168-
E0162,
169-
E0165,
170262
E0170,
171263
E0261, // use of undeclared lifetime name
172264
E0262, // illegal lifetime parameter name
@@ -194,12 +286,9 @@ register_diagnostics! {
194286
E0284, // cannot resolve type
195287
E0285, // overflow evaluation builtin bounds
196288
E0296, // malformed recursion limit attribute
197-
E0297, // refutable pattern in for loop binding
198289
E0298, // mismatched types between arms
199290
E0299, // mismatched types between arms
200291
E0300, // unexpanded macro
201-
E0301, // cannot mutable borrow in a pattern guard
202-
E0302, // cannot assign in a pattern guard
203292
E0304, // expected signed integer constant
204293
E0305, // expected constant
205294
E0306, // expected positive integer for repeat count

0 commit comments

Comments
 (0)