Open jonas-schievink opened 4 years ago
Another repro from #73150:
error[E0499]: cannot borrow `x` as mutable more than once at a time --> src/main.rs:12:13 | 10 | let result = foo(&mut x); | ------ first mutable borrow occurs here 11 | drop(result); 12 | let _ = x.bar(); | ^ | | | second mutable borrow occurs here | first borrow later used here
This seems like a reasonable category of error to encounter (where the same lifetime is used in an
fn
signature causing non-obvious lifetime bonding), that I believe we should tackle it specifically. I believe it would ideally look closer to the following:error[E0499]: cannot borrow `x` as mutable more than once at a time --> src/main.rs:12:13 | 3 | fn foo<'a>(_: &'a mut S<'a>) {} | ------------- this type requires that the borrow last as long as `S<'a>`, consider using different lifetimes 4 | 5 | impl<'a> S<'a> { 6 | fn bar(&mut self) {} | --------- this requires a mutable borrow ... 10 | let result = foo(&mut x); | ------ first mutable borrow occurs here 11 | drop(result); | ------------ `x` is no longer accessible after this call 12 | let _ = x.bar(); | ^ --- this call mutably borrows `x` after it is no longer accessible | | | second mutable borrow occurs here
This code gives a somewhat hard to understand error message:
The error is correct, as
ouroboros_me
stores a self-reference ins
, but it also happens when not doing that purely due to the method signature. That's also correct, but users frequently write such a method accidentally and are then left to wonder why the variable stays borrowed for seemingly no reason.It would be good to give a more targeted explanation as to why the borrow stays active for longer than expected.