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

Can turn immutable into mut with ref mut #52240

Closed
Esption opened this issue Jul 11, 2018 · 3 comments
Closed

Can turn immutable into mut with ref mut #52240

Esption opened this issue Jul 11, 2018 · 3 comments
Labels
E-needs-test Call for participation: Writing correctness tests.

Comments

@Esption
Copy link
Contributor

Esption commented Jul 11, 2018

This lets you get a mutable reference to an immutable value under a specific case.

Code:

enum Foo {
	Bar(i32),
}

fn main() {
	let arr = vec!(Foo::Bar(0));
	if let (Some(Foo::Bar(ref mut val)), _) = (&arr.get(0), 0) {
		*val = 9001;
	}
	match arr[0] {
		Foo::Bar(ref s) => println!("{}", s)
	}
}

From what I can tell, the tuple and at least two depths of Enums are necessary, along with an & on the right side. There's probably other variations, but that seems to work well enough.

Expected output would be a compile error, instead output is 9001

This works on both stable rustc 1.27.1 (5f2b325f6 2018-07-07) and nightly rustc 1.29.0-nightly (e5f6498d3 2018-07-10)

@sfackler
Copy link
Member

@arielb1 would #52232 fix this as well?

@hellow554
Copy link
Contributor

Fixed by #![feature(nll)].

Off: I wonder why we can't ship nll yet. It would solve so many issues right now, because of the match ergonomics sigh
Match ergonomics produces roughly 10% of all errors/unsoundness/ICEs atm (at least it feels like this)

@csmoe csmoe added the NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. label Jul 11, 2018
@kennytm
Copy link
Member

kennytm commented Jul 11, 2018

Yes #52232 correctly fixed this issue.

$ rustup-toolchain-install-master 4700e1188f66fdb4086b7593416b678b8fe935f4
downloading <https://s3-us-west-1.amazonaws.com/rust-lang-ci2/rustc-builds/4700e1188f66fdb4086b7593416b678b8fe935f4/rustc-nightly-x86_64-apple-darwin.tar.xz>...
completed                                                                                                                                                                                                                                  
downloading <https://s3-us-west-1.amazonaws.com/rust-lang-ci2/rustc-builds/4700e1188f66fdb4086b7593416b678b8fe935f4/rust-std-nightly-x86_64-apple-darwin.tar.xz>...
completed                                                                                                                                                                                                                                  
toolchain `4700e1188f66fdb4086b7593416b678b8fe935f4` is successfully installed!

$ rustc +4700e1188f66fdb4086b7593416b678b8fe935f4 1.rs
error[E0596]: cannot borrow field of immutable binding as mutable
 --> 1.rs:7:24
  |
7 |     if let (Some(Foo::Bar(ref mut val)), _) = (&arr.get(0), 0) {
  |                           ^^^^^^^^^^^ cannot mutably borrow field of immutable binding

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.

@kennytm kennytm added the E-needs-test Call for participation: Writing correctness tests. label Jul 11, 2018
@arielb1 arielb1 removed the NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. label Jul 11, 2018
memoryruins added a commit to memoryruins/rust that referenced this issue Oct 12, 2018
memoryruins added a commit to memoryruins/rust that referenced this issue Oct 12, 2018
bors added a commit that referenced this issue Oct 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-needs-test Call for participation: Writing correctness tests.
Projects
None yet
Development

No branches or pull requests

6 participants