Skip to content

Commit 79a402b

Browse files
Update diagnostics.rs
1 parent 1d7d019 commit 79a402b

File tree

1 file changed

+119
-143
lines changed

1 file changed

+119
-143
lines changed

src/librustc/diagnostics.rs

Lines changed: 119 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,11 @@ 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-
```
79-
let x = Some("s".to_string());
80-
match x {
81-
op_string @ Some(s) => ...
82-
None => ...
83-
}
84-
```
78+
let x = Some("s".to_string());
79+
match x {
80+
op_string @ Some(s) => ...
81+
None => ...
82+
}
8583
8684
See also Error 303.
8785
"##,
@@ -92,12 +90,10 @@ name is bound by move in a pattern, it should also be moved to wherever it is
9290
referenced in the pattern guard code. Doing so however would prevent the name
9391
from being available in the body of the match arm. Consider the following:
9492
95-
```
96-
match Some("hi".to_string()) {
97-
Some(s) if s.len() == 0 => // use s.
98-
...
99-
}
100-
```
93+
match Some("hi".to_string()) {
94+
Some(s) if s.len() == 0 => // use s.
95+
...
96+
}
10197
10298
The variable `s` has type String, and its use in the guard is as a variable of
10399
type String. The guard code effectively executes in a separate scope to the body
@@ -106,13 +102,11 @@ become unavailable in the body of the arm. Although this example seems
106102
innocuous, the problem is most clear when considering functions that take their
107103
argument by value.
108104
109-
```
110-
match Some("hi".to_string()) {
111-
Some(s) if { drop(s); false } => (),
112-
Some(s) => // use s.
113-
...
114-
}
115-
```
105+
match Some("hi".to_string()) {
106+
Some(s) if { drop(s); false } => (),
107+
Some(s) => // use s.
108+
...
109+
}
116110
117111
The value would be dropped in the guard then become unavailable not only in the
118112
body of that arm but also in all subsequent arms! The solution is to bind by
@@ -129,43 +123,37 @@ This limitation may be removed in a future version of Rust.
129123
130124
Wrong example:
131125
132-
```
133-
struct X { x: (), }
134-
135-
let x = Some((X { x: () }, X { x: () }));
136-
match x {
137-
Some((y, ref z)) => {},
138-
None => panic!()
139-
}
140-
```
126+
struct X { x: (), }
127+
128+
let x = Some((X { x: () }, X { x: () }));
129+
match x {
130+
Some((y, ref z)) => {},
131+
None => panic!()
132+
}
141133
142134
You have two solutions:
143135
1. Bind the pattern's values the same way:
144136
145-
```
146-
struct X { x: (), }
147-
148-
let x = Some((X { x: () }, X { x: () }));
149-
match x {
150-
Some((ref y, ref z)) => {},
151-
// or Some((y, z)) => {}
152-
None => panic!()
153-
}
154-
```
137+
struct X { x: (), }
138+
139+
let x = Some((X { x: () }, X { x: () }));
140+
match x {
141+
Some((ref y, ref z)) => {},
142+
// or Some((y, z)) => {}
143+
None => panic!()
144+
}
155145
156146
2. Implement the `Copy` trait for the X structure (however, please
157147
keep in mind that the first solution should be preferred!):
158148
159-
```
160-
#[derive(Clone, Copy)]
161-
struct X { x: (), }
162-
163-
let x = Some((X { x: () }, X { x: () }));
164-
match x {
165-
Some((y, ref z)) => {},
166-
None => panic!()
167-
}
168-
```
149+
#[derive(Clone, Copy)]
150+
struct X { x: (), }
151+
152+
let x = Some((X { x: () }, X { x: () }));
153+
match x {
154+
Some((y, ref z)) => {},
155+
None => panic!()
156+
}
169157
"##,
170158

171159
E0015: r##"
@@ -187,8 +175,8 @@ them yourself.
187175
You can build a free-standing crate by adding `#![no_std]` to the crate
188176
attributes:
189177
190-
#![feature(no_std)]
191-
#![no_std]
178+
#![feature(no_std)]
179+
#![no_std]
192180
193181
See also https://doc.rust-lang.org/book/no-stdlib.html
194182
"##,
@@ -204,11 +192,11 @@ mutex can be declared `static` as well.
204192
205193
If you want to match against a `static`, consider using a guard instead:
206194
207-
static FORTY_TWO: i32 = 42;
208-
match Some(42) {
209-
Some(x) if x == FORTY_TWO => ...
210-
...
211-
}
195+
static FORTY_TWO: i32 = 42;
196+
match Some(42) {
197+
Some(x) if x == FORTY_TWO => ...
198+
...
199+
}
212200
"##,
213201

214202
E0161: r##"
@@ -224,58 +212,54 @@ An if-let pattern attempts to match the pattern, and enters the body if the
224212
match was succesful. If the match is irrefutable (when it cannot fail to match),
225213
use a regular `let`-binding instead. For instance:
226214
227-
```
228-
struct Irrefutable(i32);
229-
let irr = Irrefutable(0);
230-
231-
// This fails to compile because the match is irrefutable.
232-
if let Irrefutable(x) = irr {
233-
// This body will always be executed.
215+
struct Irrefutable(i32);
216+
let irr = Irrefutable(0);
217+
218+
// This fails to compile because the match is irrefutable.
219+
if let Irrefutable(x) = irr {
220+
// This body will always be executed.
221+
foo(x);
222+
}
223+
224+
// Try this instead:
225+
let Irrefutable(x) = irr;
234226
foo(x);
235-
}
236-
237-
// Try this instead:
238-
let Irrefutable(x) = irr;
239-
foo(x);
240-
```
241227
"##,
242228

