Skip to content

Commit f25cbe6

Browse files
committedMay 6, 2016
Add detailed error explanation for E0389
Cleanup of E0389 Added type-d out version of type in E0389 description
1 parent 3157691 commit f25cbe6

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed
 

Diff for: ‎src/librustc_borrowck/diagnostics.rs

+64-1
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,70 @@ You can read more about cell types in the API documentation:
286286
https://doc.rust-lang.org/std/cell/
287287
"##,
288288

289+
E0389: r##"
290+
An attempt was made to mutate data using a non-mutable reference. This
291+
commonly occurs when attempting to assign to a non-mutable reference of a
292+
mutable reference (`&(&mut T)`).
293+
294+
Example of erroneous code:
295+
296+
```compile_fail
297+
struct FancyNum {
298+
num: u8
299+
}
300+
301+
fn main() {
302+
let mut fancy = FancyNum{ num: 5 };
303+
let fancy_ref = &(&mut fancy);
304+
fancy_ref.num = 6; // error: cannot assign to data in a `&` reference
305+
println!("{}", fancy_ref.num);
306+
}
307+
```
308+
309+
Here, `&mut fancy` is mutable, but `&(&mut fancy)` is not. Creating an
310+
immutable reference to a value borrows it immutably. There can be multiple
311+
references of type `&(&mut T)` that point to the same value, so they must be
312+
immutable to prevent multiple mutable references to the same value.
313+
314+
To fix this, either remove the outer reference:
315+
316+
```
317+
struct FancyNum {
318+
num: u8
319+
}
320+
321+
fn main() {
322+
let mut fancy = FancyNum{ num: 5 };
323+
324+
let fancy_ref = &mut fancy;
325+
// `fancy_ref` is now &mut FancyNum, rather than &(&mut FancyNum)
326+
327+
fancy_ref.num = 6; // No error!
328+
329+
println!("{}", fancy_ref.num);
330+
}
331+
```
332+
333+
Or make the outer reference mutable:
334+
335+
```
336+
struct FancyNum {
337+
num: u8
338+
}
339+
340+
fn main() {
341+
let mut fancy = FancyNum{ num: 5 };
342+
343+
let fancy_ref = &mut (&mut fancy);
344+
// `fancy_ref` is now &mut(&mut FancyNum), rather than &(&mut FancyNum)
345+
346+
fancy_ref.num = 6; // No error!
347+
348+
println!("{}", fancy_ref.num);
349+
}
350+
```
351+
"##,
352+
289353
E0499: r##"
290354
A variable was borrowed as mutable more than once. Erroneous code example:
291355
@@ -434,7 +498,6 @@ http://doc.rust-lang.org/stable/book/references-and-borrowing.html
434498
register_diagnostics! {
435499
E0385, // {} in an aliasable location
436500
E0388, // {} in a static location
437-
E0389, // {} in a `&` reference
438501
E0500, // closure requires unique access to `..` but .. is already borrowed
439502
E0501, // cannot borrow `..`.. as .. because previous closure requires unique access
440503
E0502, // cannot borrow `..`.. as .. because .. is also borrowed as ...

0 commit comments

Comments
 (0)
Please sign in to comment.