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

Dereferencing of mutable &mut paths found in borrowed paths should not be permitted #8625

Closed
nikomatsakis opened this issue Aug 19, 2013 · 3 comments
Assignees
Labels
A-type-system Area: Type system

Comments

@nikomatsakis
Copy link
Contributor

In particular this program ought to be illegal:

struct S<'self> {
    pointer: &'self mut int
}

fn copy_borrowed_ptr2<'a>(p: &'a mut S<'a>) -> S<'a> {
    S { pointer: &mut *p.pointer }
}

fn main() {
    let mut x = 1;

    {
        let mut y = S { pointer: &mut x };
        let z = copy_borrowed_ptr2(&mut y);
        *y.pointer += 1; // ERROR EXPECTED HERE
        *z.pointer += 1;
    }
}

See also #8624

@ghost ghost assigned nikomatsakis Aug 19, 2013
@metajack
Copy link
Contributor

nominating production ready

@bblum
Copy link
Contributor

bblum commented Aug 20, 2013

this should only be illegal for writes, right? reading a dereference of same seems still legal.

@nikomatsakis
Copy link
Contributor Author

On Mon, Aug 19, 2013 at 05:03:45PM -0700, Ben Blum wrote:

this should only be illegal for writes, right? reading a dereference of same seems still legal.

That is correct, reads are always legal.

bors added a commit that referenced this issue Aug 28, 2013
…n-borrowed-loc-2, r=pcwalton

Fixes for #8625 to prevent assigning to `&mut` in borrowed or aliasable locations. The old code was insufficient in that it failed to catch bizarre cases like `& &mut &mut`. 

r? @pnkfelix
flip1995 pushed a commit to flip1995/rust that referenced this issue May 5, 2022
Add `cargo dev rename_lint`

fixes rust-lang#7799

changelog: None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

3 participants