243229
E0165: r##"
244230
A while-let pattern attempts to match the pattern, and enters the body if the
245231
match was succesful. If the match is irrefutable (when it cannot fail to match),
246232
use a regular `let`-binding inside a `loop` instead. For instance:
247233
248-
```
249-
struct Irrefutable(i32);
250-
let irr = Irrefutable(0);
251-
252-
// This fails to compile because the match is irrefutable.
253-
while let Irrefutable(x) = irr {
254-
...
255-
}
256-
257-
// Try this instead:
258-
loop {
259-
let Irrefutable(x) = irr;
260-
...
261-
}
262-
```
234+
struct Irrefutable(i32);
235+
let irr = Irrefutable(0);
236+
237+
// This fails to compile because the match is irrefutable.
238+
while let Irrefutable(x) = irr {
239+
...
240+
}
241+
242+
// Try this instead:
243+
loop {
244+
let Irrefutable(x) = irr;
245+
...
246+
}
263247
"##,
264248

265249
E0170: r##"
266250
Enum variants are qualified by default. For example, given this type:
267251
268-
enum Method {
269-
GET,
270-
POST
271-
}
252+
enum Method {
253+
GET,
254+
POST
255+
}
272256
273257
you would match it using:
274258
275-
match m {
276-
Method::GET => ...
277-
Method::POST => ...
278-
}
259+
match m {
260+
Method::GET => ...
261+
Method::POST => ...
262+
}
279263
280264
If you don't qualify the names, the code will bind new variables named "GET" and
281265
"POST" instead. This behavior is likely not what you want, so rustc warns when
@@ -284,8 +268,8 @@ that happens.
284268
Qualified names are good practice, and most code works well with them. But if
285269
you prefer them unqualified, you can import the variants into scope:
286270
287-
use Method::*;
288-
enum Method { GET, POST }
271+
use Method::*;
272+
enum Method { GET, POST }
289273
"##,
290274

291275
E0267: r##"
@@ -305,7 +289,7 @@ E0296: r##"
305289
This error indicates that the given recursion limit could not be parsed. Ensure
306290
that the value provided is a positive integer between quotes, like so:
307291
308-
#![recursion_limit="1000"]
292+
#![recursion_limit="1000"]
309293
"##,
310294

311295
E0297: r##"
@@ -314,27 +298,25 @@ that a name will be extracted in all cases. Instead of pattern matching the
314298
loop variable, consider using a `match` or `if let` inside the loop body. For
315299
instance:
316300
317-
```
318-
// This fails because `None` is not covered.
319-
for Some(x) in xs {
320-
...
321-
}
322-
323-
// Match inside the loop instead:
324-
for item in xs {
325-
match item {
326-
Some(x) => ...
327-
None => ...
328-
}
329-
}
330-
331-
// Or use `if let`:
332-
for item in xs {
333-
if let Some(x) = item {
301+
// This fails because `None` is not covered.
302+
for Some(x) in xs {
334303
...
335304
}
336-
}
337-
```
305+
306+
// Match inside the loop instead:
307+
for item in xs {
308+
match item {
309+
Some(x) => ...
310+
None => ...
311+
}
312+
}
313+
314+
// Or use `if let`:
315+
for item in xs {
316+
if let Some(x) = item {
317+
...
318+
}
319+
}
338320
"##,
339321

340322
E0301: r##"
@@ -344,13 +326,11 @@ on which the match depends in such a way, that the match would not be
344326
exhaustive. For instance, the following would not match any arm if mutable
345327
borrows were allowed:
346328
347-
```
348-
match Some(()) {
349-
None => { },
350-
option if option.take().is_none() => { /* impossible, option is `Some` */ },
351-
Some(_) => { } // When the previous match failed, the option became `None`.
352-
}
353-
```
329+
match Some(()) {
330+
None => { },
331+
option if option.take().is_none() => { /* impossible, option is `Some` */ },
332+
Some(_) => { } // When the previous match failed, the option became `None`.
333+
}
354334
"##,
355335

356336
E0302: r##"
@@ -360,36 +340,32 @@ on which the match depends in such a way, that the match would not be
360340
exhaustive. For instance, the following would not match any arm if assignments
361341
were allowed:
362342
363-
```
364-
match Some(()) {
365-
None => { },
366-
option if { option = None; false } { },
367-
Some(_) => { } // When the previous match failed, the option became `None`.
368-
}
369-
```
343+
match Some(()) {
344+
None => { },
345+
option if { option = None; false } { },
346+
Some(_) => { } // When the previous match failed, the option became `None`.
347+
}
370348
"##,
371349

372350
E0303: r##"
373351
In certain cases it is possible for sub-bindings to violate memory safety.
374352
Updates to the borrow checker in a future version of Rust may remove this
375353
restriction, but for now patterns must be rewritten without sub-bindings.
376354
377-
```
378-
// Code like this...
379-
match Some(5) {
380-
ref op_num @ Some(num) => ...
381-
None => ...
382-
}
383-
384-
// After.
385-
match Some("hi".to_string()) {
386-
Some(ref s) => {
387-
let op_string_ref = &Some(&s);
388-
...
355+
// Code like this...
356+
match Some(5) {
357+
ref op_num @ Some(num) => ...
358+
None => ...
359+
}
360+
361+
// After.
362+
match Some("hi".to_string()) {
363+
Some(ref s) => {
364+
let op_string_ref = &Some(&s);
365+
...
366+
}
367+
None => ...
389368
}
390-
None => ...
391-
}
392-
```
393369
394370
The `op_string_ref` binding has type &Option<&String> in both cases.
395371

0 commit comments

Comments
 (0)