Skip to content

Commit a406627

Browse files
committed
Auto merge of #27758 - nathankleyn:diagnostics-386, r=Manishearth
This adds detailed diagnostics for E0386, 'cannot assign to data in an immutable container'. This is part of #24407. r? @Manishearth
2 parents 7b7fc67 + b0b2952 commit a406627

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/librustc_borrowck/diagnostics.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,36 @@ fn main(){
181181
```
182182
"##,
183183

184+
E0386: r##"
185+
This error occurs when an attempt is made to mutate the target of a mutable
186+
reference stored inside an immutable container.
187+
188+
For example, this can happen when storing a `&mut` inside an immutable `Box`:
189+
190+
```
191+
let mut x: i64 = 1;
192+
let y: Box<_> = Box::new(&mut x);
193+
**y = 2; // error, cannot assign to data in an immutable container
194+
```
195+
196+
This error can be fixed by making the container mutable:
197+
198+
```
199+
let mut x: i64 = 1;
200+
let mut y: Box<_> = Box::new(&mut x);
201+
**y = 2;
202+
```
203+
204+
It can also be fixed by using a type with interior mutability, such as `Cell` or
205+
`RefCell`:
206+
207+
```
208+
let x: i64 = 1;
209+
let y: Box<Cell<_>> = Box::new(Cell::new(x));
210+
y.set(2);
211+
```
212+
"##,
213+
184214
E0387: r##"
185215
This error occurs when an attempt is made to mutate or mutably reference data
186216
that a closure has captured immutably. Examples of this error are shown below:
@@ -239,7 +269,6 @@ https://doc.rust-lang.org/std/cell/
239269

240270
register_diagnostics! {
241271
E0385, // {} in an aliasable location
242-
E0386, // {} in an immutable container
243272
E0388, // {} in a static location
244273
E0389 // {} in a `&` reference
245274
}

0 commit comments

Comments
 (0)