]> git.lizzy.rs Git - rust.git/blob - crates/ide/src/inlay_hints/fn_lifetime_fn.rs
Auto merge of #13806 - WaffleLapkin:typed_blÄhaj, r=Veykril
[rust.git] / crates / ide / src / inlay_hints / fn_lifetime_fn.rs
1 //! Implementation of "lifetime elision" inlay hints:
2 //! ```no_run
3 //! fn example/* <'0> */(a: &/* '0 */()) {}
4 //! ```
5 use ide_db::{syntax_helpers::node_ext::walk_ty, FxHashMap};
6 use itertools::Itertools;
7 use syntax::SmolStr;
8 use syntax::{
9     ast::{self, AstNode, HasGenericParams, HasName},
10     SyntaxToken,
11 };
12
13 use crate::{InlayHint, InlayHintsConfig, InlayKind, InlayTooltip, LifetimeElisionHints};
14
15 pub(super) fn hints(
16     acc: &mut Vec<InlayHint>,
17     config: &InlayHintsConfig,
18     func: ast::Fn,
19 ) -> Option<()> {
20     if config.lifetime_elision_hints == LifetimeElisionHints::Never {
21         return None;
22     }
23
24     let mk_lt_hint = |t: SyntaxToken, label: String| InlayHint {
25         range: t.text_range(),
26         kind: InlayKind::LifetimeHint,
27         label: label.into(),
28         tooltip: Some(InlayTooltip::String("Elided lifetime".into())),
29     };
30
31     let param_list = func.param_list()?;
32     let generic_param_list = func.generic_param_list();
33     let ret_type = func.ret_type();
34     let self_param = param_list.self_param().filter(|it| it.amp_token().is_some());
35
36     let is_elided = |lt: &Option<ast::Lifetime>| match lt {
37         Some(lt) => matches!(lt.text().as_str(), "'_"),
38         None => true,
39     };
40
41     let potential_lt_refs = {
42         let mut acc: Vec<_> = vec![];
43         if let Some(self_param) = &self_param {
44             let lifetime = self_param.lifetime();
45             let is_elided = is_elided(&lifetime);
46             acc.push((None, self_param.amp_token(), lifetime, is_elided));
47         }
48         param_list.params().filter_map(|it| Some((it.pat(), it.ty()?))).for_each(|(pat, ty)| {
49             // FIXME: check path types
50             walk_ty(&ty, &mut |ty| match ty {
51                 ast::Type::RefType(r) => {
52                     let lifetime = r.lifetime();
53                     let is_elided = is_elided(&lifetime);
54                     acc.push((
55                         pat.as_ref().and_then(|it| match it {
56                             ast::Pat::IdentPat(p) => p.name(),
57                             _ => None,
58                         }),
59                         r.amp_token(),
60                         lifetime,
61                         is_elided,
62                     ))
63                 }
64                 _ => (),
65             })
66         });
67         acc
68     };
69
70     // allocate names
71     let mut gen_idx_name = {
72         let mut gen = (0u8..).map(|idx| match idx {
73             idx if idx < 10 => SmolStr::from_iter(['\'', (idx + 48) as char]),
74             idx => format!("'{idx}").into(),
75         });
76         move || gen.next().unwrap_or_default()
77     };
78     let mut allocated_lifetimes = vec![];
79
80     let mut used_names: FxHashMap<SmolStr, usize> =
81         match config.param_names_for_lifetime_elision_hints {
82             true => generic_param_list
83                 .iter()
84                 .flat_map(|gpl| gpl.lifetime_params())
85                 .filter_map(|param| param.lifetime())
86                 .filter_map(|lt| Some((SmolStr::from(lt.text().as_str().get(1..)?), 0)))
87                 .collect(),
88             false => Default::default(),
89         };
90     {
91         let mut potential_lt_refs = potential_lt_refs.iter().filter(|&&(.., is_elided)| is_elided);
92         if let Some(_) = &self_param {
93             if let Some(_) = potential_lt_refs.next() {
94                 allocated_lifetimes.push(if config.param_names_for_lifetime_elision_hints {
95                     // self can't be used as a lifetime, so no need to check for collisions
96                     "'self".into()
97                 } else {
98                     gen_idx_name()
99                 });
100             }
101         }
102         potential_lt_refs.for_each(|(name, ..)| {
103             let name = match name {
104                 Some(it) if config.param_names_for_lifetime_elision_hints => {
105                     if let Some(c) = used_names.get_mut(it.text().as_str()) {
106                         *c += 1;
107                         SmolStr::from(format!("'{text}{c}", text = it.text().as_str()))
108                     } else {
109                         used_names.insert(it.text().as_str().into(), 0);
110                         SmolStr::from_iter(["\'", it.text().as_str()])
111                     }
112                 }
113                 _ => gen_idx_name(),
114             };
115             allocated_lifetimes.push(name);
116         });
117     }
118
119     // fetch output lifetime if elision rule applies
120     let output = match potential_lt_refs.as_slice() {
121         [(_, _, lifetime, _), ..] if self_param.is_some() || potential_lt_refs.len() == 1 => {
122             match lifetime {
123                 Some(lt) => match lt.text().as_str() {
124                     "'_" => allocated_lifetimes.get(0).cloned(),
125                     "'static" => None,
126                     name => Some(name.into()),
127                 },
128                 None => allocated_lifetimes.get(0).cloned(),
129             }
130         }
131         [..] => None,
132     };
133
134     if allocated_lifetimes.is_empty() && output.is_none() {
135         return None;
136     }
137
138     // apply hints
139     // apply output if required
140     let mut is_trivial = true;
141     if let (Some(output_lt), Some(r)) = (&output, ret_type) {
142         if let Some(ty) = r.ty() {
143             walk_ty(&ty, &mut |ty| match ty {
144                 ast::Type::RefType(ty) if ty.lifetime().is_none() => {
145                     if let Some(amp) = ty.amp_token() {
146                         is_trivial = false;
147                         acc.push(mk_lt_hint(amp, output_lt.to_string()));
148                     }
149                 }
150                 _ => (),
151             })
152         }
153     }
154
155     if config.lifetime_elision_hints == LifetimeElisionHints::SkipTrivial && is_trivial {
156         return None;
157     }
158
159     let mut a = allocated_lifetimes.iter();
160     for (_, amp_token, _, is_elided) in potential_lt_refs {
161         if is_elided {
162             let t = amp_token?;
163             let lt = a.next()?;
164             acc.push(mk_lt_hint(t, lt.to_string()));
165         }
166     }
167
168     // generate generic param list things
169     match (generic_param_list, allocated_lifetimes.as_slice()) {
170         (_, []) => (),
171         (Some(gpl), allocated_lifetimes) => {
172             let angle_tok = gpl.l_angle_token()?;
173             let is_empty = gpl.generic_params().next().is_none();
174             acc.push(InlayHint {
175                 range: angle_tok.text_range(),
176                 kind: InlayKind::LifetimeHint,
177                 label: format!(
178                     "{}{}",
179                     allocated_lifetimes.iter().format(", "),
180                     if is_empty { "" } else { ", " }
181                 )
182                 .into(),
183                 tooltip: Some(InlayTooltip::String("Elided lifetimes".into())),
184             });
185         }
186         (None, allocated_lifetimes) => acc.push(InlayHint {
187             range: func.name()?.syntax().text_range(),
188             kind: InlayKind::GenericParamListHint,
189             label: format!("<{}>", allocated_lifetimes.iter().format(", "),).into(),
190             tooltip: Some(InlayTooltip::String("Elided lifetimes".into())),
191         }),
192     }
193     Some(())
194 }
195
196 #[cfg(test)]
197 mod tests {
198     use crate::{
199         inlay_hints::tests::{check, check_with_config, TEST_CONFIG},
200         InlayHintsConfig, LifetimeElisionHints,
201     };
202
203     #[test]
204     fn hints_lifetimes() {
205         check(
206             r#"
207 fn empty() {}
208
209 fn no_gpl(a: &()) {}
210  //^^^^^^<'0>
211           // ^'0
212 fn empty_gpl<>(a: &()) {}
213       //    ^'0   ^'0
214 fn partial<'b>(a: &(), b: &'b ()) {}
215 //        ^'0, $  ^'0
216 fn partial<'a>(a: &'a (), b: &()) {}
217 //        ^'0, $             ^'0
218
219 fn single_ret(a: &()) -> &() {}
220 // ^^^^^^^^^^<'0>
221               // ^'0     ^'0
222 fn full_mul(a: &(), b: &()) {}
223 // ^^^^^^^^<'0, '1>
224             // ^'0     ^'1
225
226 fn foo<'c>(a: &'c ()) -> &() {}
227                       // ^'c
228
229 fn nested_in(a: &   &X< &()>) {}
230 // ^^^^^^^^^<'0, '1, '2>
231               //^'0 ^'1 ^'2
232 fn nested_out(a: &()) -> &   &X< &()>{}
233 // ^^^^^^^^^^<'0>
234                //^'0     ^'0 ^'0 ^'0
235
236 impl () {
237     fn foo(&self) {}
238     // ^^^<'0>
239         // ^'0
240     fn foo(&self) -> &() {}
241     // ^^^<'0>
242         // ^'0       ^'0
243     fn foo(&self, a: &()) -> &() {}
244     // ^^^<'0, '1>
245         // ^'0       ^'1     ^'0
246 }
247 "#,
248         );
249     }
250
251     #[test]
252     fn hints_lifetimes_named() {
253         check_with_config(
254             InlayHintsConfig { param_names_for_lifetime_elision_hints: true, ..TEST_CONFIG },
255             r#"
256 fn nested_in<'named>(named: &        &X<      &()>) {}
257 //          ^'named1, 'named2, 'named3, $
258                           //^'named1 ^'named2 ^'named3
259 "#,
260         );
261     }
262
263     #[test]
264     fn hints_lifetimes_trivial_skip() {
265         check_with_config(
266             InlayHintsConfig {
267                 lifetime_elision_hints: LifetimeElisionHints::SkipTrivial,
268                 ..TEST_CONFIG
269             },
270             r#"
271 fn no_gpl(a: &()) {}
272 fn empty_gpl<>(a: &()) {}
273 fn partial<'b>(a: &(), b: &'b ()) {}
274 fn partial<'a>(a: &'a (), b: &()) {}
275
276 fn single_ret(a: &()) -> &() {}
277 // ^^^^^^^^^^<'0>
278               // ^'0     ^'0
279 fn full_mul(a: &(), b: &()) {}
280
281 fn foo<'c>(a: &'c ()) -> &() {}
282                       // ^'c
283
284 fn nested_in(a: &   &X< &()>) {}
285 fn nested_out(a: &()) -> &   &X< &()>{}
286 // ^^^^^^^^^^<'0>
287                //^'0     ^'0 ^'0 ^'0
288
289 impl () {
290     fn foo(&self) {}
291     fn foo(&self) -> &() {}
292     // ^^^<'0>
293         // ^'0       ^'0
294     fn foo(&self, a: &()) -> &() {}
295     // ^^^<'0, '1>
296         // ^'0       ^'1     ^'0
297 }
298 "#,
299         );
300     }
301 }