]> git.proxmox.com Git - rustc.git/blame - src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / src / test / ui / suggestions / lifetimes / missing-lifetimes-in-signature.rs
CommitLineData
f9f354fc
XL
1pub trait Get<T> {
2 fn get(self) -> T;
3}
4
5struct Foo {
6 x: usize,
7}
8
9impl Get<usize> for Foo {
10 fn get(self) -> usize {
11 self.x
12 }
13}
14
15fn foo<G, T>(g: G, dest: &mut T) -> impl FnOnce()
16where
17 G: Get<T>
18{
cdc7bbd5 19 move || {
f9f354fc
XL
20 *dest = g.get();
21 }
22}
23
24// After applying suggestion for `foo`:
25fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
f9f354fc
XL
26where
27 G: Get<T>
28{
29 move || {
30 *dest = g.get();
31 }
32}
33
34
35// After applying suggestion for `bar`:
36fn baz<G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ //~ ERROR undeclared lifetime
37where
38 G: Get<T>
39{
40 move || {
41 *dest = g.get();
42 }
43}
44
45// After applying suggestion for `baz`:
46fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
f9f354fc
XL
47where
48 G: Get<T>
49{
50 move || {
51 *dest = g.get();
52 }
53}
54
55// Same as above, but show that we pay attention to lifetime names from parent item
56impl<'a> Foo {
57 fn qux<'b, G: Get<T> + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
f9f354fc
XL
58 move || {
59 *dest = g.get();
60 }
61 }
62}
63
64// After applying suggestion for `qux`:
65fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
f9f354fc
XL
66where
67 G: Get<T>
68{
69 move || {
70 *dest = g.get();
71 }
72}
73
74// Potential incorrect attempt:
75fn bak<'a, G, T>(g: G, dest: &'a mut T) -> impl FnOnce() + 'a
f9f354fc
XL
76where
77 G: Get<T>
78{
79 move || {
80 *dest = g.get();
81 }
82}
83
84
85// We need to tie the lifetime of `G` with the lifetime of `&mut T` and the returned closure:
86fn ok<'a, G: 'a, T>(g: G, dest: &'a mut T) -> impl FnOnce() + 'a
87where
88 G: Get<T>
89{
90 move || {
91 *dest = g.get();
92 }
93}
94
95// This also works. The `'_` isn't necessary but it's where we arrive to following the suggestions:
96fn ok2<'a, G: 'a, T>(g: G, dest: &'a mut T) -> impl FnOnce() + '_ + 'a
97where
98 G: Get<T>
99{
100 move || {
101 *dest = g.get();
102 }
103}
104
105fn main() {}