From 882fe0a47ee6f60928395326d1f194eec521ce2e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Nov 2019 21:18:26 +0300 Subject: More precise NameKind::Self --- crates/ra_hir/src/code_model.rs | 2 +- crates/ra_ide_api/src/goto_definition.rs | 23 +++--- crates/ra_ide_api/src/hover.rs | 95 ++++++++++------------ crates/ra_ide_api/src/references.rs | 5 +- crates/ra_ide_api/src/references/classify.rs | 3 +- .../ra_ide_api/src/references/name_definition.rs | 6 +- 6 files changed, 63 insertions(+), 71 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index c5cf39ee1..0edcdb177 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -982,7 +982,7 @@ impl ImplBlock { } } -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq, Debug)] pub struct Type { pub(crate) krate: CrateId, pub(crate) ty: InEnvironment, diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index b6c72efdf..c10a6c844 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -71,10 +71,11 @@ pub(crate) fn reference_definition( Some(nav) => return Exact(nav), None => return Approximate(vec![]), }, - Some(SelfType(ty)) => { - if let Some((adt, _)) = ty.as_adt() { - return Exact(adt.to_nav(db)); - } + Some(SelfType(imp)) => { + // FIXME: ideally, this should point to the type in the impl, and + // not at the whole impl. And goto **type** definition should bring + // us to the actual type + return Exact(imp.to_nav(db)); } Some(Local(local)) => return Exact(local.to_nav(db)), Some(GenericParam(_)) => { @@ -503,7 +504,7 @@ mod tests { } } ", - "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + "impl IMPL_BLOCK FileId(1) [12; 73)", ); check_goto( @@ -516,7 +517,7 @@ mod tests { } } ", - "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + "impl IMPL_BLOCK FileId(1) [12; 73)", ); check_goto( @@ -529,7 +530,7 @@ mod tests { } } ", - "Foo ENUM_DEF FileId(1) [0; 14) [5; 8)", + "impl IMPL_BLOCK FileId(1) [15; 75)", ); check_goto( @@ -541,7 +542,7 @@ mod tests { } } ", - "Foo ENUM_DEF FileId(1) [0; 14) [5; 8)", + "impl IMPL_BLOCK FileId(1) [15; 62)", ); } @@ -560,7 +561,7 @@ mod tests { } } ", - "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + "impl IMPL_BLOCK FileId(1) [49; 115)", ); check_goto( @@ -572,11 +573,11 @@ mod tests { } impl Make for Foo { fn new() -> Self<|> { - Self{} + Self {} } } ", - "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + "impl IMPL_BLOCK FileId(1) [49; 115)", ); } diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index 9839be985..260a7b869 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -133,20 +133,12 @@ fn hover_text_from_name_kind( hir::ModuleDef::TypeAlias(it) => from_def_source(db, it), hir::ModuleDef::BuiltinType(it) => Some(it.to_string()), }, - SelfType(ty) => match ty.as_adt() { - Some((adt_def, _)) => match adt_def { - hir::Adt::Struct(it) => from_def_source(db, it), - hir::Adt::Union(it) => from_def_source(db, it), - hir::Adt::Enum(it) => from_def_source(db, it), - }, - _ => None, - }, Local(_) => { // Hover for these shows type names *no_fallback = true; None } - GenericParam(_) => { + GenericParam(_) | SelfType(_) => { // FIXME: Hover for generic param None } @@ -622,49 +614,52 @@ fn func(foo: i32) { if true { <|>foo; }; } ", ); let hover = analysis.hover(position).unwrap().unwrap(); - assert_eq!(trim_markup_opt(hover.info.first()), Some("struct Thing")); - assert_eq!(hover.info.is_exact(), true); - - let (analysis, position) = single_file_with_position( - " - struct Thing { x: u32 } - impl Thing { - fn new() -> Self<|> { - Self { x: 0 } - } - } - ", - ); - let hover = analysis.hover(position).unwrap().unwrap(); - assert_eq!(trim_markup_opt(hover.info.first()), Some("struct Thing")); - assert_eq!(hover.info.is_exact(), true); - - let (analysis, position) = single_file_with_position( - " - enum Thing { A } - impl Thing { - pub fn new() -> Self<|> { - Thing::A - } - } - ", - ); - let hover = analysis.hover(position).unwrap().unwrap(); - assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing")); + assert_eq!(trim_markup_opt(hover.info.first()), Some("Thing")); assert_eq!(hover.info.is_exact(), true); - let (analysis, position) = single_file_with_position( - " - enum Thing { A } - impl Thing { - pub fn thing(a: Self<|>) { - } - } - ", - ); - let hover = analysis.hover(position).unwrap().unwrap(); - assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing")); - assert_eq!(hover.info.is_exact(), true); + /* FIXME: revive these tests + let (analysis, position) = single_file_with_position( + " + struct Thing { x: u32 } + impl Thing { + fn new() -> Self<|> { + Self { x: 0 } + } + } + ", + ); + + let hover = analysis.hover(position).unwrap().unwrap(); + assert_eq!(trim_markup_opt(hover.info.first()), Some("Thing")); + assert_eq!(hover.info.is_exact(), true); + + let (analysis, position) = single_file_with_position( + " + enum Thing { A } + impl Thing { + pub fn new() -> Self<|> { + Thing::A + } + } + ", + ); + let hover = analysis.hover(position).unwrap().unwrap(); + assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing")); + assert_eq!(hover.info.is_exact(), true); + + let (analysis, position) = single_file_with_position( + " + enum Thing { A } + impl Thing { + pub fn thing(a: Self<|>) { + } + } + ", + ); + let hover = analysis.hover(position).unwrap().unwrap(); + assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing")); + assert_eq!(hover.info.is_exact(), true); + */ } #[test] diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index cb343e59a..21a1ea69e 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs @@ -83,10 +83,7 @@ pub(crate) fn find_all_refs( NameKind::Field(field) => field.to_nav(db), NameKind::AssocItem(assoc) => assoc.to_nav(db), NameKind::Def(def) => NavigationTarget::from_def(db, def)?, - NameKind::SelfType(ref ty) => match ty.as_adt() { - Some((adt, _)) => adt.to_nav(db), - None => return None, - }, + NameKind::SelfType(imp) => imp.to_nav(db), NameKind::Local(local) => local.to_nav(db), NameKind::GenericParam(_) => return None, }; diff --git a/crates/ra_ide_api/src/references/classify.rs b/crates/ra_ide_api/src/references/classify.rs index 227737ad2..5cea805ec 100644 --- a/crates/ra_ide_api/src/references/classify.rs +++ b/crates/ra_ide_api/src/references/classify.rs @@ -178,8 +178,7 @@ pub(crate) fn classify_name_ref( Some(NameDefinition { kind, container, visibility }) } PathResolution::SelfType(impl_block) => { - let ty = impl_block.target_ty(db); - let kind = NameKind::SelfType(ty); + let kind = NameKind::SelfType(impl_block); let container = impl_block.module(db); Some(NameDefinition { kind, container, visibility }) } diff --git a/crates/ra_ide_api/src/references/name_definition.rs b/crates/ra_ide_api/src/references/name_definition.rs index cf12db066..10d3a2364 100644 --- a/crates/ra_ide_api/src/references/name_definition.rs +++ b/crates/ra_ide_api/src/references/name_definition.rs @@ -4,8 +4,8 @@ //! Note that the reference search is possible for not all of the classified items. use hir::{ - Adt, AssocItem, GenericParam, HasSource, Local, MacroDef, Module, ModuleDef, StructField, Ty, - VariantDef, + Adt, AssocItem, GenericParam, HasSource, ImplBlock, Local, MacroDef, Module, ModuleDef, + StructField, VariantDef, }; use ra_syntax::{ast, ast::VisibilityOwner}; @@ -17,7 +17,7 @@ pub enum NameKind { Field(StructField), AssocItem(AssocItem), Def(ModuleDef), - SelfType(Ty), + SelfType(ImplBlock), Local(Local), GenericParam(GenericParam), } -- cgit v1.2.3 From 936c6950e78d073f54c9ba66795f7f6f3abb351b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Nov 2019 21:25:17 +0300 Subject: Remove last traces of adt from Ty --- crates/ra_hir/src/code_model.rs | 2 +- crates/ra_hir/src/expr.rs | 4 ++-- crates/ra_hir/src/ty.rs | 7 ++++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 0edcdb177..a842dfed6 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -1104,7 +1104,7 @@ impl Type { pub fn as_adt(&self) -> Option { let (adt, _subst) = self.ty.value.as_adt()?; - Some(adt) + Some(adt.into()) } fn derived(&self, ty: Ty) -> Type { diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index adb9805ab..5c82c23d6 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -12,7 +12,7 @@ use crate::{ db::HirDatabase, diagnostics::{MissingFields, MissingOkInTailExpr}, ty::{ApplicationTy, InferenceResult, Ty, TypeCtor}, - Adt, Function, Name, Path, + Function, Name, Path, Struct, }; pub use hir_def::{ @@ -69,7 +69,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { } let struct_def = match self.infer[id].as_adt() { - Some((Adt::Struct(s), _)) => s, + Some((AdtId::StructId(s), _)) => Struct::from(s), _ => return, }; diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 680ddc2f9..791b6064a 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -22,13 +22,14 @@ use hir_def::{ expr::ExprId, generics::GenericParams, type_ref::Mutability, AdtId, ContainerId, DefWithBodyId, GenericDefId, HasModule, Lookup, TraitId, TypeAliasId, }; +use hir_expand::name::Name; use ra_db::{impl_intern_key, salsa}; use crate::{ db::HirDatabase, ty::primitive::{FloatTy, IntTy, Uncertain}, util::make_mut_slice, - Adt, Crate, Name, + Crate, }; use display::{HirDisplay, HirFormatter}; @@ -598,10 +599,10 @@ impl Ty { } } - pub fn as_adt(&self) -> Option<(Adt, &Substs)> { + pub fn as_adt(&self) -> Option<(AdtId, &Substs)> { match self { Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(adt_def), parameters }) => { - Some(((*adt_def).into(), parameters)) + Some((*adt_def, parameters)) } _ => None, } -- cgit v1.2.3