Skip to content

"immutable field" errors are confusing when the handle is mutable, should describe why it is immutable #18150

Closed
@huonw

Description

@huonw
struct Foo { a: usize }
fn main() {
    let mut x = std::rc::Rc::new(Foo { a: 1 });
    x.a += 1;

    let mut y = &(Foo { a: 1 });
    y.a += 1;
}
<anon>:4:5: 4:13 error: cannot assign to immutable field
<anon>:4     x.a += 1;
             ^~~~~~~~
<anon>:7:5: 7:13 error: cannot assign to immutable field `y.a`
<anon>:7     y.a += 1;
             ^~~~~~~~

Gives no indication that the field is immutable because it is stored inside an Rc/&. These example are somewhat obvious, since the initialiser is directly above, but it's certainly possible to be matching deep inside some data structure and meet this error even though there are muts on all the appropriate variables.

The error message could include: "note: this field is immutable because it is being accessed via an & reference [created by the autoderef of a Rc]".

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.E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.P-highHigh priorityT-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