• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // edition:2021
2 // [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
3 // revisions: current next
4 
5 #![feature(return_position_impl_trait_in_trait)]
6 #![allow(incomplete_features)]
7 
8 use std::future::Future;
9 
10 trait Captures<'a> {}
11 impl<T> Captures<'_> for T {}
12 
13 trait Captures2<'a, 'b> {}
14 impl<T> Captures2<'_, '_> for T {}
15 
16 pub trait AsyncTrait {
async_fn(&self, buff: &[u8]) -> impl Future<Output = Vec<u8>>17     fn async_fn(&self, buff: &[u8]) -> impl Future<Output = Vec<u8>>;
async_fn_early<'a: 'a>(&self, buff: &'a [u8]) -> impl Future<Output = Vec<u8>>18     fn async_fn_early<'a: 'a>(&self, buff: &'a [u8]) -> impl Future<Output = Vec<u8>>;
async_fn_multiple<'a>(&'a self, buff: &[u8]) -> impl Future<Output = Vec<u8>> + Captures<'a>19     fn async_fn_multiple<'a>(&'a self, buff: &[u8])
20         -> impl Future<Output = Vec<u8>> + Captures<'a>;
async_fn_reduce_outlive<'a, T>( &'a self, buff: &[u8], t: T, ) -> impl Future<Output = Vec<u8>> + 'a21     fn async_fn_reduce_outlive<'a, T>(
22         &'a self,
23         buff: &[u8],
24         t: T,
25     ) -> impl Future<Output = Vec<u8>> + 'a;
async_fn_reduce<'a, T>( &'a self, buff: &[u8], t: T, ) -> impl Future<Output = Vec<u8>> + Captures<'a>26     fn async_fn_reduce<'a, T>(
27         &'a self,
28         buff: &[u8],
29         t: T,
30     ) -> impl Future<Output = Vec<u8>> + Captures<'a>;
31 }
32 
33 pub struct Struct;
34 
35 impl AsyncTrait for Struct {
async_fn<'a>(&self, buff: &'a [u8]) -> impl Future<Output = Vec<u8>> + 'a36     fn async_fn<'a>(&self, buff: &'a [u8]) -> impl Future<Output = Vec<u8>> + 'a {
37         //~^ ERROR return type captures more lifetimes than trait definition
38         async move { buff.to_vec() }
39     }
40 
async_fn_early<'a: 'a>(&self, buff: &'a [u8]) -> impl Future<Output = Vec<u8>> + 'a41     fn async_fn_early<'a: 'a>(&self, buff: &'a [u8]) -> impl Future<Output = Vec<u8>> + 'a {
42         //~^ ERROR return type captures more lifetimes than trait definition
43         async move { buff.to_vec() }
44     }
45 
async_fn_multiple<'a, 'b>( &'a self, buff: &'b [u8], ) -> impl Future<Output = Vec<u8>> + Captures2<'a, 'b>46     fn async_fn_multiple<'a, 'b>(
47         &'a self,
48         buff: &'b [u8],
49     ) -> impl Future<Output = Vec<u8>> + Captures2<'a, 'b> {
50         //~^ ERROR return type captures more lifetimes than trait definition
51         async move { buff.to_vec() }
52     }
53 
async_fn_reduce_outlive<'a, 'b, T>( &'a self, buff: &'b [u8], t: T, ) -> impl Future<Output = Vec<u8>>54     fn async_fn_reduce_outlive<'a, 'b, T>(
55         &'a self,
56         buff: &'b [u8],
57         t: T,
58     ) -> impl Future<Output = Vec<u8>> {
59         //~^ ERROR the parameter type `T` may not live long enough
60         async move {
61             let _t = t;
62             vec![]
63         }
64     }
65 
66     // OK: We remove the `Captures<'a>`, providing a guarantee that we don't capture `'a`,
67     // but we still fulfill the `Captures<'a>` trait bound.
async_fn_reduce<'a, 'b, T>(&'a self, buff: &'b [u8], t: T) -> impl Future<Output = Vec<u8>>68     fn async_fn_reduce<'a, 'b, T>(&'a self, buff: &'b [u8], t: T) -> impl Future<Output = Vec<u8>> {
69         async move {
70             let _t = t;
71             vec![]
72         }
73     }
74 }
75 
main()76 fn main() {}
77