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

Arithmetic makes borrow checker overly restrictive #29743

Closed
dotdash opened this Issue Nov 10, 2015 · 14 comments

Comments

Projects
None yet
9 participants
@dotdash
Contributor

dotdash commented Nov 10, 2015

UPDATE: This was fixed by #47167 but still needs a regression test to be added.


fn main() {
    let mut i = [1,2,3];
    i[i[0]] = 0; // Works
    i[i[0] - 1] = 0; // Is rejected
}

I had expect the - 1 not to make any difference, but the borrow checker complains with the following error:

<anon>:4:7: 4:11 error: cannot use `i[..]` because it was mutably borrowed
<anon>:4     i[i[0] - 1] = 0;
               ^~~~
<anon>:4:5: 4:6 note: borrow of `i` occurs here
<anon>:4     i[i[0] - 1] = 0;

@steveklabnik steveklabnik added the A-lang label Nov 11, 2015

@steveklabnik

This comment has been minimized.

Member

steveklabnik commented Nov 11, 2015

@rust-lang/lang, is this behavior expected? I wonder if there's something weird going on here

@nikomatsakis

This comment has been minimized.

Contributor

nikomatsakis commented Nov 12, 2015

Does seem odd. I suspect that the borrow checker is (incorrectly) still locked into the days when indices were passed by reference, rather than by value.

@nikomatsakis

This comment has been minimized.

Contributor

nikomatsakis commented Nov 12, 2015

triage: P-medium

@nikomatsakis

This comment has been minimized.

Contributor

nikomatsakis commented Nov 12, 2015

Likely to be fixed by MIR, though.

@sanxiyn

This comment has been minimized.

Member

sanxiyn commented Aug 4, 2016

Still reproduces.

@nikomatsakis

This comment has been minimized.

Contributor

nikomatsakis commented Aug 4, 2016

(Note: would be fixed by a MIR-based borrowck is what I meant, which is not yet done.)

@cyplo

This comment has been minimized.

Contributor

cyplo commented Nov 1, 2017

Still seems to be a problem on rustc 1.23.0-nightly (8b22e70 2017-10-31) - not sure if expected ?

error[E0503]: cannot use `i[..]` because it was mutably borrowed
 --> main.rs:4:7
  |
4 |     i[i[0] - 1] = 0; // Is rejected
  |     - ^^^^ use of borrowed `i`
  |     |
  |     borrow of `i` occurs here

error: aborting due to previous error
@pnkfelix

This comment has been minimized.

Member

pnkfelix commented Dec 20, 2017

Hmm MIR-borrowck does not seem to address this case, at least not out-of-the-box

@nikomatsakis

This comment has been minimized.

Contributor

nikomatsakis commented Jan 3, 2018

The first question is to decide, I suppose, what the expected behavior is =)

@nikomatsakis

This comment has been minimized.

Contributor

nikomatsakis commented Jan 3, 2018

In other words, I think I would expect consistent behavior, but I could see how this might wind up as an error in both cases.

@nikomatsakis

This comment has been minimized.

Contributor

nikomatsakis commented Jan 4, 2018

Well, based on this play test case, we do not get consistent behavior.

@nikomatsakis

This comment has been minimized.

Contributor

nikomatsakis commented Jan 4, 2018

I wonder if #47167 will affect this, actually.

@nikomatsakis

This comment has been minimized.

Contributor

nikomatsakis commented Jan 5, 2018

Seems like #47167 fixes it.

@nikomatsakis

This comment has been minimized.

Contributor

nikomatsakis commented Jan 18, 2018

It now works on nightly. Marking as E-needstest. Removing from WG-compiler-nll, since this was not specific to NLL in any way.

euclio added a commit to euclio/rust that referenced this issue Oct 4, 2018

bors added a commit that referenced this issue Oct 7, 2018

Auto merge of #54823 - euclio:needs-test, r=alexcrichton
Add tests for some E-needstest issues

Fixes #28134.
Fixes #24338.
Fixes #29743.

@bors bors closed this in #54823 Oct 7, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment