]> git.proxmox.com Git - rustc.git/blame - src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.rs
New upstream version 1.66.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
04454e1e 17 G: Get<T>,
f9f354fc 18{
cdc7bbd5 19 move || {
064997fb 20 //~^ ERROR hidden type for `impl FnOnce()` captures lifetime
f9f354fc
XL
21 *dest = g.get();
22 }
23}
24
25// After applying suggestion for `foo`:
26fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
f9f354fc 27where
04454e1e 28 G: Get<T>,
f9f354fc
XL
29{
30 move || {
923072b8 31 //~^ ERROR the parameter type `G` may not live long enough
f9f354fc
XL
32 *dest = g.get();
33 }
34}
35
f9f354fc 36// After applying suggestion for `bar`:
04454e1e
FG
37fn baz<G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
38//~^ ERROR undeclared lifetime name `'a`
f9f354fc 39where
04454e1e 40 G: Get<T>,
f9f354fc
XL
41{
42 move || {
43 *dest = g.get();
44 }
45}
46
47// After applying suggestion for `baz`:
48fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
f9f354fc 49where
04454e1e 50 G: Get<T>,
f9f354fc
XL
51{
52 move || {
923072b8 53 //~^ ERROR the parameter type `G` may not live long enough
f9f354fc
XL
54 *dest = g.get();
55 }
56}
57
58// Same as above, but show that we pay attention to lifetime names from parent item
59impl<'a> Foo {
60 fn qux<'b, G: Get<T> + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
f9f354fc 61 move || {
923072b8 62 //~^ ERROR the parameter type `G` may not live long enough
f9f354fc
XL
63 *dest = g.get();
64 }
65 }
66}
67
68// After applying suggestion for `qux`:
69fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
f9f354fc 70where
04454e1e 71 G: Get<T>,
f9f354fc
XL
72{
73 move || {
923072b8
FG
74 //~^ ERROR the parameter type `G` may not live long enough
75 //~| ERROR explicit lifetime required
f9f354fc
XL
76 *dest = g.get();
77 }
78}
79
80// Potential incorrect attempt:
81fn bak<'a, G, T>(g: G, dest: &'a mut T) -> impl FnOnce() + 'a
f9f354fc 82where
04454e1e 83 G: Get<T>,
f9f354fc
XL
84{
85 move || {
923072b8 86 //~^ ERROR the parameter type `G` may not live long enough
f9f354fc
XL
87 *dest = g.get();
88 }
89}
90
f9f354fc
XL
91// We need to tie the lifetime of `G` with the lifetime of `&mut T` and the returned closure:
92fn ok<'a, G: 'a, T>(g: G, dest: &'a mut T) -> impl FnOnce() + 'a
93where
04454e1e 94 G: Get<T>,
f9f354fc
XL
95{
96 move || {
97 *dest = g.get();
98 }
99}
100
101// This also works. The `'_` isn't necessary but it's where we arrive to following the suggestions:
102fn ok2<'a, G: 'a, T>(g: G, dest: &'a mut T) -> impl FnOnce() + '_ + 'a
103where
04454e1e 104 G: Get<T>,
f9f354fc
XL
105{
106 move || {
107 *dest = g.get();
108 }
109}
110
111fn main() {}