Skip to content

Commit edb2e57

Browse files
committed
Auto merge of rust-lang#12416 - Veykril:inlay-hints, r=Veykril
Add implicit static lifetime hints
2 parents 9ceaff9 + aced76d commit edb2e57

File tree

1 file changed

+85
-20
lines changed

1 file changed

+85
-20
lines changed

crates/ide/src/inlay_hints.rs

+85-20
Original file line numberDiff line numberDiff line change
@@ -117,33 +117,32 @@ pub(crate) fn inlay_hints(
117117

118118
let mut acc = Vec::new();
119119

120-
let hints = |node| hints(&mut acc, &sema, config, file_id, node);
121-
match range_limit {
122-
Some(FileRange { range, .. }) => match file.covering_element(range) {
123-
NodeOrToken::Token(_) => return acc,
124-
NodeOrToken::Node(n) => n
125-
.descendants()
126-
.filter(|descendant| range.intersect(descendant.text_range()).is_some())
127-
.for_each(hints),
128-
},
129-
None => file.descendants().for_each(hints),
130-
};
120+
if let Some(scope) = sema.scope(&file) {
121+
let famous_defs = FamousDefs(&sema, scope.krate());
122+
123+
let hints = |node| hints(&mut acc, &famous_defs, config, file_id, node);
124+
match range_limit {
125+
Some(FileRange { range, .. }) => match file.covering_element(range) {
126+
NodeOrToken::Token(_) => return acc,
127+
NodeOrToken::Node(n) => n
128+
.descendants()
129+
.filter(|descendant| range.intersect(descendant.text_range()).is_some())
130+
.for_each(hints),
131+
},
132+
None => file.descendants().for_each(hints),
133+
};
134+
}
131135

132136
acc
133137
}
134138

135139
fn hints(
136140
hints: &mut Vec<InlayHint>,
137-
sema: &Semantics<RootDatabase>,
141+
famous_defs @ FamousDefs(sema, _): &FamousDefs,
138142
config: &InlayHintsConfig,
139143
file_id: FileId,
140144
node: SyntaxNode,
141145
) {
142-
let famous_defs = match sema.scope(&node) {
143-
Some(it) => FamousDefs(sema, it.krate()),
144-
None => return,
145-
};
146-
147146
closing_brace_hints(hints, sema, config, file_id, node.clone());
148147
match_ast! {
149148
match node {
@@ -168,8 +167,18 @@ fn hints(
168167
}
169168
Some(())
170169
},
171-
ast::Fn(it) => lifetime_fn_hints(hints, config, it),
172-
_ => Some(()),
170+
ast::Item(it) => match it {
171+
// FIXME: record impl lifetimes so they aren't being reused in assoc item lifetime inlay hints
172+
ast::Item::Impl(_) => None,
173+
ast::Item::Fn(it) => fn_lifetime_fn_hints(hints, config, it),
174+
// static type elisions
175+
ast::Item::Static(it) => implicit_static_hints(hints, config, Either::Left(it)),
176+
ast::Item::Const(it) => implicit_static_hints(hints, config, Either::Right(it)),
177+
_ => None,
178+
},
179+
// FIXME: fn-ptr type, dyn fn type, and trait object type elisions
180+
ast::Type(_) => None,
181+
_ => None,
173182
}
174183
};
175184
}
@@ -279,7 +288,39 @@ fn closing_brace_hints(
279288
None
280289
}
281290

282-
fn lifetime_fn_hints(
291+
fn implicit_static_hints(
292+
acc: &mut Vec<InlayHint>,
293+
config: &InlayHintsConfig,
294+
statik_or_const: Either<ast::Static, ast::Const>,
295+
) -> Option<()> {
296+
if config.lifetime_elision_hints != LifetimeElisionHints::Always {
297+
return None;
298+
}
299+
300+
if let Either::Right(it) = &statik_or_const {
301+
if ast::AssocItemList::can_cast(
302+
it.syntax().parent().map_or(SyntaxKind::EOF, |it| it.kind()),
303+
) {
304+
return None;
305+
}
306+
}
307+
308+
if let Some(ast::Type::RefType(ty)) = statik_or_const.either(|it| it.ty(), |it| it.ty()) {
309+
if ty.lifetime().is_none() {
310+
let t = ty.amp_token()?;
311+
acc.push(InlayHint {
312+
range: t.text_range(),
313+
kind: InlayKind::LifetimeHint,
314+
label: "'static".to_owned(),
315+
tooltip: Some(InlayTooltip::String("Elided static lifetime".into())),
316+
});
317+
}
318+
}
319+
320+
Some(())
321+
}
322+
323+
fn fn_lifetime_fn_hints(
283324
acc: &mut Vec<InlayHint>,
284325
config: &InlayHintsConfig,
285326
func: ast::Fn,
@@ -2593,6 +2634,30 @@ impl () {
25932634
);
25942635
}
25952636

2637+
#[test]
2638+
fn hints_lifetimes_static() {
2639+
check_with_config(
2640+
InlayHintsConfig {
2641+
lifetime_elision_hints: LifetimeElisionHints::Always,
2642+
..TEST_CONFIG
2643+
},
2644+
r#"
2645+
trait Trait {}
2646+
static S: &str = "";
2647+
// ^'static
2648+
const C: &str = "";
2649+
// ^'static
2650+
const C: &dyn Trait = panic!();
2651+
// ^'static
2652+
2653+
impl () {
2654+
const C: &str = "";
2655+
const C: &dyn Trait = panic!();
2656+
}
2657+
"#,
2658+
);
2659+
}
2660+
25962661
#[test]
25972662
fn hints_implicit_reborrow() {
25982663
check_with_config(

0 commit comments

Comments
 (0)