Skip to content

Autoderef chooses deref() when deref_mut() is needed for ref mut pattern #15609

Closed
@kmcallister

Description

@kmcallister
use std::cell::RefCell;

struct S {
    node: E,
}

enum E {
    Foo(uint),
    Bar,
}

fn main() {
    let x = RefCell::new(S { node: Foo(0) });

    let mut b = x.borrow_mut();
    match b.node {
        Foo(ref mut n) => *n += 1,
        _ => (),
    }
}
$ rustc -v
rustc 0.11.0 (49bc17bfdd7143909aede81652d7d624cecd8a70 2014-07-07 17:16:34 +0000)
$ rustc foo.rs
foo.rs:17:13: 17:22 error: cannot borrow immutable anonymous field as mutable
foo.rs:17         Foo(ref mut n) => *n += 1,
                      ^~~~~~~~~

Changing to match b.deref_mut().node fixes the error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions