Skip to content

Dereference of pointer does not end its life #6159

Closed
@kalenedrael

Description

@kalenedrael

Compiling this:

struct Foo {
        x: int,
}

impl Foo {
        pub fn get_x(&self) -> &'self int { &self.x }
}

struct Bar {
        x: Foo,
}

impl Bar {
        pub fn do_shit(&mut self) -> () { }
        pub fn break_shit(&mut self) -> () {
                let x = *self.x.get_x();
                core::util::ignore(x);
                self.do_shit()
        }
}

fn main() {
        let mut x = Bar { x: Foo { x: 1 } };
        x.break_shit();
}

yields the error

shit.rs:20:2: 20:6 error: loan of dereference of mutable & pointer as mutable conflicts with prior loan
shit.rs:20              self.do_shit()
                        ^~~~
shit.rs:16:10: 16:14 note: prior loan as immutable granted here
shit.rs:16              let y = self.x.get_x();

The self pointer is considered loaned out from 'let x = *self.x.get_x()', even though it... isn't.

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