Skip to content

Commit b634d99

Browse files
committed
Give the lifetimes better names
1 parent 673e2b1 commit b634d99

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

crates/ide/src/inlay_hints.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct InlayHintsConfig {
1717
pub parameter_hints: bool,
1818
pub chaining_hints: bool,
1919
pub closure_return_type_hints: bool,
20+
// FIXME: ternary option here, on off non-noisy
2021
pub lifetime_elision_hints: bool,
2122
pub hide_named_constructor_hints: bool,
2223
pub max_length: Option<usize>,
@@ -135,15 +136,12 @@ fn lifetime_hints(
135136
let ret_type = func.ret_type();
136137
let self_param = param_list.self_param();
137138

139+
// FIXME: don't use already used lifetimenames
140+
138141
let mut allocated_lifetimes = vec![];
139142
let mut gen_name = {
140-
let mut iter = 'a'..;
141-
let allocated_lifetimes = &mut allocated_lifetimes;
142-
move || {
143-
if let Some(it) = iter.next() {
144-
allocated_lifetimes.push(SmolStr::from_iter(['\'', it]))
145-
}
146-
}
143+
let mut gen = ('a'..).map(|it| SmolStr::from_iter(['\'', it]));
144+
move || gen.next().unwrap_or_else(SmolStr::default)
147145
};
148146

149147
let potential_lt_refs: Vec<_> = param_list
@@ -152,7 +150,13 @@ fn lifetime_hints(
152150
let ty = it.ty()?;
153151
// FIXME: look into the nested types here and check path types
154152
match ty {
155-
ast::Type::RefType(r) => Some(r),
153+
ast::Type::RefType(r) => Some((
154+
it.pat().and_then(|it| match it {
155+
ast::Pat::IdentPat(p) => p.name(),
156+
_ => None,
157+
}),
158+
r,
159+
)),
156160
_ => None,
157161
}
158162
})
@@ -180,13 +184,16 @@ fn lifetime_hints(
180184
// allocate names
181185
if let Some(self_param) = &self_param {
182186
if is_elided(self_param.lifetime()) {
183-
gen_name();
187+
allocated_lifetimes.push(SmolStr::new_inline("'self"));
184188
}
185189
}
186-
potential_lt_refs.iter().for_each(|it| {
190+
potential_lt_refs.iter().for_each(|(name, it)| {
187191
// FIXME: look into the nested types here and check path types
188192
if is_elided(it.lifetime()) {
189-
gen_name();
193+
allocated_lifetimes.push(
194+
name.as_ref()
195+
.map_or_else(|| gen_name(), |it| SmolStr::from_iter(["'", it.text().as_str()])),
196+
);
190197
}
191198
});
192199

@@ -200,7 +207,7 @@ fn lifetime_hints(
200207
}
201208
} else {
202209
match potential_lt_refs.as_slice() {
203-
[r] => match fetch_lt_text(r.lifetime()) {
210+
[(_, r)] => match fetch_lt_text(r.lifetime()) {
204211
LifetimeKind::Elided => allocated_lifetimes.get(0).cloned(),
205212
LifetimeKind::Named(name) => Some(name),
206213
LifetimeKind::Static => None,
@@ -246,7 +253,7 @@ fn lifetime_hints(
246253
0
247254
};
248255

249-
for p in potential_lt_refs.iter() {
256+
for (_, p) in potential_lt_refs.iter() {
250257
if is_elided(p.lifetime()) {
251258
let t = p.amp_token()?;
252259
let lt = allocated_lifetimes[idx].clone();
@@ -2015,8 +2022,8 @@ fn empty_gpl<>(a: &()) {}
20152022
// ^'a ^'a
20162023
fn partial<'b>(a: &(), b: &'b ()) {}
20172024
// ^'a, $ ^'a
2018-
fn partial<'b>(a: &'b (), b: &()) {}
2019-
// ^'a, $ ^'a
2025+
fn partial<'a>(a: &'a (), b: &()) {}
2026+
// ^'b, $ ^'b
20202027
20212028
fn single_ret(a: &()) -> &() {}
20222029
// ^^^^^^^^^^<'a>
@@ -2030,11 +2037,11 @@ fn foo<'c>(a: &'c ()) -> &() {}
20302037
20312038
impl () {
20322039
fn foo(&self) -> &() {}
2033-
// ^^^<'a>
2034-
// ^'a ^'a
2040+
// ^^^<'self>
2041+
// ^'self ^'self
20352042
fn foo(&self, a: &()) -> &() {}
2036-
// ^^^<'a, 'b>
2037-
// ^'a ^'b ^'a$
2043+
// ^^^<'self, 'a>
2044+
// ^'self ^'a ^'self$
20382045
}
20392046
"#,
20402047
);

0 commit comments

Comments
 (0)