From 55752a4bdeae5dad77875b7702a14882fd12725b Mon Sep 17 00:00:00 2001 From: Nathan Kleyn Date: Wed, 12 Aug 2015 20:46:38 +0100 Subject: [PATCH 1/3] Add detailed diagnostics for E0386. This adds detailed diagnostics for E0386, 'cannot assign to data in an immutable container'. --- src/librustc_borrowck/diagnostics.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/librustc_borrowck/diagnostics.rs b/src/librustc_borrowck/diagnostics.rs index 850701e704613..1aa735f8c639e 100644 --- a/src/librustc_borrowck/diagnostics.rs +++ b/src/librustc_borrowck/diagnostics.rs @@ -160,6 +160,27 @@ fn main(){ ``` "##, +E0386: r##" +This error occurs when an attempt is made to mutate the target of a mutable +reference stored inside an immutable container. + +For example, this can happen when storing a `&mut` inside an immutable `Box`: + +``` +let mut x: i64 = 1; +let y: Box<_> = Box::new(&mut x); +**y = 2; // error, cannot assign to data in an immutable container +``` + +This error can be fixed by making the container mutable: + +``` +let mut x: i64 = 1; +let mut y: Box<_> = Box::new(&mut x); +**y = 2; +``` +"##, + E0387: r##" This error occurs when an attempt is made to mutate or mutably reference data that a closure has captured immutably. Examples of this error are shown below: @@ -219,7 +240,6 @@ https://doc.rust-lang.org/std/cell/ register_diagnostics! { E0383, // partial reinitialization of uninitialized structure E0385, // {} in an aliasable location - E0386, // {} in an immutable container E0388, // {} in a static location E0389 // {} in a `&` reference } From 75c6428e1bf2f5564392276642d962163d8a9809 Mon Sep 17 00:00:00 2001 From: Nathan Kleyn Date: Thu, 13 Aug 2015 09:25:56 +0100 Subject: [PATCH 2/3] Add details about types with interior mutability to E0386. Types with interior mutability like `Cell` and `RefCell` can be used to skirt the restriction on mutating mutable values inside an immutable container. --- src/librustc_borrowck/diagnostics.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/librustc_borrowck/diagnostics.rs b/src/librustc_borrowck/diagnostics.rs index 1aa735f8c639e..0d0456cfb42cc 100644 --- a/src/librustc_borrowck/diagnostics.rs +++ b/src/librustc_borrowck/diagnostics.rs @@ -179,6 +179,14 @@ let mut x: i64 = 1; let mut y: Box<_> = Box::new(&mut x); **y = 2; ``` + +It can also be fixed by using a type with interior mutability, such as `Cell` or +`RefCell`: + +``` +let y: Cell<_> = Cell::new(1); +y.set(2); +``` "##, E0387: r##" From b0b29521896d280a2f5340edc901a812ad618266 Mon Sep 17 00:00:00 2001 From: Nathan Kleyn Date: Thu, 13 Aug 2015 10:17:18 +0100 Subject: [PATCH 3/3] Improve interior mutability example for E0386. --- src/librustc_borrowck/diagnostics.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/librustc_borrowck/diagnostics.rs b/src/librustc_borrowck/diagnostics.rs index 0d0456cfb42cc..d7762896debd7 100644 --- a/src/librustc_borrowck/diagnostics.rs +++ b/src/librustc_borrowck/diagnostics.rs @@ -184,7 +184,8 @@ It can also be fixed by using a type with interior mutability, such as `Cell` or `RefCell`: ``` -let y: Cell<_> = Cell::new(1); +let x: i64 = 1; +let y: Box> = Box::new(Cell::new(x)); y.set(2); ``` "##,