Skip to content

Commit 2f8cbec

Browse files
committed
Rollup merge of rust-lang#24523 - GuillaumeGomez:clean-error-codes, r=Manishearth
2 parents 21f278a + 2ddc8f5 commit 2f8cbec

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

src/librustc/diagnostics.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ the following is invalid as it requires the entire Option<String> to be moved
7575
into a variable called `op_string` while simultaneously requiring the inner
7676
String to be moved into a variable called `s`.
7777
78+
```
7879
let x = Some("s".to_string());
7980
match x {
8081
op_string @ Some(s) => ...
8182
None => ...
8283
}
84+
```
8385
8486
See also Error 303.
8587
"##,
@@ -90,10 +92,12 @@ name is bound by move in a pattern, it should also be moved to wherever it is
9092
referenced in the pattern guard code. Doing so however would prevent the name
9193
from being available in the body of the match arm. Consider the following:
9294
95+
```
9396
match Some("hi".to_string()) {
9497
Some(s) if s.len() == 0 => // use s.
9598
...
9699
}
100+
```
97101
98102
The variable `s` has type String, and its use in the guard is as a variable of
99103
type String. The guard code effectively executes in a separate scope to the body
@@ -102,11 +106,13 @@ become unavailable in the body of the arm. Although this example seems
102106
innocuous, the problem is most clear when considering functions that take their
103107
argument by value.
104108
109+
```
105110
match Some("hi".to_string()) {
106111
Some(s) if { drop(s); false } => (),
107112
Some(s) => // use s.
108113
...
109114
}
115+
```
110116
111117
The value would be dropped in the guard then become unavailable not only in the
112118
body of that arm but also in all subsequent arms! The solution is to bind by
@@ -219,8 +225,10 @@ them yourself.
219225
You can build a free-standing crate by adding `#![no_std]` to the crate
220226
attributes:
221227
228+
```
222229
#![feature(no_std)]
223230
#![no_std]
231+
```
224232
225233
See also https://doc.rust-lang.org/book/no-stdlib.html
226234
"##,
@@ -236,11 +244,13 @@ mutex can be declared `static` as well.
236244
237245
If you want to match against a `static`, consider using a guard instead:
238246
247+
```
239248
static FORTY_TWO: i32 = 42;
240249
match Some(42) {
241250
Some(x) if x == FORTY_TWO => ...
242251
...
243252
}
253+
```
244254
"##,
245255

246256
E0161: r##"
@@ -256,6 +266,7 @@ An if-let pattern attempts to match the pattern, and enters the body if the
256266
match was succesful. If the match is irrefutable (when it cannot fail to match),
257267
use a regular `let`-binding instead. For instance:
258268
269+
```
259270
struct Irrefutable(i32);
260271
let irr = Irrefutable(0);
261272
@@ -268,13 +279,15 @@ if let Irrefutable(x) = irr {
268279
// Try this instead:
269280
let Irrefutable(x) = irr;
270281
foo(x);
282+
```
271283
"##,
272284

273285
E0165: r##"
274286
A while-let pattern attempts to match the pattern, and enters the body if the
275287
match was succesful. If the match is irrefutable (when it cannot fail to match),
276288
use a regular `let`-binding inside a `loop` instead. For instance:
277289
290+
```
278291
struct Irrefutable(i32);
279292
let irr = Irrefutable(0);
280293
@@ -288,22 +301,27 @@ loop {
288301
let Irrefutable(x) = irr;
289302
...
290303
}
304+
```
291305
"##,
292306

293307
E0170: r##"
294308
Enum variants are qualified by default. For example, given this type:
295309
310+
```
296311
enum Method {
297312
GET,
298313
POST
299314
}
315+
```
300316
301317
you would match it using:
302318
319+
```
303320
match m {
304321
Method::GET => ...
305322
Method::POST => ...
306323
}
324+
```
307325
308326
If you don't qualify the names, the code will bind new variables named "GET" and
309327
"POST" instead. This behavior is likely not what you want, so rustc warns when
@@ -312,8 +330,10 @@ that happens.
312330
Qualified names are good practice, and most code works well with them. But if
313331
you prefer them unqualified, you can import the variants into scope:
314332
333+
```
315334
use Method::*;
316335
enum Method { GET, POST }
336+
```
317337
"##,
318338

319339
E0267: r##"
@@ -333,7 +353,9 @@ E0296: r##"
333353
This error indicates that the given recursion limit could not be parsed. Ensure
334354
that the value provided is a positive integer between quotes, like so:
335355
356+
```
336357
#![recursion_limit="1000"]
358+
```
337359
"##,
338360

339361
E0297: r##"
@@ -342,6 +364,7 @@ that a name will be extracted in all cases. Instead of pattern matching the
342364
loop variable, consider using a `match` or `if let` inside the loop body. For
343365
instance:
344366
367+
```
345368
// This fails because `None` is not covered.
346369
for Some(x) in xs {
347370
...
@@ -361,6 +384,7 @@ for item in xs {
361384
...
362385
}
363386
}
387+
```
364388
"##,
365389

366390
E0301: r##"
@@ -370,11 +394,13 @@ on which the match depends in such a way, that the match would not be
370394
exhaustive. For instance, the following would not match any arm if mutable
371395
borrows were allowed:
372396
397+
```
373398
match Some(()) {
374399
None => { },
375400
option if option.take().is_none() => { /* impossible, option is `Some` */ },
376401
Some(_) => { } // When the previous match failed, the option became `None`.
377402
}
403+
```
378404
"##,
379405

380406
E0302: r##"
@@ -384,21 +410,24 @@ on which the match depends in such a way, that the match would not be
384410
exhaustive. For instance, the following would not match any arm if assignments
385411
were allowed:
386412
413+
```
387414
match Some(()) {
388415
None => { },
389416
option if { option = None; false } { },
390417
Some(_) => { } // When the previous match failed, the option became `None`.
391418
}
419+
```
392420
"##,
393421

394422
E0303: r##"
395423
In certain cases it is possible for sub-bindings to violate memory safety.
396424
Updates to the borrow checker in a future version of Rust may remove this
397425
restriction, but for now patterns must be rewritten without sub-bindings.
398426
399-
// Before.
400-
match Some("hi".to_string()) {
401-
ref op_string_ref @ Some(ref s) => ...
427+
```
428+
// Code like this...
429+
match Some(5) {
430+
ref op_num @ Some(num) => ...
402431
None => ...
403432
}
404433
@@ -410,6 +439,7 @@ match Some("hi".to_string()) {
410439
}
411440
None => ...
412441
}
442+
```
413443
414444
The `op_string_ref` binding has type &Option<&String> in both cases.
415445

0 commit comments

Comments
 (0)