From 974518fde7975b839ed4ccd4c5ce1d48cd6db3c7 Mon Sep 17 00:00:00 2001 From: Zac Pullar-Strecker Date: Tue, 1 Sep 2020 20:26:10 +1200 Subject: Code reorganisation and field support --- crates/hir/src/code_model.rs | 48 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'crates/hir') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 031c91ccf..1dd6d73f3 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -35,7 +35,7 @@ use hir_ty::{ traits::SolutionVariables, ApplicationTy, BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate, InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, Ty, - TyDefId, TyKind, TypeCtor, + TyDefId, TyKind, TypeCtor, TyLoweringContext, TypeCtor, }; use rustc_hash::FxHashSet; use stdx::impl_from; @@ -186,6 +186,25 @@ impl_from!( for ModuleDef ); +impl From for ModuleDef { + fn from(mowner: MethodOwner) -> Self { + match mowner { + MethodOwner::Trait(t) => t.into(), + MethodOwner::Adt(t) => t.into(), + } + } +} + +impl From for ModuleDef { + fn from(var: VariantDef) -> Self { + match var { + VariantDef::Struct(t) => Adt::from(t).into(), + VariantDef::Union(t) => Adt::from(t).into(), + VariantDef::EnumVariant(t) => t.into(), + } + } +} + impl ModuleDef { pub fn module(self, db: &dyn HirDatabase) -> Option { match self { @@ -752,8 +771,35 @@ impl Function { pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { hir_ty::diagnostics::validate_body(db, self.id.into(), sink) } + + pub fn parent_def(self, db: &dyn HirDatabase) -> Option { + match self.as_assoc_item(db).map(|assoc| assoc.container(db)) { + Some(AssocItemContainer::Trait(t)) => Some(t.into()), + Some(AssocItemContainer::ImplDef(imp)) => { + let resolver = ModuleId::from(imp.module(db)).resolver(db.upcast()); + let ctx = TyLoweringContext::new(db, &resolver); + let adt = Ty::from_hir( + &ctx, + &imp.target_trait(db).unwrap_or_else(|| imp.target_type(db)), + ) + .as_adt() + .map(|t| t.0) + .unwrap(); + Some(Adt::from(adt).into()) + } + None => None, + } + } } +#[derive(Debug)] +pub enum MethodOwner { + Trait(Trait), + Adt(Adt), +} + +impl_from!(Trait, Adt for MethodOwner); + // Note: logically, this belongs to `hir_ty`, but we are not using it there yet. pub enum Access { Shared, -- cgit v1.2.3 From c648884397bfdb779c447fa31964dc1fce94bd95 Mon Sep 17 00:00:00 2001 From: Zac Pullar-Strecker Date: Thu, 3 Sep 2020 19:55:24 +1200 Subject: Differentiate method/tymethod by determining 'defaultness' Currently a method only has defaultness if it is a provided trait method, but this will change when specialisation is available and may need to become a concept known to hir. I opted to go for a 'fewest changes' approach given specialisation is still under development. --- crates/hir/src/code_model.rs | 9 ++++++++- crates/hir/src/lib.rs | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) (limited to 'crates/hir') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 1dd6d73f3..0b24f247c 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -772,7 +772,14 @@ impl Function { hir_ty::diagnostics::validate_body(db, self.id.into(), sink) } - pub fn parent_def(self, db: &dyn HirDatabase) -> Option { + /// Whether this function declaration has a definition. + /// + /// This is false in the case of required (not provided) trait methods. + pub fn has_body(self, db: &dyn HirDatabase) -> bool { + db.function_data(self.id).has_body + } + + pub fn method_owner(self, db: &dyn HirDatabase) -> Option { match self.as_assoc_item(db).map(|assoc| assoc.container(db)) { Some(AssocItemContainer::Trait(t)) => Some(t.into()), Some(AssocItemContainer::ImplDef(imp)) => { diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 4094a76cb..687abe6ca 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -35,8 +35,8 @@ pub use crate::{ code_model::{ Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const, Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, Function, - GenericDef, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, - Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, + GenericDef, HasVisibility, ImplDef, Local, MacroDef, MethodOwner, Module, ModuleDef, + ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, }, has_source::HasSource, semantics::{original_range, PathResolution, Semantics, SemanticsScope}, -- cgit v1.2.3 From 26086faab2dab6baeaa050f73a7f64b83ead6807 Mon Sep 17 00:00:00 2001 From: Zac Pullar-Strecker Date: Thu, 3 Sep 2020 20:09:36 +1200 Subject: Change Option::Some bug to a fixme note IMO this is too much work to be worth fixing at the moment. --- crates/hir/src/code_model.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'crates/hir') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 0b24f247c..231ec0004 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -779,6 +779,7 @@ impl Function { db.function_data(self.id).has_body } + /// If this function is a method, the trait or type where it is declared. pub fn method_owner(self, db: &dyn HirDatabase) -> Option { match self.as_assoc_item(db).map(|assoc| assoc.container(db)) { Some(AssocItemContainer::Trait(t)) => Some(t.into()), -- cgit v1.2.3 From 8af1dd733760c51abadda8f2bd20139e11ebba04 Mon Sep 17 00:00:00 2001 From: Zac Pullar-Strecker Date: Thu, 8 Oct 2020 15:22:57 +1300 Subject: Rebase fixes --- crates/hir/src/code_model.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/hir') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 231ec0004..9989b9b56 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -35,7 +35,7 @@ use hir_ty::{ traits::SolutionVariables, ApplicationTy, BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate, InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, Ty, - TyDefId, TyKind, TypeCtor, TyLoweringContext, TypeCtor, + TyDefId, TyKind, TypeCtor, TyLoweringContext, }; use rustc_hash::FxHashSet; use stdx::impl_from; -- cgit v1.2.3 From 3bd4fe96dce17eb2bff380389b24ea325bf54803 Mon Sep 17 00:00:00 2001 From: Zac Pullar-Strecker Date: Thu, 8 Oct 2020 15:44:52 +1300 Subject: Remove methodowner & fix formatting --- crates/hir/src/code_model.rs | 39 +-------------------------------------- crates/hir/src/lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 40 deletions(-) (limited to 'crates/hir') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 9989b9b56..650b4fa40 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -35,7 +35,7 @@ use hir_ty::{ traits::SolutionVariables, ApplicationTy, BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate, InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, Ty, - TyDefId, TyKind, TypeCtor, TyLoweringContext, + TyDefId, TyKind, TypeCtor, }; use rustc_hash::FxHashSet; use stdx::impl_from; @@ -186,15 +186,6 @@ impl_from!( for ModuleDef ); -impl From for ModuleDef { - fn from(mowner: MethodOwner) -> Self { - match mowner { - MethodOwner::Trait(t) => t.into(), - MethodOwner::Adt(t) => t.into(), - } - } -} - impl From for ModuleDef { fn from(var: VariantDef) -> Self { match var { @@ -778,36 +769,8 @@ impl Function { pub fn has_body(self, db: &dyn HirDatabase) -> bool { db.function_data(self.id).has_body } - - /// If this function is a method, the trait or type where it is declared. - pub fn method_owner(self, db: &dyn HirDatabase) -> Option { - match self.as_assoc_item(db).map(|assoc| assoc.container(db)) { - Some(AssocItemContainer::Trait(t)) => Some(t.into()), - Some(AssocItemContainer::ImplDef(imp)) => { - let resolver = ModuleId::from(imp.module(db)).resolver(db.upcast()); - let ctx = TyLoweringContext::new(db, &resolver); - let adt = Ty::from_hir( - &ctx, - &imp.target_trait(db).unwrap_or_else(|| imp.target_type(db)), - ) - .as_adt() - .map(|t| t.0) - .unwrap(); - Some(Adt::from(adt).into()) - } - None => None, - } - } } -#[derive(Debug)] -pub enum MethodOwner { - Trait(Trait), - Adt(Adt), -} - -impl_from!(Trait, Adt for MethodOwner); - // Note: logically, this belongs to `hir_ty`, but we are not using it there yet. pub enum Access { Shared, diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 687abe6ca..4094a76cb 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -35,8 +35,8 @@ pub use crate::{ code_model::{ Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const, Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, Function, - GenericDef, HasVisibility, ImplDef, Local, MacroDef, MethodOwner, Module, ModuleDef, - ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, + GenericDef, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, + Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, }, has_source::HasSource, semantics::{original_range, PathResolution, Semantics, SemanticsScope}, -- cgit v1.2.3