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

IndexMut incorrectly chosen instead of Index #28935

Closed
wthrowe opened this issue Oct 9, 2015 · 4 comments · Fixed by #97011
Closed

IndexMut incorrectly chosen instead of Index #28935

wthrowe opened this issue Oct 9, 2015 · 4 comments · Fixed by #97011
Labels
A-inference Area: Type inference A-traits Area: Trait system C-bug Category: This is a bug. E-needs-test Call for participation: Writing correctness tests. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@wthrowe
Copy link
Contributor

wthrowe commented Oct 9, 2015

use std::ops::{Index, IndexMut};

struct S;

impl S { fn foo(&self) -> i32 { 0 } }

impl Index<usize> for S {
    type Output = S;
    fn index(&self, _: usize) -> &S { self }
}

impl IndexMut<usize> for S {
    fn index_mut(&mut self, _: usize) -> &mut S { self }
}

fn main() {
    let s = S;
    let _ = &mut s.foo(); // OK
    let _ = &mut (&s[0]).foo(); // OK
    let _ = &mut s[0].foo(); // error: cannot borrow immutable local variable `s` as mutable                                                                   
}

The whole example compiles it the IndexMut implementation is removed.

@gwillen
Copy link
Contributor

gwillen commented Dec 9, 2015

I'm also running into something that appears to be this issue. I'm indexing a Vec, so the implementation of IndexMut is not under my control. I can try to make a reduced example using Vec if it would be helpful. EDIT: oh, it seems that 29869 already contains such an example.

@steveklabnik steveklabnik added T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed A-lang labels Mar 24, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 24, 2017
@PeterHatch
Copy link

Just wanted to note that I encountered this bug.

@shepmaster
Copy link
Member

shepmaster commented Oct 25, 2018

This came up on Stack Overflow. A simplified example:

use std::cell::RefCell;

fn f(v: Vec<RefCell<u8>>) {
   let _t = &mut *v[0].borrow_mut();
}

fn main() {}
error[E0596]: cannot borrow immutable argument `v` as mutable
 --> src/main.rs:4:20
  |
3 | fn f(v: Vec<RefCell<u8>>) {
  |      - consider changing this to `mut v`
4 |     let _t = &mut *v[0].borrow_mut();
  |                    ^ cannot borrow mutably

Workarounds include:

  1. Using extra braces:

    fn f(v: Vec<RefCell<u8>>) {
        let _t = &mut *{ v[0].borrow_mut() };
    }
  2. Calling Index::index explicitly:

    fn f(v: Vec<RefCell<u8>>) {
        use std::ops::Index;
        let _t = &mut *v.index(0).borrow_mut();
    }
  3. Calling RefCell::borrow explicitly:

    fn f(v: Vec<RefCell<u8>>) {
        let _t = &mut *RefCell::borrow_mut(&v[0]);
    }

@jonas-schievink jonas-schievink added the A-inference Area: Type inference label May 8, 2020
@jackh726
Copy link
Member

This compiles now. Marking as needs-test (unsure if there is one that covers this already)

@jackh726 jackh726 added E-needs-test Call for participation: Writing correctness tests. and removed WG-compiler-middle labels Jan 29, 2022
JohnTitor added a commit to JohnTitor/rust that referenced this issue May 13, 2022
bors added a commit to rust-lang-ci/rust that referenced this issue May 13, 2022
…askrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#96154 (Expand core::hint::unreachable_unchecked() docs)
 - rust-lang#96615 (Add a regression test for rust-lang#54779)
 - rust-lang#96982 (fix clippy expect_fun_call)
 - rust-lang#97003 (Remove some unnecessary `rustc_allow_const_fn_unstable` attributes.)
 - rust-lang#97011 (Add regression test for rust-lang#28935)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in cc357bd May 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference A-traits Area: Trait system C-bug Category: This is a bug. E-needs-test Call for participation: Writing correctness tests. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants