diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/regions/regions-close-associated-type-into-object.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/regions/regions-close-associated-type-into-object.rs')
-rw-r--r-- | src/test/ui/regions/regions-close-associated-type-into-object.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/test/ui/regions/regions-close-associated-type-into-object.rs b/src/test/ui/regions/regions-close-associated-type-into-object.rs new file mode 100644 index 000000000..428477e24 --- /dev/null +++ b/src/test/ui/regions/regions-close-associated-type-into-object.rs @@ -0,0 +1,71 @@ +trait X {} + + + +trait Iter { + type Item: X; + + fn into_item(self) -> Self::Item; + fn as_item(&self) -> &Self::Item; +} + +fn bad1<T: Iter>(v: T) -> Box<dyn X + 'static> +{ + let item = v.into_item(); + Box::new(item) //~ ERROR associated type `<T as Iter>::Item` may not live long enough +} + +fn bad2<T: Iter>(v: T) -> Box<dyn X + 'static> + where Box<T::Item> : X +{ + let item: Box<_> = Box::new(v.into_item()); + Box::new(item) //~ ERROR associated type `<T as Iter>::Item` may not live long enough +} + +fn bad3<'a, T: Iter>(v: T) -> Box<dyn X + 'a> +{ + let item = v.into_item(); + Box::new(item) //~ ERROR associated type `<T as Iter>::Item` may not live long enough +} + +fn bad4<'a, T: Iter>(v: T) -> Box<dyn X + 'a> + where Box<T::Item> : X +{ + let item: Box<_> = Box::new(v.into_item()); + Box::new(item) //~ ERROR associated type `<T as Iter>::Item` may not live long enough +} + +fn ok1<'a, T: Iter>(v: T) -> Box<dyn X + 'a> + where T::Item : 'a +{ + let item = v.into_item(); + Box::new(item) // OK, T::Item : 'a is declared +} + +fn ok2<'a, T: Iter>(v: &T, w: &'a T::Item) -> Box<dyn X + 'a> + where T::Item : Clone +{ + let item = Clone::clone(w); + Box::new(item) // OK, T::Item : 'a is implied +} + +fn ok3<'a, T: Iter>(v: &'a T) -> Box<dyn X + 'a> + where T::Item : Clone + 'a +{ + let item = Clone::clone(v.as_item()); + Box::new(item) // OK, T::Item : 'a was declared +} + +fn meh1<'a, T: Iter>(v: &'a T) -> Box<dyn X + 'a> + where T::Item : Clone +{ + // This case is kind of interesting. It's the same as `ok3` but + // without the explicit declaration. This is valid because `T: 'a + // => T::Item: 'a`, and the former we can deduce from our argument + // of type `&'a T`. + + let item = Clone::clone(v.as_item()); + Box::new(item) +} + +fn main() {} |