From a3a8ad8bc45d87607493d3c07d7e89e54f7b8c11 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 20 Oct 2020 17:38:21 +0200 Subject: Don't rely on display names in inlay_hints --- crates/ide/src/inlay_hints.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 56b985e80..cccea129a 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -99,6 +99,9 @@ fn get_chaining_hints( return None; } + let krate = sema.scope(expr.syntax()).module().map(|it| it.krate()); + let famous_defs = FamousDefs(&sema, krate); + let mut tokens = expr .syntax() .siblings_with_tokens(Direction::Next) @@ -128,7 +131,7 @@ fn get_chaining_hints( acc.push(InlayHint { range: expr.syntax().text_range(), kind: InlayKind::ChainingHint, - label: hint_iterator(sema, config, &ty).unwrap_or_else(|| { + label: hint_iterator(sema, &famous_defs, config, &ty).unwrap_or_else(|| { ty.display_truncated(sema.db, config.max_length).to_string().into() }), }); @@ -188,6 +191,9 @@ fn get_bind_pat_hints( return None; } + let krate = sema.scope(pat.syntax()).module().map(|it| it.krate()); + let famous_defs = FamousDefs(&sema, krate); + let ty = sema.type_of_pat(&pat.clone().into())?; if should_not_display_type_hint(sema, &pat, &ty) { @@ -196,7 +202,7 @@ fn get_bind_pat_hints( acc.push(InlayHint { range: pat.syntax().text_range(), kind: InlayKind::TypeHint, - label: hint_iterator(sema, config, &ty) + label: hint_iterator(sema, &famous_defs, config, &ty) .unwrap_or_else(|| ty.display_truncated(sema.db, config.max_length).to_string().into()), }); @@ -206,6 +212,7 @@ fn get_bind_pat_hints( /// Checks if the type is an Iterator from std::iter and replaces its hint with an `impl Iterator`. fn hint_iterator( sema: &Semantics, + famous_defs: &FamousDefs, config: &InlayHintsConfig, ty: &hir::Type, ) -> Option { @@ -214,11 +221,11 @@ fn hint_iterator( .last() .and_then(|strukt| strukt.as_adt())?; let krate = strukt.krate(db)?; - if krate.display_name(db).as_deref() != Some("core") { + if krate != famous_defs.core()? { return None; } - let iter_trait = FamousDefs(sema, krate).core_iter_Iterator()?; - let iter_mod = FamousDefs(sema, krate).core_iter()?; + let iter_trait = famous_defs.core_iter_Iterator()?; + let iter_mod = famous_defs.core_iter()?; // assert this struct comes from `core::iter` iter_mod.visibility_of(db, &strukt.into()).filter(|&vis| vis == hir::Visibility::Public)?; if ty.impls_trait(db, iter_trait, &[]) { @@ -230,7 +237,7 @@ fn hint_iterator( const LABEL_START: &str = "impl Iterator