diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-11-22 14:43:32 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-11-22 14:43:32 +0000 |
commit | b841c53a0c93cdca3f08b1c917c9fa8a63d31604 (patch) | |
tree | 2b1d06598328b9d1922e83c791f181b5789eb789 /crates/ra_hir/src | |
parent | a132cc715ed05cab83fd058156eb152f0be96982 (diff) | |
parent | 64df759418a83fd05fab850b8351fcddbdd3ef63 (diff) |
Merge #2356
2356: Move FunctionData to hir_def r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 89 | ||||
-rw-r--r-- | crates/ra_hir/src/db.rs | 13 | ||||
-rw-r--r-- | crates/ra_hir/src/impl_block.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 13 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/lower.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/method_resolution.rs | 6 |
7 files changed, 36 insertions, 105 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 72c9b466f..3daf7488e 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -10,10 +10,10 @@ use hir_def::{ | |||
10 | adt::VariantData, | 10 | adt::VariantData, |
11 | body::scope::ExprScopes, | 11 | body::scope::ExprScopes, |
12 | builtin_type::BuiltinType, | 12 | builtin_type::BuiltinType, |
13 | data::TraitData, | ||
13 | nameres::per_ns::PerNs, | 14 | nameres::per_ns::PerNs, |
14 | resolver::{HasResolver, TypeNs}, | 15 | resolver::{HasResolver, TypeNs}, |
15 | traits::TraitData, | 16 | type_ref::TypeRef, |
16 | type_ref::{Mutability, TypeRef}, | ||
17 | ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup, | 17 | ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup, |
18 | ModuleId, UnionId, | 18 | ModuleId, UnionId, |
19 | }; | 19 | }; |
@@ -561,77 +561,6 @@ pub struct Function { | |||
561 | pub(crate) id: FunctionId, | 561 | pub(crate) id: FunctionId, |
562 | } | 562 | } |
563 | 563 | ||
564 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
565 | pub struct FnData { | ||
566 | pub(crate) name: Name, | ||
567 | pub(crate) params: Vec<TypeRef>, | ||
568 | pub(crate) ret_type: TypeRef, | ||
569 | /// True if the first param is `self`. This is relevant to decide whether this | ||
570 | /// can be called as a method. | ||
571 | pub(crate) has_self_param: bool, | ||
572 | } | ||
573 | |||
574 | impl FnData { | ||
575 | pub(crate) fn fn_data_query( | ||
576 | db: &(impl DefDatabase + AstDatabase), | ||
577 | func: Function, | ||
578 | ) -> Arc<FnData> { | ||
579 | let src = func.source(db); | ||
580 | let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); | ||
581 | let mut params = Vec::new(); | ||
582 | let mut has_self_param = false; | ||
583 | if let Some(param_list) = src.value.param_list() { | ||
584 | if let Some(self_param) = param_list.self_param() { | ||
585 | let self_type = if let Some(type_ref) = self_param.ascribed_type() { | ||
586 | TypeRef::from_ast(type_ref) | ||
587 | } else { | ||
588 | let self_type = TypeRef::Path(name::SELF_TYPE.into()); | ||
589 | match self_param.kind() { | ||
590 | ast::SelfParamKind::Owned => self_type, | ||
591 | ast::SelfParamKind::Ref => { | ||
592 | TypeRef::Reference(Box::new(self_type), Mutability::Shared) | ||
593 | } | ||
594 | ast::SelfParamKind::MutRef => { | ||
595 | TypeRef::Reference(Box::new(self_type), Mutability::Mut) | ||
596 | } | ||
597 | } | ||
598 | }; | ||
599 | params.push(self_type); | ||
600 | has_self_param = true; | ||
601 | } | ||
602 | for param in param_list.params() { | ||
603 | let type_ref = TypeRef::from_ast_opt(param.ascribed_type()); | ||
604 | params.push(type_ref); | ||
605 | } | ||
606 | } | ||
607 | let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) { | ||
608 | TypeRef::from_ast(type_ref) | ||
609 | } else { | ||
610 | TypeRef::unit() | ||
611 | }; | ||
612 | |||
613 | let sig = FnData { name, params, ret_type, has_self_param }; | ||
614 | Arc::new(sig) | ||
615 | } | ||
616 | pub fn name(&self) -> &Name { | ||
617 | &self.name | ||
618 | } | ||
619 | |||
620 | pub fn params(&self) -> &[TypeRef] { | ||
621 | &self.params | ||
622 | } | ||
623 | |||
624 | pub fn ret_type(&self) -> &TypeRef { | ||
625 | &self.ret_type | ||
626 | } | ||
627 | |||
628 | /// True if the first arg is `self`. This is relevant to decide whether this | ||
629 | /// can be called as a method. | ||
630 | pub fn has_self_param(&self) -> bool { | ||
631 | self.has_self_param | ||
632 | } | ||
633 | } | ||
634 | |||
635 | impl Function { | 564 | impl Function { |
636 | pub fn module(self, db: &impl DefDatabase) -> Module { | 565 | pub fn module(self, db: &impl DefDatabase) -> Module { |
637 | self.id.lookup(db).module(db).into() | 566 | self.id.lookup(db).module(db).into() |
@@ -642,7 +571,15 @@ impl Function { | |||
642 | } | 571 | } |
643 | 572 | ||
644 | pub fn name(self, db: &impl HirDatabase) -> Name { | 573 | pub fn name(self, db: &impl HirDatabase) -> Name { |
645 | self.data(db).name.clone() | 574 | db.function_data(self.id).name.clone() |
575 | } | ||
576 | |||
577 | pub fn has_self_param(self, db: &impl HirDatabase) -> bool { | ||
578 | db.function_data(self.id).has_self_param | ||
579 | } | ||
580 | |||
581 | pub fn params(self, db: &impl HirDatabase) -> Vec<TypeRef> { | ||
582 | db.function_data(self.id).params.clone() | ||
646 | } | 583 | } |
647 | 584 | ||
648 | pub(crate) fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> { | 585 | pub(crate) fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> { |
@@ -657,10 +594,6 @@ impl Function { | |||
657 | db.type_for_def(self.into(), Namespace::Values) | 594 | db.type_for_def(self.into(), Namespace::Values) |
658 | } | 595 | } |
659 | 596 | ||
660 | pub fn data(self, db: &impl HirDatabase) -> Arc<FnData> { | ||
661 | db.fn_data(self) | ||
662 | } | ||
663 | |||
664 | pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> { | 597 | pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> { |
665 | db.infer(self.into()) | 598 | db.infer(self.into()) |
666 | } | 599 | } |
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::{ | |||
16 | CallableDef, FnSig, GenericPredicate, InferenceResult, Namespace, Substs, Ty, TypableDef, | 16 | CallableDef, FnSig, GenericPredicate, InferenceResult, Namespace, Substs, Ty, TypableDef, |
17 | TypeCtor, | 17 | TypeCtor, |
18 | }, | 18 | }, |
19 | Const, ConstData, Crate, DefWithBody, FnData, Function, GenericDef, ImplBlock, Module, Static, | 19 | Const, ConstData, Crate, DefWithBody, GenericDef, ImplBlock, Module, Static, StructField, |
20 | StructField, Trait, | 20 | Trait, |
21 | }; | 21 | }; |
22 | 22 | ||
23 | pub use hir_def::db::{ | 23 | pub use hir_def::db::{ |
24 | BodyQuery, BodyWithSourceMapQuery, CrateDefMapQuery, DefDatabase2, DefDatabase2Storage, | 24 | BodyQuery, BodyWithSourceMapQuery, CrateDefMapQuery, DefDatabase2, DefDatabase2Storage, |
25 | EnumDataQuery, ExprScopesQuery, GenericParamsQuery, ImplDataQuery, InternDatabase, | 25 | EnumDataQuery, ExprScopesQuery, FunctionDataQuery, GenericParamsQuery, ImplDataQuery, |
26 | InternDatabaseStorage, RawItemsQuery, RawItemsWithSourceMapQuery, StructDataQuery, | 26 | InternDatabase, InternDatabaseStorage, RawItemsQuery, RawItemsWithSourceMapQuery, |
27 | TraitDataQuery, TypeAliasDataQuery, | 27 | StructDataQuery, TraitDataQuery, TypeAliasDataQuery, |
28 | }; | 28 | }; |
29 | pub use hir_expand::db::{ | 29 | pub use hir_expand::db::{ |
30 | AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, | 30 | AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, |
@@ -35,9 +35,6 @@ pub use hir_expand::db::{ | |||
35 | #[salsa::query_group(DefDatabaseStorage)] | 35 | #[salsa::query_group(DefDatabaseStorage)] |
36 | #[salsa::requires(AstDatabase)] | 36 | #[salsa::requires(AstDatabase)] |
37 | pub trait DefDatabase: HirDebugDatabase + DefDatabase2 { | 37 | pub trait DefDatabase: HirDebugDatabase + DefDatabase2 { |
38 | #[salsa::invoke(FnData::fn_data_query)] | ||
39 | fn fn_data(&self, func: Function) -> Arc<FnData>; | ||
40 | |||
41 | #[salsa::invoke(ConstData::const_data_query)] | 38 | #[salsa::invoke(ConstData::const_data_query)] |
42 | fn const_data(&self, konst: Const) -> Arc<ConstData>; | 39 | fn const_data(&self, konst: Const) -> Arc<ConstData>; |
43 | 40 | ||
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 { | |||
18 | 18 | ||
19 | impl ImplBlock { | 19 | impl ImplBlock { |
20 | pub fn target_trait(&self, db: &impl DefDatabase) -> Option<TypeRef> { | 20 | pub fn target_trait(&self, db: &impl DefDatabase) -> Option<TypeRef> { |
21 | db.impl_data(self.id).target_trait().cloned() | 21 | db.impl_data(self.id).target_trait.clone() |
22 | } | 22 | } |
23 | 23 | ||
24 | pub fn target_type(&self, db: &impl DefDatabase) -> TypeRef { | 24 | pub fn target_type(&self, db: &impl DefDatabase) -> TypeRef { |
25 | db.impl_data(self.id).target_type().clone() | 25 | db.impl_data(self.id).target_type.clone() |
26 | } | 26 | } |
27 | 27 | ||
28 | pub fn target_ty(&self, db: &impl HirDatabase) -> Ty { | 28 | pub fn target_ty(&self, db: &impl HirDatabase) -> Ty { |
@@ -35,11 +35,11 @@ impl ImplBlock { | |||
35 | } | 35 | } |
36 | 36 | ||
37 | pub fn items(&self, db: &impl DefDatabase) -> Vec<AssocItem> { | 37 | pub fn items(&self, db: &impl DefDatabase) -> Vec<AssocItem> { |
38 | db.impl_data(self.id).items().iter().map(|it| (*it).into()).collect() | 38 | db.impl_data(self.id).items.iter().map(|it| (*it).into()).collect() |
39 | } | 39 | } |
40 | 40 | ||
41 | pub fn is_negative(&self, db: &impl DefDatabase) -> bool { | 41 | pub fn is_negative(&self, db: &impl DefDatabase) -> bool { |
42 | db.impl_data(self.id).is_negative() | 42 | db.impl_data(self.id).is_negative |
43 | } | 43 | } |
44 | 44 | ||
45 | pub fn module(&self, db: &impl DefDatabase) -> Module { | 45 | pub fn module(&self, db: &impl DefDatabase) -> Module { |
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::{ | |||
55 | docs::{DocDef, Docs, Documentation}, | 55 | docs::{DocDef, Docs, Documentation}, |
56 | src::{HasBodySource, HasSource}, | 56 | src::{HasBodySource, HasSource}, |
57 | Adt, AssocItem, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum, | 57 | Adt, AssocItem, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum, |
58 | EnumVariant, FieldSource, FnData, Function, GenericDef, GenericParam, HasBody, ImplBlock, | 58 | EnumVariant, FieldSource, Function, GenericDef, GenericParam, HasBody, ImplBlock, Local, |
59 | Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, | 59 | MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, Trait, |
60 | Trait, TypeAlias, Union, VariantDef, | 60 | TypeAlias, Union, VariantDef, |
61 | }, | 61 | }, |
62 | expr::ExprScopes, | 62 | expr::ExprScopes, |
63 | from_source::FromSource, | 63 | from_source::FromSource, |
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 69b13baef..6e07ab86e 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}; | |||
22 | use rustc_hash::FxHashMap; | 22 | use rustc_hash::FxHashMap; |
23 | 23 | ||
24 | use hir_def::{ | 24 | use hir_def::{ |
25 | data::FunctionData, | ||
25 | path::known, | 26 | path::known, |
26 | resolver::{HasResolver, Resolver, TypeNs}, | 27 | resolver::{HasResolver, Resolver, TypeNs}, |
27 | type_ref::{Mutability, TypeRef}, | 28 | type_ref::{Mutability, TypeRef}, |
@@ -43,8 +44,8 @@ use crate::{ | |||
43 | db::HirDatabase, | 44 | db::HirDatabase, |
44 | expr::{BindingAnnotation, Body, ExprId, PatId}, | 45 | expr::{BindingAnnotation, Body, ExprId, PatId}, |
45 | ty::infer::diagnostics::InferenceDiagnostic, | 46 | ty::infer::diagnostics::InferenceDiagnostic, |
46 | Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path, | 47 | Adt, AssocItem, ConstData, DefWithBody, FloatTy, Function, HasBody, IntTy, Path, StructField, |
47 | StructField, Trait, VariantDef, | 48 | Trait, VariantDef, |
48 | }; | 49 | }; |
49 | 50 | ||
50 | macro_rules! ty_app { | 51 | macro_rules! ty_app { |
@@ -70,7 +71,7 @@ pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResu | |||
70 | 71 | ||
71 | match def { | 72 | match def { |
72 | DefWithBody::Const(ref c) => ctx.collect_const(&c.data(db)), | 73 | DefWithBody::Const(ref c) => ctx.collect_const(&c.data(db)), |
73 | DefWithBody::Function(ref f) => ctx.collect_fn(&f.data(db)), | 74 | DefWithBody::Function(ref f) => ctx.collect_fn(&db.function_data(f.id)), |
74 | DefWithBody::Static(ref s) => ctx.collect_const(&s.data(db)), | 75 | DefWithBody::Static(ref s) => ctx.collect_const(&s.data(db)), |
75 | } | 76 | } |
76 | 77 | ||
@@ -562,14 +563,14 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
562 | self.return_ty = self.make_ty(data.type_ref()); | 563 | self.return_ty = self.make_ty(data.type_ref()); |
563 | } | 564 | } |
564 | 565 | ||
565 | fn collect_fn(&mut self, data: &FnData) { | 566 | fn collect_fn(&mut self, data: &FunctionData) { |
566 | let body = Arc::clone(&self.body); // avoid borrow checker problem | 567 | let body = Arc::clone(&self.body); // avoid borrow checker problem |
567 | for (type_ref, pat) in data.params().iter().zip(body.params()) { | 568 | for (type_ref, pat) in data.params.iter().zip(body.params()) { |
568 | let ty = self.make_ty(type_ref); | 569 | let ty = self.make_ty(type_ref); |
569 | 570 | ||
570 | self.infer_pat(*pat, &ty, BindingMode::default()); | 571 | self.infer_pat(*pat, &ty, BindingMode::default()); |
571 | } | 572 | } |
572 | self.return_ty = self.make_ty(data.ret_type()); | 573 | self.return_ty = self.make_ty(&data.ret_type); |
573 | } | 574 | } |
574 | 575 | ||
575 | fn infer_body(&mut self) { | 576 | 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) -> | |||
622 | } | 622 | } |
623 | 623 | ||
624 | fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { | 624 | fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { |
625 | let data = def.data(db); | 625 | let data = db.function_data(def.id); |
626 | let resolver = def.id.resolver(db); | 626 | let resolver = def.id.resolver(db); |
627 | let params = data.params().iter().map(|tr| Ty::from_hir(db, &resolver, tr)).collect::<Vec<_>>(); | 627 | let params = data.params.iter().map(|tr| Ty::from_hir(db, &resolver, tr)).collect::<Vec<_>>(); |
628 | let ret = Ty::from_hir(db, &resolver, data.ret_type()); | 628 | let ret = Ty::from_hir(db, &resolver, &data.ret_type); |
629 | FnSig::from_params_and_return(params, ret) | 629 | FnSig::from_params_and_return(params, ret) |
630 | } | 630 | } |
631 | 631 | ||
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( | |||
291 | ) -> bool { | 291 | ) -> bool { |
292 | match item { | 292 | match item { |
293 | AssocItem::Function(m) => { | 293 | AssocItem::Function(m) => { |
294 | let data = m.data(db); | 294 | let data = db.function_data(m.id); |
295 | name.map_or(true, |name| data.name() == name) | 295 | name.map_or(true, |name| data.name == *name) |
296 | && (data.has_self_param() || mode == LookupMode::Path) | 296 | && (data.has_self_param || mode == LookupMode::Path) |
297 | } | 297 | } |
298 | AssocItem::Const(c) => { | 298 | AssocItem::Const(c) => { |
299 | name.map_or(true, |name| Some(name) == c.name(db).as_ref()) | 299 | name.map_or(true, |name| Some(name) == c.name(db).as_ref()) |