rust/tests/ui/associated-types/associated-types-outlives.rs
Esteban Küber a1a3abb08f When possible, suggest cloning the result of a call instead of an argument
```
error[E0505]: cannot move out of `a` because it is borrowed
  --> $DIR/variance-issue-20533.rs:28:14
   |
LL |         let a = AffineU32(1);
   |             - binding `a` declared here
LL |         let x = foo(&a);
   |                     -- borrow of `a` occurs here
LL |         drop(a);
   |              ^ move out of `a` occurs here
LL |         drop(x);
   |              - borrow later used here
   |
help: consider cloning the value if the performance cost is acceptable
   |
LL |         let x = foo(&a).clone();
   |                        ++++++++
```
2024-04-11 16:41:41 +00:00

29 lines
717 B
Rust

// Regression test for issue #24622. The older associated types code
// was erroneously assuming that all projections outlived the current
// fn body, causing this (invalid) code to be accepted.
pub trait Foo<'a> {
type Bar: Clone;
}
impl<'a, T: 'a> Foo<'a> for T {
type Bar = &'a T;
}
fn denormalise<'a, T>(t: &'a T) -> <T as Foo<'a>>::Bar {
t
}
pub fn free_and_use<T: for<'a> Foo<'a>,
F: for<'a> FnOnce(<T as Foo<'a>>::Bar)>(x: T, f: F) {
let y;
'body: loop { // lifetime annotations added for clarity
's: loop { y = denormalise(&x); break }
drop(x); //~ ERROR cannot move out of `x` because it is borrowed
return f(y);
}
}
pub fn main() {
}