Skip to content

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

Closed
@alexcrichton

Description

@alexcrichton
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions