Borrow checker failing when providing trait object as type parameter


#1

Hello,

I have some weird behaviour of the borrow checker when using generic structures. Am I doing something wrong?

struct MyGenericStruct<T>;

impl <T> MyGenericStruct<T> {
    fn bar(&self, _: T) {}
}

struct MyNonGenericStruct;

impl MyNonGenericStruct {
    fn bar(&self, _: &mut MyTrait) {}
}   

trait MyTrait{}

struct FooBar;
impl MyTrait for FooBar{}

fn test() { 
    let mut foobar = FooBar;

    {    // No problem when using non generic struct
        let foo = MyNonGenericStruct;
        foo.bar(&mut foobar);
        foo.bar(&mut foobar);
    }
    
    {   
        let foo = MyGenericStruct::<&mut MyTrait>;
        {   
            foo.bar(&mut foobar);   // First borrow passes ...
        }   
        {   
            foo.bar(&mut foobar);   // This fails as "foobar" is already borrowed
        }
    }   // The previous borrow ends here (lifetime of "foo")
}

Thanks!