Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit re-borrows of &mut references where a move would be expected #25899

Closed
elinorbgr opened this issue May 30, 2015 · 2 comments
Closed

Comments

@elinorbgr
Copy link
Contributor

Consider this snippet of code:

fn main() {
    let a: &mut i32 = &mut 0;
    { let b = a; }
    let c = a;
}

It properly fails to compile, as a is moved into b.

However this one compiles fine:

fn main() {
    let a: &mut i32 = &mut 0;
    { let b: &mut i32 = a; }
    let c = a;
}

Adding a type annotation to b implicitly changed the move of a into a re-borrow. This behavior actually makes sense: if b was annotated &i32, a move would have not been possible, and a re-borrow would have been mandatory.

It is pretty harmless and does not pose any safety issue (actually I think having a "always re-borrow" policy would still be safe), but is still an implicit behavior that is not documented (at least I didn't find any documentation about it).

(Comes from a SO question: http://stackoverflow.com/q/30535529/2536143 )

@huonw huonw added the A-docs label May 30, 2015
@steveklabnik
Copy link
Member

/cc @nikomatsakis , we should talk about what exactly the reborrow rules are

@steveklabnik
Copy link
Member

I'm not planning on significant changes to the book, so I've made a note in the appropriate part of the next-gen repo to make sure we talk about re-borrowing. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants