-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regions
Description
The following is an example program that shows what I mean:
struct Stuff {
x: int,
y: int
}
// This works fine:
fn borrow_local_mut_to_immutable(stuff: &mut Stuff)
{
let a : &mut int = &mut stuff.x;
let c = copy_out_of_immutable(&stuff.y);
}
// But this does not:
fn borrow_mut_to_immutable(stuff: &mut Stuff)
{
let a = borrow_part(stuff);
let c = copy_out_of_immutable(&stuff.y);
}
fn borrow_part<'a>(stuff: &'a mut Stuff) -> &'a mut int {
return &mut stuff.x;
}
fn copy_out_of_immutable(int_ref: &int) -> int {
return *int_ref;
}
fn main() {
let mut stuff = Stuff { x: 10, y: 20 };
borrow_local_mut_to_immutable(&mut stuff);
borrow_mut_to_immutable(&mut stuff);
}
The compiler error is:
/home/mw/playground-rust/borrow-mut-to-imm.rs:17:34: 17:42 error: cannot borrow `(*stuff).y` as immutable because it is also borrowed as mutable
/home/mw/playground-rust/borrow-mut-to-imm.rs:17 let c = copy_out_of_immutable(&stuff.y);
^~~~~~~~
/home/mw/playground-rust/borrow-mut-to-imm.rs:16:24: 16:29 note: second borrow of `(*stuff).y` occurs here
/home/mw/playground-rust/borrow-mut-to-imm.rs:16 let a = borrow_part(stuff);
^~~~~
I think the borrow checker should allow both cases. During the execution of copy_out_of_immutable()
no mutable alias to neither stuff
nor stuff.y
is accessible, so it is guaranteed that the data behind the immutable reference won't change.
This might be the same as #6268 (although I think it is a bit different).
Metadata
Metadata
Assignees
Labels
A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regions