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

inconsistent behaviour regarding @mut and &mut in managed and borrowed reference types #5720

Closed
dhardy opened this issue Apr 4, 2013 · 2 comments

Comments

@dhardy
Copy link
Contributor

dhardy commented Apr 4, 2013

Using @int instead of @mut int in a type causes a compile error, but using &int instead of &mut int does not.

let o = @mut 5;
let mut m : @int = o;       // error: mismatched types: expected `@int` but found `@mut <VI5>` (values differ in mutability)
let mut b : &int = o;       // compiles (with type '&int' or '&mut int')?
let mut c : &int;           // to contrast...
c = o;      // error: mismatched types: expected `&int` but found `@mut int` (expected &-ptr but found @-ptr)

This could be intended behaviour, but I presume not since as I understand it variable typing is either automatic (from the rvalue) or explicit, not some mixture of the two.

@nikomatsakis
Copy link
Contributor

This is expected behavior. The reason is that you are allowed to take immutable references to mutable data, which causes the mutable data to be frozen. (This is what is happening when you assign to &int). But you can only freeze for a limited lifetime. The assignment to @int is disallowed because that would be a permanent freeze. It's possible we could allow this, but we don't today.

@dhardy
Copy link
Contributor Author

dhardy commented Apr 5, 2013

Seems very very strange to me (that assignment to variables b and c is not handled equivalently), but as you say...

I'll try to update the tutorial to explain this. No, maybe I won't just yet, there's enough other documentation on Rust I haven't yet read.

bors added a commit that referenced this issue Apr 6, 2013
This is some stuff which seemed to be missing to me (though I haven't read everything yet so hope I haven't missed the relevant section).

A similar addition for borrowing handles is needed, but #5720 stumped me.

Comments welcome.
flip1995 pushed a commit to flip1995/rust that referenced this issue Aug 28, 2020
Add unnecessary lazy evaluation lint

changelog: Add [`unnecessary_lazy_evaluations`] lint that checks for usages of `unwrap_or_else` and similar functions that can be simplified.

Closes rust-lang#5715
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

2 participants