2420: Remove last traces of adt from Ty r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-11-26 18:42:52 +00:00 committed by GitHub
commit d770f22c53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 68 additions and 75 deletions

View file

@ -982,7 +982,7 @@ impl ImplBlock {
} }
} }
#[derive(Clone, PartialEq, Eq)] #[derive(Clone, PartialEq, Eq, Debug)]
pub struct Type { pub struct Type {
pub(crate) krate: CrateId, pub(crate) krate: CrateId,
pub(crate) ty: InEnvironment<Ty>, pub(crate) ty: InEnvironment<Ty>,
@ -1104,7 +1104,7 @@ impl Type {
pub fn as_adt(&self) -> Option<Adt> { pub fn as_adt(&self) -> Option<Adt> {
let (adt, _subst) = self.ty.value.as_adt()?; let (adt, _subst) = self.ty.value.as_adt()?;
Some(adt) Some(adt.into())
} }
fn derived(&self, ty: Ty) -> Type { fn derived(&self, ty: Ty) -> Type {

View file

@ -12,7 +12,7 @@ use crate::{
db::HirDatabase, db::HirDatabase,
diagnostics::{MissingFields, MissingOkInTailExpr}, diagnostics::{MissingFields, MissingOkInTailExpr},
ty::{ApplicationTy, InferenceResult, Ty, TypeCtor}, ty::{ApplicationTy, InferenceResult, Ty, TypeCtor},
Adt, Function, Name, Path, Function, Name, Path, Struct,
}; };
pub use hir_def::{ pub use hir_def::{
@ -69,7 +69,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
} }
let struct_def = match self.infer[id].as_adt() { let struct_def = match self.infer[id].as_adt() {
Some((Adt::Struct(s), _)) => s, Some((AdtId::StructId(s), _)) => Struct::from(s),
_ => return, _ => return,
}; };

View file

@ -22,13 +22,14 @@ use hir_def::{
expr::ExprId, generics::GenericParams, type_ref::Mutability, AdtId, ContainerId, DefWithBodyId, expr::ExprId, generics::GenericParams, type_ref::Mutability, AdtId, ContainerId, DefWithBodyId,
GenericDefId, HasModule, Lookup, TraitId, TypeAliasId, GenericDefId, HasModule, Lookup, TraitId, TypeAliasId,
}; };
use hir_expand::name::Name;
use ra_db::{impl_intern_key, salsa}; use ra_db::{impl_intern_key, salsa};
use crate::{ use crate::{
db::HirDatabase, db::HirDatabase,
ty::primitive::{FloatTy, IntTy, Uncertain}, ty::primitive::{FloatTy, IntTy, Uncertain},
util::make_mut_slice, util::make_mut_slice,
Adt, Crate, Name, Crate,
}; };
use display::{HirDisplay, HirFormatter}; 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 { match self {
Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(adt_def), parameters }) => { Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(adt_def), parameters }) => {
Some(((*adt_def).into(), parameters)) Some((*adt_def, parameters))
} }
_ => None, _ => None,
} }

View file

