From 78f3b0627cf4a5d34aaf63c7b5a2e1b744a11b14 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 17:10:51 +0300 Subject: Move FunctionData to hir_def --- crates/ra_hir/src/code_model.rs | 87 ++++--------------------------- crates/ra_hir/src/db.rs | 13 ++--- crates/ra_hir/src/lib.rs | 6 +-- crates/ra_hir/src/ty/infer.rs | 13 ++--- crates/ra_hir/src/ty/lower.rs | 6 +-- crates/ra_hir/src/ty/method_resolution.rs | 6 +-- 6 files changed, 31 insertions(+), 100 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 72c9b466f..f426f8c9f 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -13,7 +13,7 @@ use hir_def::{ nameres::per_ns::PerNs, resolver::{HasResolver, TypeNs}, traits::TraitData, - type_ref::{Mutability, TypeRef}, + type_ref::TypeRef, ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup, ModuleId, UnionId, }; @@ -561,77 +561,6 @@ pub struct Function { pub(crate) id: FunctionId, } -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct FnData { - pub(crate) name: Name, - pub(crate) params: Vec, - pub(crate) ret_type: TypeRef, - /// True if the first param is `self`. This is relevant to decide whether this - /// can be called as a method. - pub(crate) has_self_param: bool, -} - -impl FnData { - pub(crate) fn fn_data_query( - db: &(impl DefDatabase + AstDatabase), - func: Function, - ) -> Arc { - let src = func.source(db); - let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); - let mut params = Vec::new(); - let mut has_self_param = false; - if let Some(param_list) = src.value.param_list() { - if let Some(self_param) = param_list.self_param() { - let self_type = if let Some(type_ref) = self_param.ascribed_type() { - TypeRef::from_ast(type_ref) - } else { - let self_type = TypeRef::Path(name::SELF_TYPE.into()); - match self_param.kind() { - ast::SelfParamKind::Owned => self_type, - ast::SelfParamKind::Ref => { - TypeRef::Reference(Box::new(self_type), Mutability::Shared) - } - ast::SelfParamKind::MutRef => { - TypeRef::Reference(Box::new(self_type), Mutability::Mut) - } - } - }; - params.push(self_type); - has_self_param = true; - } - for param in param_list.params() { - let type_ref = TypeRef::from_ast_opt(param.ascribed_type()); - params.push(type_ref); - } - } - let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) { - TypeRef::from_ast(type_ref) - } else { - TypeRef::unit() - }; - - let sig = FnData { name, params, ret_type, has_self_param }; - Arc::new(sig) - } - pub fn name(&self) -> &Name { - &self.name - } - - pub fn params(&self) -> &[TypeRef] { - &self.params - } - - pub fn ret_type(&self) -> &TypeRef { - &self.ret_type - } - - /// True if the first arg is `self`. This is relevant to decide whether this - /// can be called as a method. - pub fn has_self_param(&self) -> bool { - self.has_self_param - } -} - impl Function { pub fn module(self, db: &impl DefDatabase) -> Module { self.id.lookup(db).module(db).into() @@ -642,7 +571,15 @@ impl Function { } pub fn name(self, db: &impl HirDatabase) -> Name { - self.data(db).name.clone() + db.function_data(self.id).name.clone() + } + + pub fn has_self_param(self, db: &impl HirDatabase) -> bool { + db.function_data(self.id).has_self_param + } + + pub fn params(self, db: &impl HirDatabase) -> Vec { + db.function_data(self.id).params.clone() } pub(crate) fn body_source_map(self, db: &impl HirDatabase) -> Arc { @@ -657,10 +594,6 @@ impl Function { db.type_for_def(self.into(), Namespace::Values) } - pub fn data(self, db: &impl HirDatabase) -> Arc { - db.fn_data(self) - } - pub fn infer(self, db: &impl HirDatabase) -> Arc { db.infer(self.into()) } diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 1cfcb2fd2..8b9af0565 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -16,15 +16,15 @@ use crate::{ CallableDef, FnSig, GenericPredicate, InferenceResult, Namespace, Substs, Ty, TypableDef, TypeCtor, }, - Const, ConstData, Crate, DefWithBody, FnData, Function, GenericDef, ImplBlock, Module, Static, - StructField, Trait, + Const, ConstData, Crate, DefWithBody, GenericDef, ImplBlock, Module, Static, StructField, + Trait, }; pub use hir_def::db::{ BodyQuery, BodyWithSourceMapQuery, CrateDefMapQuery, DefDatabase2, DefDatabase2Storage, - EnumDataQuery, ExprScopesQuery, GenericParamsQuery, ImplDataQuery, InternDatabase, - InternDatabaseStorage, RawItemsQuery, RawItemsWithSourceMapQuery, StructDataQuery, - TraitDataQuery, TypeAliasDataQuery, + EnumDataQuery, ExprScopesQuery, FunctionDataQuery, GenericParamsQuery, ImplDataQuery, + InternDatabase, InternDatabaseStorage, RawItemsQuery, RawItemsWithSourceMapQuery, + StructDataQuery, TraitDataQuery, TypeAliasDataQuery, }; pub use hir_expand::db::{ AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, @@ -35,9 +35,6 @@ pub use hir_expand::db::{ #[salsa::query_group(DefDatabaseStorage)] #[salsa::requires(AstDatabase)] pub trait DefDatabase: HirDebugDatabase + DefDatabase2 { - #[salsa::invoke(FnData::fn_data_query)] - fn fn_data(&self, func: Function) -> Arc; - #[salsa::invoke(ConstData::const_data_query)] fn const_data(&self, konst: Const) -> Arc; diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 8535629ca..d29cc9258 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -55,9 +55,9 @@ pub use crate::{ docs::{DocDef, Docs, Documentation}, src::{HasBodySource, HasSource}, Adt, AssocItem, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum, - EnumVariant, FieldSource, FnData, Function, GenericDef, GenericParam, HasBody, ImplBlock, - Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, - Trait, TypeAlias, Union, VariantDef, + EnumVariant, FieldSource, Function, GenericDef, GenericParam, HasBody, ImplBlock, Local, + MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, Trait, + TypeAlias, Union, VariantDef, }, expr::ExprScopes, from_source::FromSource, diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 69b13baef..41a51283d 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -22,6 +22,7 @@ use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; use rustc_hash::FxHashMap; use hir_def::{ + function::FunctionData, path::known, resolver::{HasResolver, Resolver, TypeNs}, type_ref::{Mutability, TypeRef}, @@ -43,8 +44,8 @@ use crate::{ db::HirDatabase, expr::{BindingAnnotation, Body, ExprId, PatId}, ty::infer::diagnostics::InferenceDiagnostic, - Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path, - StructField, Trait, VariantDef, + Adt, AssocItem, ConstData, DefWithBody, FloatTy, Function, HasBody, IntTy, Path, StructField, + Trait, VariantDef, }; macro_rules! ty_app { @@ -70,7 +71,7 @@ pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc ctx.collect_const(&c.data(db)), - DefWithBody::Function(ref f) => ctx.collect_fn(&f.data(db)), + DefWithBody::Function(ref f) => ctx.collect_fn(&db.function_data(f.id)), DefWithBody::Static(ref s) => ctx.collect_const(&s.data(db)), } @@ -562,14 +563,14 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { self.return_ty = self.make_ty(data.type_ref()); } - fn collect_fn(&mut self, data: &FnData) { + fn collect_fn(&mut self, data: &FunctionData) { let body = Arc::clone(&self.body); // avoid borrow checker problem - for (type_ref, pat) in data.params().iter().zip(body.params()) { + for (type_ref, pat) in data.params.iter().zip(body.params()) { let ty = self.make_ty(type_ref); self.infer_pat(*pat, &ty, BindingMode::default()); } - self.return_ty = self.make_ty(data.ret_type()); + self.return_ty = self.make_ty(&data.ret_type); } fn infer_body(&mut self) { diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 75c552569..42daa9cb9 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -622,10 +622,10 @@ pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> } fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { - let data = def.data(db); + let data = db.function_data(def.id); let resolver = def.id.resolver(db); - let params = data.params().iter().map(|tr| Ty::from_hir(db, &resolver, tr)).collect::>(); - let ret = Ty::from_hir(db, &resolver, data.ret_type()); + let params = data.params.iter().map(|tr| Ty::from_hir(db, &resolver, tr)).collect::>(); + let ret = Ty::from_hir(db, &resolver, &data.ret_type); FnSig::from_params_and_return(params, ret) } diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 64adb814d..f84aae26e 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -291,9 +291,9 @@ fn is_valid_candidate( ) -> bool { match item { AssocItem::Function(m) => { - let data = m.data(db); - name.map_or(true, |name| data.name() == name) - && (data.has_self_param() || mode == LookupMode::Path) + let data = db.function_data(m.id); + name.map_or(true, |name| data.name == *name) + && (data.has_self_param || mode == LookupMode::Path) } AssocItem::Const(c) => { name.map_or(true, |name| Some(name) == c.name(db).as_ref()) -- cgit v1.2.3 From 64df759418a83fd05fab850b8351fcddbdd3ef63 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 17:33:53 +0300 Subject: Make ImplData's fields public --- crates/ra_hir/src/code_model.rs | 2 +- crates/ra_hir/src/impl_block.rs | 8 ++++---- crates/ra_hir/src/ty/infer.rs | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index f426f8c9f..3daf7488e 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -10,9 +10,9 @@ use hir_def::{ adt::VariantData, body::scope::ExprScopes, builtin_type::BuiltinType, + data::TraitData, nameres::per_ns::PerNs, resolver::{HasResolver, TypeNs}, - traits::TraitData, type_ref::TypeRef, ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup, ModuleId, UnionId, diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 774fa1d96..334eeebac 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -18,11 +18,11 @@ impl HasSource for ImplBlock { impl ImplBlock { pub fn target_trait(&self, db: &impl DefDatabase) -> Option { - db.impl_data(self.id).target_trait().cloned() + db.impl_data(self.id).target_trait.clone() } pub fn target_type(&self, db: &impl DefDatabase) -> TypeRef { - db.impl_data(self.id).target_type().clone() + db.impl_data(self.id).target_type.clone() } pub fn target_ty(&self, db: &impl HirDatabase) -> Ty { @@ -35,11 +35,11 @@ impl ImplBlock { } pub fn items(&self, db: &impl DefDatabase) -> Vec { - db.impl_data(self.id).items().iter().map(|it| (*it).into()).collect() + db.impl_data(self.id).items.iter().map(|it| (*it).into()).collect() } pub fn is_negative(&self, db: &impl DefDatabase) -> bool { - db.impl_data(self.id).is_negative() + db.impl_data(self.id).is_negative } pub fn module(&self, db: &impl DefDatabase) -> Module { diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 41a51283d..6e07ab86e 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -22,7 +22,7 @@ use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; use rustc_hash::FxHashMap; use hir_def::{ - function::FunctionData, + data::FunctionData, path::known, resolver::{HasResolver, Resolver, TypeNs}, type_ref::{Mutability, TypeRef}, -- cgit v1.2.3