Skip to content

borrow checking not transparent to inlining #16594

Closed
@bfops

Description

@bfops

The borrow checker gives different results when functions are manually inlined, giving confusing or annoying results.

The following compiles:

struct Foo {
    x: int,
    y: int,
}

impl Foo {
    pub fn foo(&mut self) {
        let y = &mut self.y;
        self.x = self.x + *y;
    }
}

The following does not:

struct Foo {
    x: int,
    y: int,
}

impl Foo {
    pub fn foo(&mut self) {
        let y = self.bar();
        self.x = self.x + *y;
    }

    pub fn bar(&mut self) -> &mut int {
        &mut self.y
    }
}

I don't believe this to be a duplicate of #6393, because this has not only to do with scoping, but also the fact that a member function always borrows all of self.

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