Skip to content

borrow checker error message has wrong pointer type with trait Test<'a> #14098

Closed
@erickt

Description

@erickt

creduce found this error:

trait Test<'a> {
    fn method(&'a mut self);
}

fn run_test<'a>(test: &'a mut Test<'a>) {
    test.method();
    test.method();
}

fn main() {}

With this error:

ice.rs:7:5: 7:9 error: cannot borrow `*test` as mutable more than once at a time
ice.rs:7     test.method();
             ^~~~
ice.rs:6:5: 6:9 note: previous borrow of `*test` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `*test` until the borrow ends
ice.rs:6     test.method();
             ^~~~
ice.rs:8:2: 8:2 note: previous borrow ends here
ice.rs:5 fn run_test<'a>(test: &'a mut Test<'a>) {
ice.rs:6     test.method();
ice.rs:7     test.method();
ice.rs:8 }
         ^
error: aborting due to previous error

One thing that makes this case potentially interesting though is that if you remove the lifetime from the trait the error goes away:

trait Test {
    fn method<'a>(&'a mut self);
}

fn run_test<'a>(test: &'a mut Test) {
    test.method();
    test.method();
}

fn main() {}

@nikomatsakis: Is this second case an error? Or is it supposed to happen?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions