Skip to content

Misleading error message when mutably referencing members of cloned &T when T does not implement Clone #34629

Closed
@peterjoel

Description

@peterjoel

Code example:

#[derive(Debug)]
struct Str {
   x: Option<i32>,
}

fn main() {
    let s = Str { x: None };
    let sr = &s;
    let mut sm = sr.clone();
    foo(&mut sm.x);
}

fn foo(s: &mut Option<i32>) {
    if s.is_none() {
        *s = Some(0);
    }
    println!("{:?}", s);
}

The Error is:

error: cannot borrow immutable field `sm.x` as mutable
  --> <anon>:10:14
   |>
10 |>     foo(&mut sm.x);
   |>              ^^^^

This is misleading because the fix is to add a Clone impl for Str

Meta

rustc --version --verbose:
rustc 1.9.0-nightly (d5a91e6 2016-03-26)
binary: rustc
commit-hash: d5a91e6
commit-date: 2016-03-26
host: x86_64-apple-darwin
release: 1.9.0-nightly

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions