@@ -286,6 +286,70 @@ You can read more about cell types in the API documentation:
286286https://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+
289353E0499 : r##"
290354A 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
434498register_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