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 through & oddities #11913

Closed
alexcrichton opened this issue Jan 29, 2014 · 8 comments · Fixed by #12117
Closed

Accessing &mut through & oddities #11913

alexcrichton opened this issue Jan 29, 2014 · 8 comments · Fixed by #12117
Milestone

Comments

@alexcrichton
Copy link
Member

This program fails to compile:

struct A<'a> { b: &'a mut int }

fn foo(a: &A) {
    *a.b = 3;
}

fn main() {
    let mut a = 2;
    let b = A { b: &mut a };
    foo(&b);
}

whereas this program succeeds to compile

struct A<'a> { b: &'a mut int }

fn bar(a: &mut int) { *a = 2; }

fn foo(a: &A) {
    bar(a.b);
    bar(a.b);
}

fn main() {
    let mut a = 2;
    let b = A { b: &mut a };
    foo(&b);
}

These seem to be doing the same thing to me, so I would expect both of them to be rejected or both of them be accepted.

cc @nikomatsakis

@flaper87
Copy link
Contributor

Adding more info (perhaps not actually necessary for this bug). Treating everything as mutable succeeds as well.

struct A<'a> { b: &'a mut int }

fn foo(a: &mut A) {
    *a.b = 3;
}

fn main() {
    let mut a = 2;
    let mut b = A { b: &mut a };
    foo(&mut b);
}

@nikomatsakis
Copy link
Contributor

Agreed, this is wrong. The mutable borrow in the second case should definitely be disallowed.

@alexcrichton
Copy link
Member Author

Nominating.

@pnkfelix
Copy link
Member

pnkfelix commented Feb 6, 2014

1.0, P-backcompat-lang

@pnkfelix pnkfelix added this to the 1.0 milestone Feb 6, 2014
@nikomatsakis
Copy link
Contributor

The problem seems to be that we removed RESTR_ALIAS from the list of things to be restricted, probably when removing const loans.

bors added a commit that referenced this issue Feb 9, 2014
…ble-loc, r=pcwalton

Repair a rather embarassingly obvious hole that I created as part of #9629. In particular, prevent `&mut` borrows of data in an aliasable location. This used to be prevented through the restrictions mechanism, but in #9629 I modified those rules incorrectly. 

r? @pcwalton

Fixes #11913
@kkimdev
Copy link

kkimdev commented Jan 4, 2015

@nikomatsakis Would you mind to explain what can go wrong if we allow this? (I started playing with Rust recently, and encountered a problem that this can solve. So I wondered what can go wrong by allowing this)

@Gankra
Copy link
Contributor

Gankra commented Jan 4, 2015

@kkimdev You can get multiple &'s of the same value. If you then allow using an &mut through an & you've basically allowed multiple &mut's to be created. In fact you would be able to trivially bypass mutable aliasing rules with &&mut.

@kkimdev
Copy link

kkimdev commented Jan 4, 2015

@gankro I see, that makes sense!

flip1995 pushed a commit to flip1995/rust that referenced this issue Dec 16, 2023
…-null, r=llogiq

fix(ptr_as_ptr): handle `std::ptr::null{_mut}`

close rust-lang#11066
close rust-lang#11665
close rust-lang#11911

*Please write a short comment explaining your change (or "none" for internal only changes)*

changelog: [`ptr_as_ptr`]: handle `std::ptr::null` and `std::ptr::null_mut`
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

Successfully merging a pull request may close this issue.

6 participants