Skip to content

Autoderef requests a mutable slot when it shouldn't #13066

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

Closed
alexcrichton opened this issue Mar 22, 2014 · 1 comment
Closed

Autoderef requests a mutable slot when it shouldn't #13066

alexcrichton opened this issue Mar 22, 2014 · 1 comment

Comments

@alexcrichton
Copy link
Member

struct Foo<T> {
    a: T,
}

struct Bar<'a, T> {
    foo: &'a Foo<T>,
}

impl<T> Foo<T> {
    fn bar<'a>(&'a self) -> Bar<'a, T> { fail!() }
}

impl<'a, T> Deref<T> for Bar<'a, T> {
    fn deref<'a>(&'a self) -> &'a T { fail!() }
}
impl<'a, T> DerefMut<T> for Bar<'a, T> {
    fn deref_mut<'a>(&'a mut self) -> &'a mut T { fail!() }
}

fn main() {
    let a = Foo { a: Foo { a: 2 } };
    let b = a.bar();
    let c = b.bar();
}
$ rustc foo.rs
foo.rs:23:13: 23:14 error: cannot borrow immutable local variable `b` as mutable
foo.rs:23     let c = b.bar();
                      ^
error: aborting due to previous error

This should not require b to be mutable. If I replace b.bar() with b.deref().bar() it will compile ok.

cc @eddyb

@alexcrichton
Copy link
Member Author

Aha! I had a feeling there was already an issue on this.

Closing as a dupe of #12825

flip1995 pushed a commit to flip1995/rust that referenced this issue Apr 3, 2025
Closes rust-lang#13066 (tests that are ignored with no reason message).

changelog: Add `ignore_without_reason` lint
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

1 participant