Skip to content

Commit a7c1754

Browse files
authored
Merge pull request #22087 from A4-Tacks/self-ty-hints
fix: Fix incorrect lifetime hints for self with type
2 parents f4d493c + f0ab829 commit a7c1754

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

crates/ide/src/inlay_hints/lifetime.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ pub(super) fn fn_hints(
3131
let param_list = func.param_list()?;
3232
let generic_param_list = func.generic_param_list();
3333
let ret_type = func.ret_type();
34-
let self_param = param_list.self_param().filter(|it| it.amp_token().is_some());
3534
let gpl_append_range = func.name()?.syntax().text_range();
3635
hints_(
3736
acc,
@@ -49,7 +48,7 @@ pub(super) fn fn_hints(
4948
}),
5049
generic_param_list,
5150
ret_type,
52-
self_param,
51+
param_list.self_param(),
5352
|acc, allocated_lifetimes| {
5453
acc.push(InlayHint {
5554
range: gpl_append_range,
@@ -208,6 +207,20 @@ fn hints_(
208207
Some(lt) => matches!(lt.text().as_str(), "'_"),
209208
None => true,
210209
};
210+
let self_param = self_param.and_then(|it| {
211+
if it.colon_token().is_none() {
212+
return Some((it.amp_token(), it.lifetime()));
213+
}
214+
it.ty().map(|ty| {
215+
let ref_type = ty.syntax().descendants().find_map(ast::RefType::cast);
216+
let lifetime = ref_type
217+
.as_ref()
218+
.and_then(|it| it.lifetime())
219+
.or_else(|| ty.syntax().descendants().find_map(ast::Lifetime::cast));
220+
(ref_type.and_then(|it| it.amp_token()), lifetime)
221+
})
222+
});
223+
let self_param = self_param.filter(|(amp, lt)| amp.is_some() || lt.is_some());
211224

212225
let mk_lt_hint = |t: SyntaxToken, label: String| InlayHint {
213226
range: t.text_range(),
@@ -222,10 +235,9 @@ fn hints_(
222235

223236
let potential_lt_refs = {
224237
let mut acc: Vec<_> = vec![];
225-
if let Some(self_param) = &self_param {
226-
let lifetime = self_param.lifetime();
238+
if let Some((amp_token, lifetime)) = self_param.clone() {
227239
let is_elided = is_elided(&lifetime);
228-
acc.push((None, self_param.amp_token(), lifetime, is_elided));
240+
acc.push((None, amp_token, lifetime, is_elided));
229241
}
230242
params.for_each(|(name, ty)| {
231243
// FIXME: check path types
@@ -433,6 +445,9 @@ fn nested_out(a: &()) -> & &X< &()>{}
433445
//^'0 ^'0 ^'0 ^'0
434446
435447
impl () {
448+
fn foo(self, x: &()) -> &() {}
449+
// ^^^<'0>
450+
// ^'0 ^'0
436451
fn foo(&self) {}
437452
// ^^^<'0>
438453
// ^'0
@@ -442,6 +457,10 @@ impl () {
442457
fn foo(&self, a: &()) -> &() {}
443458
// ^^^<'0, '1>
444459
// ^'0 ^'1 ^'0
460+
fn foo(self: &Self, a: &()) -> &() {}
461+
// ^^^<'0, '1>
462+
// ^'0 ^'1 ^'0
463+
445464
}
446465
"#,
447466
);

0 commit comments

Comments
 (0)