@ -71,10 +71,11 @@ pub(crate) fn reference_definition(
Some(nav) => return Exact(nav), Some(nav) => return Exact(nav),
None => return Approximate(vec![]), None => return Approximate(vec![]),
}, },
Some(SelfType(ty)) => { Some(SelfType(imp)) => {
if let Some((adt, _)) = ty.as_adt() { // FIXME: ideally, this should point to the type in the impl, and
return Exact(adt.to_nav(db)); // 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(Local(local)) => return Exact(local.to_nav(db)),
Some(GenericParam(_)) => { 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( 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( 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( 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( check_goto(
@ -572,11 +573,11 @@ mod tests {
} }
impl Make for Foo { impl Make for Foo {
fn new() -> Self<|> { fn new() -> Self<|> {
Self{} Self {}
} }
} }
", ",
"Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", "impl IMPL_BLOCK FileId(1) [49; 115)",
); );
} }

View file

@ -133,20 +133,12 @@ fn hover_text_from_name_kind(
hir::ModuleDef::TypeAlias(it) => from_def_source(db, it), hir::ModuleDef::TypeAlias(it) => from_def_source(db, it),
hir::ModuleDef::BuiltinType(it) => Some(it.to_string()), 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(_) => { Local(_) => {
// Hover for these shows type names // Hover for these shows type names
*no_fallback = true; *no_fallback = true;
None None
} }
GenericParam(_) => { GenericParam(_) | SelfType(_) => {
// FIXME: Hover for generic param // FIXME: Hover for generic param
None None
} }
@ -622,49 +614,52 @@ fn func(foo: i32) { if true { <|>foo; }; }
", ",
); );
let hover = analysis.hover(position).unwrap().unwrap(); let hover = analysis.hover(position).unwrap().unwrap();
assert_eq!(trim_markup_opt(hover.info.first()), Some("struct Thing")); assert_eq!(trim_markup_opt(hover.info.first()), Some("Thing"));
assert_eq!(hover.info.is_exact(), true); assert_eq!(hover.info.is_exact(), true);
let (analysis, position) = single_file_with_position( /* FIXME: revive these tests
" let (analysis, position) = single_file_with_position(
struct Thing { x: u32 } "
impl Thing { struct Thing { x: u32 }
fn new() -> Self<|> { impl Thing {
Self { x: 0 } 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( let hover = analysis.hover(position).unwrap().unwrap();
" assert_eq!(trim_markup_opt(hover.info.first()), Some("Thing"));
enum Thing { A } assert_eq!(hover.info.is_exact(), true);
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( let (analysis, position) = single_file_with_position(
" "
enum Thing { A } enum Thing { A }
impl Thing { impl Thing {
pub fn thing(a: Self<|>) { pub fn new() -> Self<|> {
} Thing::A
} }
", }
); ",
let hover = analysis.hover(position).unwrap().unwrap(); );
assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing")); let hover = analysis.hover(position).unwrap().unwrap();
assert_eq!(hover.info.is_exact(), true); 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] #[test]

View file

@ -83,10 +83,7 @@ pub(crate) fn find_all_refs(
NameKind::Field(field) => field.to_nav(db), NameKind::Field(field) => field.to_nav(db),
NameKind::AssocItem(assoc) => assoc.to_nav(db), NameKind::AssocItem(assoc) => assoc.to_nav(db),
NameKind::Def(def) => NavigationTarget::from_def(db, def)?, NameKind::Def(def) => NavigationTarget::from_def(db, def)?,
NameKind::SelfType(ref ty) => match ty.as_adt() { NameKind::SelfType(imp) => imp.to_nav(db),
Some((adt, _)) => adt.to_nav(db),
None => return None,
},
NameKind::Local(local) => local.to_nav(db), NameKind::Local(local) => local.to_nav(db),
NameKind::GenericParam(_) => return None, NameKind::GenericParam(_) => return None,
}; };

View file

@ -178,8 +178,7 @@ pub(crate) fn classify_name_ref(
Some(NameDefinition { kind, container, visibility }) Some(NameDefinition { kind, container, visibility })
} }
PathResolution::SelfType(impl_block) => { PathResolution::SelfType(impl_block) => {
let ty = impl_block.target_ty(db); let kind = NameKind::SelfType(impl_block);
let kind = NameKind::SelfType(ty);
let container = impl_block.module(db); let container = impl_block.module(db);
Some(NameDefinition { kind, container, visibility }) Some(NameDefinition { kind, container, visibility })
} }

View file

@ -4,8 +4,8 @@
//! Note that the reference search is possible for not all of the classified items. //! Note that the reference search is possible for not all of the classified items.
use hir::{ use hir::{
Adt, AssocItem, GenericParam, HasSource, Local, MacroDef, Module, ModuleDef, StructField, Ty, Adt, AssocItem, GenericParam, HasSource, ImplBlock, Local, MacroDef, Module, ModuleDef,
VariantDef, StructField, VariantDef,
}; };
use ra_syntax::{ast, ast::VisibilityOwner}; use ra_syntax::{ast, ast::VisibilityOwner};
@ -17,7 +17,7 @@ pub enum NameKind {
Field(StructField), Field(StructField),
AssocItem(AssocItem), AssocItem(AssocItem),
Def(ModuleDef), Def(ModuleDef),
SelfType(Ty), SelfType(ImplBlock),
Local(Local), Local(Local),
GenericParam(GenericParam), GenericParam(GenericParam),
} }