Skip to content

Commit 5dd99aa

Browse files
bors[bot]matklad
andauthored
Merge #6301
6301: Don't rely on display names in inlay_hints r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents f925735 + a3a8ad8 commit 5dd99aa

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

crates/assists/src/handlers/fill_match_arms.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<
5959
.filter(|variant_pat| is_variant_missing(&mut arms, variant_pat))
6060
.map(|pat| make::match_arm(iter::once(pat), make::expr_empty_block()))
6161
.collect::<Vec<_>>();
62-
if Some(enum_def) == FamousDefs(&ctx.sema, module.krate()).core_option_Option() {
62+
if Some(enum_def) == FamousDefs(&ctx.sema, Some(module.krate())).core_option_Option() {
6363
// Match `Some` variant first.
6464
mark::hit!(option_order);
6565
variants.reverse()

crates/assists/src/handlers/generate_from_impl_for_enum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn existing_from_impl(
7575
let enum_ = variant.parent_enum(sema.db);
7676
let krate = enum_.module(sema.db).krate();
7777

78-
let from_trait = FamousDefs(sema, krate).core_convert_From()?;
78+
let from_trait = FamousDefs(sema, Some(krate)).core_convert_From()?;
7979

8080
let enum_type = enum_.ty(sema.db);
8181

crates/assists/src/utils.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ impl TryEnum {
275275
/// somewhat similar to the known paths infra inside hir, but it different; We
276276
/// want to make sure that IDE specific paths don't become interesting inside
277277
/// the compiler itself as well.
278-
pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Crate);
278+
pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Option<Crate>);
279279

280280
#[allow(non_snake_case)]
281281
impl FamousDefs<'_, '_> {
@@ -362,6 +362,10 @@ pub mod prelude {
362362
pub use prelude::*;
363363
"#;
364364

365+
pub fn core(&self) -> Option<Crate> {
366+
self.find_crate("core")
367+
}
368+
365369
pub(crate) fn core_convert_From(&self) -> Option<Trait> {
366370
self.find_trait("core:convert:From")
367371
}
@@ -399,21 +403,20 @@ pub use prelude::*;
399403
}
400404
}
401405

406+
fn find_crate(&self, name: &str) -> Option<Crate> {
407+
let krate = self.1?;
408+
let db = self.0.db;
409+
let res =
410+
krate.dependencies(db).into_iter().find(|dep| dep.name.to_string() == name)?.krate;
411+
Some(res)
412+
}
413+
402414
fn find_def(&self, path: &str) -> Option<ScopeDef> {
403415
let db = self.0.db;
404416
let mut path = path.split(':');
405417
let trait_ = path.next_back()?;
406418
let std_crate = path.next()?;
407-
let std_crate = if self
408-
.1
409-
.display_name(db)
410-
.map(|name| name.to_string() == std_crate)
411-
.unwrap_or(false)
412-
{
413-
self.1
414-
} else {
415-
self.1.dependencies(db).into_iter().find(|dep| dep.name.to_string() == std_crate)?.krate
416-
};
419+
let std_crate = self.find_crate(std_crate)?;
417420
let mut module = std_crate.root_module(db);
418421
for segment in path {
419422
module = module.children(db).find_map(|child| {

crates/ide/src/inlay_hints.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ fn get_chaining_hints(
9999
return None;
100100
}
101101

102+
let krate = sema.scope(expr.syntax()).module().map(|it| it.krate());
103+
let famous_defs = FamousDefs(&sema, krate);
104+
102105
let mut tokens = expr
103106
.syntax()
104107
.siblings_with_tokens(Direction::Next)
@@ -128,7 +131,7 @@ fn get_chaining_hints(
128131
acc.push(InlayHint {
129132
range: expr.syntax().text_range(),
130133
kind: InlayKind::ChainingHint,
131-
label: hint_iterator(sema, config, &ty).unwrap_or_else(|| {
134+
label: hint_iterator(sema, &famous_defs, config, &ty).unwrap_or_else(|| {
132135
ty.display_truncated(sema.db, config.max_length).to_string().into()
133136
}),
134137
});
@@ -188,6 +191,9 @@ fn get_bind_pat_hints(
188191
return None;
189192
}
190193

194+
let krate = sema.scope(pat.syntax()).module().map(|it| it.krate());
195+
let famous_defs = FamousDefs(&sema, krate);
196+
191197
let ty = sema.type_of_pat(&pat.clone().into())?;
192198

193199
if should_not_display_type_hint(sema, &pat, &ty) {
@@ -196,7 +202,7 @@ fn get_bind_pat_hints(
196202
acc.push(InlayHint {
197203
range: pat.syntax().text_range(),
198204
kind: InlayKind::TypeHint,
199-
label: hint_iterator(sema, config, &ty)
205+
label: hint_iterator(sema, &famous_defs, config, &ty)
200206
.unwrap_or_else(|| ty.display_truncated(sema.db, config.max_length).to_string().into()),
201207
});
202208

@@ -206,6 +212,7 @@ fn get_bind_pat_hints(
206212
/// Checks if the type is an Iterator from std::iter and replaces its hint with an `impl Iterator<Item = Ty>`.
207213
fn hint_iterator(
208214
sema: &Semantics<RootDatabase>,
215+
famous_defs: &FamousDefs,
209216
config: &InlayHintsConfig,
210217
ty: &hir::Type,
211218
) -> Option<SmolStr> {
@@ -214,11 +221,11 @@ fn hint_iterator(
214221
.last()
215222
.and_then(|strukt| strukt.as_adt())?;
216223
let krate = strukt.krate(db)?;
217-
if krate.display_name(db).as_deref() != Some("core") {
224+
if krate != famous_defs.core()? {
218225
return None;
219226
}
220-
let iter_trait = FamousDefs(sema, krate).core_iter_Iterator()?;
221-
let iter_mod = FamousDefs(sema, krate).core_iter()?;
227+
let iter_trait = famous_defs.core_iter_Iterator()?;
228+
let iter_mod = famous_defs.core_iter()?;
222229
// assert this struct comes from `core::iter`
223230
iter_mod.visibility_of(db, &strukt.into()).filter(|&vis| vis == hir::Visibility::Public)?;
224231
if ty.impls_trait(db, iter_trait, &[]) {
@@ -230,7 +237,7 @@ fn hint_iterator(
230237
const LABEL_START: &str = "impl Iterator<Item = ";
231238
const LABEL_END: &str = ">";
232239

233-
let ty_display = hint_iterator(sema, config, &ty)
240+
let ty_display = hint_iterator(sema, famous_defs, config, &ty)
234241
.map(|assoc_type_impl| assoc_type_impl.to_string())
235242
.unwrap_or_else(|| {
236243
ty.display_truncated(

0 commit comments

Comments
 (0)