Skip to content

Accessing &mut through & oddities #11913

Closed
@alexcrichton

Description

@alexcrichton

This program fails to compile:

struct A<'a> { b: &'a mut int }

fn foo(a: &A) {
    *a.b = 3;
}

fn main() {
    let mut a = 2;
    let b = A { b: &mut a };
    foo(&b);
}

whereas this program succeeds to compile

struct A<'a> { b: &'a mut int }

fn bar(a: &mut int) { *a = 2; }

fn foo(a: &A) {
    bar(a.b);
    bar(a.b);
}

fn main() {
    let mut a = 2;
    let b = A { b: &mut a };
    foo(&b);
}

These seem to be doing the same thing to me, so I would expect both of them to be rejected or both of them be accepted.

cc @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions