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

Should the mutable borrow of self in method call range over the arguments? #20403

Closed
honzasp opened this issue Jan 1, 2015 · 2 comments
Closed

Comments

@honzasp
Copy link

honzasp commented Jan 1, 2015

When a &mut self method is called, the receiver is mutably borrowed before the arguments are evaluated, so the following program does not compile:

fn main() {
  let mut vec = vec![1, 2, 3];
  vec.push(vec.len());
  assert_eq!(vec, vec![1, 2, 3, 4]);
}

But when the argument explicitly evaluated before the call in a let statement, is compiles just fine:

fn main() {
  let mut vec = vec![1, 2, 3];
  let vec_len = vec.len();
  vec.push(vec_len);
  assert_eq!(vec, vec![1, 2, 3, 3]);
}

I am not sure if there is a good reason for the first program to be rejected, I would expect e1(e2) to be equivalent to { let x = e2; e1(x) }.

@Gankra
Copy link
Contributor

Gankra commented Jan 1, 2015

I believe this sort of thing is intended to be resolved by SEME regions: rust-lang/rfcs#396

@Gankra Gankra closed this as completed Jan 1, 2015
@Gankra
Copy link
Contributor

Gankra commented Jan 1, 2015

In particular, motivating example 3: https://github.com/zwarich/rfcs/blob/seme-regions/active/0000-seme-regions.md

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