You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Misleading error message when mutably referencing members of cloned &T when T does not implement Clone #34629
error[E0596]: cannot borrow `sm.x` as mutable, as it is behind a `&` reference
--> src/main.rs:10:9
|
10 | foo(&mut sm.x);
| ^^^^^^^^^ `sm` is a `&` reference, so the data it refers to cannot be borrowed as mutable
|
help: consider specifying this binding's type
|
9 | let mut sm: &mut Str = sr.clone();
| ++++++++++
Applying the suggestion:
error[E0308]: mismatched types
--> src/main.rs:9:28
|
9 | let mut sm: &mut Str = sr.clone();
| -------- ^^^^^^^^^^ types differ in mutability
| |
| expected due to this
|
= note: expected mutable reference `&mut Str`
found reference `&Str`
If you explicitly give sm the type Str expecting the clone to work:
error[E0308]: mismatched types
--> src/main.rs:9:23
|
9 | let mut sm: Str = sr.clone();
| --- ^^^^^^^^^^ expected `Str`, found `&Str`
| |
| expected due to this
|
note: `Str` does not implement `Clone`, so `&Str` was cloned instead
--> src/main.rs:9:23
|
9 | let mut sm: Str = sr.clone();
| ^^
help: consider annotating `Str` with `#[derive(Clone)]`
|
2 + #[derive(Clone)]
3 | struct Str {
use std::collections::BTreeMap;use std::collections::HashSet;#[derive(Debug,Eq,PartialEq,Hash)]enumDay{Mon,}structClass{days:BTreeMap<u32,HashSet<Day>>}implClass{fndo_stuff(&self){for(_, v)in&self.days{letmut x = v.clone();let y:Vec<Day> = x.drain().collect();println!("{:?}", x);}}}fnfail(){let c = Class{days:BTreeMap::new()};
c.do_stuff();}
error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
--> f75.rs:17:31
|
17 | let y: Vec<Day> = x.drain().collect();
| ^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable
|
help: consider specifying this binding's type
|
16 | let mut x: &mut HashSet<Day> = v.clone();
| +++++++++++++++++++
error[E0308]: mismatched types
--> f75.rs:16:44
|
16 | let mut x: &mut HashSet<Day> = v.clone();
| ----------------- ^^^^^^^^^ types differ in mutability
| |
| expected due to this
|
= note: expected mutable reference `&mut HashSet<Day>`
found reference `&HashSet<Day>`
E0308]: mismatched types
--> f75.rs:16:39
|
16 | let mut x: HashSet<Day> = v.clone();
| ------------ ^^^^^^^^^ expected `HashSet<Day>`, found `&HashSet<Day>`
| |
| expected due to this
|
= note: expected struct `HashSet<Day>`
found reference `&HashSet<Day>`
note: `HashSet<Day>` does not implement `Clone`, so `&HashSet<Day>` was cloned instead
--> f75.rs:16:39
|
16 | let mut x: HashSet<Day> = v.clone();
| ^
Activity
GuillaumeGomez commentedon Jul 4, 2016
The biggest question I have is: how can the
clonemethod be called if it isn't defined onStrtype?arielb1 commentedon Jul 4, 2016
The issue is that
<&Str>::cloneis called through an autoref.pnkfelix commentedon Jan 31, 2019
cc #48677, #34896
estebank commentedon Oct 19, 2023
Current output:
Applying the suggestion:
If you explicitly give
smthe typeStrexpecting the clone to work:estebank commentedon Nov 21, 2023
Another case from #41825:
Rollup merge of rust-lang#118076 - estebank:issue-109429, r=davidtwco
Auto merge of rust-lang#118076 - estebank:issue-109429, r=davidtwco