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

Accessing &mut inside of @mut requires first borrowing the @mut #6269

Closed
nikomatsakis opened this issue May 6, 2013 · 3 comments
Closed
Labels
A-lifetimes Area: Lifetimes / regions A-typesystem Area: The type system

Comments

@nikomatsakis
Copy link
Contributor

Right now, if you have an &mut pointer inside of an @mut data structure, you cannot access it without first borrowing the @mut to &mut:

struct Foo { x: &mut int }
fn foo(m: @mut Foo) {
    *m.x += 1; // ERROR

    let n = &mut *m; // borrow m as &mut
    *n.x += 1; // OK
}

The reason for this is that we require a unique access path to mutate or re-borrow &mut pointers, and @mut is aliasable. Once the @mut is borrowed to &mut, though, we know that no mutable aliases exist, because otherwise the borrow would have failed. We could just however accept the original code and insert a write guard check ourselves. I don't do this now because it would require various subtle bits of code to make sure that the automatically inserted borrow has the appropriate lifetime to guarantee soundness. Still, it's kind of annoying.

cc #5074

@graydon
Copy link
Contributor

graydon commented Aug 1, 2013

Nominating for well-defined. Though I think this is absolutely fine as-is. If anything I still sorta feel like & doesn't belong inside @ at all. But that's a different kettle of fish.

@nikomatsakis
Copy link
Contributor Author

I thought we had decided on (and possibly even implemented?) a rule to prohibit & from @, actually? @pcwalton didn't I see such a commit go by, or just prep commits?

@catamorphism
Copy link
Contributor

Obsolete, closing

flip1995 pushed a commit to flip1995/rust that referenced this issue Nov 20, 2020
Fix map_clone with deref and clone

changelog: Fix map_clone false positive with deref coercion

Fixes rust-lang#6239
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions A-typesystem Area: The type system
Projects
None yet
Development

No branches or pull requests

3 participants