Closed
Description
Meta
$ rustc -V
rustc 1.13.0-dev (528c6f3ed 2016-08-25)
STR
This code compiles and runs successfully
use std::cell::RefCell;
fn main() {
let r = 0;
let x = RefCell::new((&r,));
let val = x.borrow().0;
println!("{}", val);
x.borrow_mut();
}
But adding a &_
type annotation causes a double borrow panic:
use std::cell::RefCell;
fn main() {
let r = 0;
let x = RefCell::new((&r,));
let val: &_ = x.borrow().0;
println!("{}", val);
x.borrow_mut();
}
Notes
"Specification-wise", the type annotation changing the code behaviour is rather dubious. The root cause is this gem of a function.
The behaviour is actually documented in the code:
// Rule B. `let x: &[...] = [foo().x]`. The rvalue `[foo().x]`
// would have an extended lifetime, but not `foo()`.
That "common case" does not apply in today's Rust, of course.
Of course, changing this now would be a [breaking-change], but we are landing a similar significant change by @KiChjang (#36029). I will post my take of the rules on a rust-memory-model issue, because @ubsan nicely asked me to (maybe that should be on the RFCs repo instead?).