archives of rust-dev@mozilla.org (2010-2015)
 help / color / mirror / Atom feed
* [rust-dev] Two mutable pointers to same memory address?
@ 2014-11-26 17:26 grayfox
  2014-11-26 17:40 ` Peter Marheine
  2014-11-26 17:55 ` Daniel Micay
  0 siblings, 2 replies; 3+ messages in thread
From: grayfox @ 2014-11-26 17:26 UTC (permalink / raw)
  To: rust-dev

Hey guys,

I'm really new to Rust (actually I've looked on Rust the last 5 hours the first time) but I think I produced something that shouldn't be possible. From the pointer guide I know that the following code will not compile because in the end I would have two mutable pointers to the same address:

let x = 5i;
let y = &x;
let z = &x;

But in the following snippet I end up with two mutable pointer tmp and *i which point both to the same address:

fn bar<'a>(i: &mut &'a int) {
    let mut tmp = *i;
    println!("{} {}", *tmp, **i);
}

fn foo<'a>() {
    let mut i: &int = &mut 5;
    bar(&mut i);
}

fn main() {
    foo();
}

Maybe I don't understand the concept of the Rust memory concept enough but if I understand everything correct so far this shouldn't compile but it does actually.

Kind regards,

grayfox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [rust-dev] Two mutable pointers to same memory address?
  2014-11-26 17:26 [rust-dev] Two mutable pointers to same memory address? grayfox
@ 2014-11-26 17:40 ` Peter Marheine
  2014-11-26 17:55 ` Daniel Micay
  1 sibling, 0 replies; 3+ messages in thread
From: Peter Marheine @ 2014-11-26 17:40 UTC (permalink / raw)
  To: grayfox; +Cc: rust-dev

This is sound- you aren't actually making more than one mutable reference here.

Stepping through the operations in your example:
1. i is a mutable pointer to an immutable memory location
2. Pass a pointer to i into `bar`, which can mutate `i`.
3. Deref that pointer so you have a copy of `i`, which still points to
an immutable memory location.

If you try to assign through `i` in `bar`, you'll quickly see that the
compiler won't let you. Here's an example in the playpen [1] which
fails to compile with "cannot assign to immutable dereference of
`&`-pointer".

If you were doing this in C, the difference between `let bar = &mut
foo` and `let mut bar: &T = &mut foo` is equivalent to the difference
between `const *` and `const *const`. The former can be changed to
point somewhere else, but neither of them allows writing into the
pointed-to memory. The real trick here is that when we specify the
type of `bar` as `&T` we opt out of mutability- if you remove the type
from that assignment, it's inferred to be `&mut T`.

[1] http://is.gd/FqaGiL

On Wed, Nov 26, 2014 at 10:26 AM, grayfox <grayfox@outerhaven.de> wrote:
> Hey guys,
>
> I'm really new to Rust (actually I've looked on Rust the last 5 hours the first time) but I think I produced something that shouldn't be possible. From the pointer guide I know that the following code will not compile because in the end I would have two mutable pointers to the same address:
>
> let x = 5i;
> let y = &x;
> let z = &x;
>
> But in the following snippet I end up with two mutable pointer tmp and *i which point both to the same address:
>
> fn bar<'a>(i: &mut &'a int) {
>     let mut tmp = *i;
>     println!("{} {}", *tmp, **i);
> }
>
> fn foo<'a>() {
>     let mut i: &int = &mut 5;
>     bar(&mut i);
> }
>
> fn main() {
>     foo();
> }
>
> Maybe I don't understand the concept of the Rust memory concept enough but if I understand everything correct so far this shouldn't compile but it does actually.
>
> Kind regards,
>
> grayfox
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev



-- 
Peter Marheine
Don't Panic

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [rust-dev] Two mutable pointers to same memory address?
  2014-11-26 17:26 [rust-dev] Two mutable pointers to same memory address? grayfox
  2014-11-26 17:40 ` Peter Marheine
@ 2014-11-26 17:55 ` Daniel Micay
  1 sibling, 0 replies; 3+ messages in thread
From: Daniel Micay @ 2014-11-26 17:55 UTC (permalink / raw)
  To: rust-dev

[-- Attachment #1: Type: text/plain, Size: 1244 bytes --]

On 26/11/14 12:26 PM, grayfox wrote:
> Hey guys,
> 
> I'm really new to Rust (actually I've looked on Rust the last 5 hours the first time) but I think I produced something that shouldn't be possible. From the pointer guide I know that the following code will not compile because in the end I would have two mutable pointers to the same address:
> 
> let x = 5i;
> let y = &x;
> let z = &x;
> 
> But in the following snippet I end up with two mutable pointer tmp and *i which point both to the same address:
> 
> fn bar<'a>(i: &mut &'a int) {
>     let mut tmp = *i;
>     println!("{} {}", *tmp, **i);
> }
> 
> fn foo<'a>() {
>     let mut i: &int = &mut 5;
>     bar(&mut i);
> }
> 
> fn main() {
>     foo();
> }
> 
> Maybe I don't understand the concept of the Rust memory concept enough but if I understand everything correct so far this shouldn't compile but it does actually.
> 
> Kind regards,
> 
> grayfox

I see two immutable refs being created from a mutable one, not two
aliasing mutable refs. The type of `tmp` is `&int`, not `&mut int`. The
fact that the variable is mutable just means that another immutable
pointer can be assigned to it - it's unnecessary, as is the `mut` on `i`
in `foo`.


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-11-26 17:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-26 17:26 [rust-dev] Two mutable pointers to same memory address? grayfox
2014-11-26 17:40 ` Peter Marheine
2014-11-26 17:55 ` Daniel Micay

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox