aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model.rs
diff options
context:
space:
mode:
authorSeivan Heidari <[email protected]>2019-11-21 00:11:41 +0000
committerSeivan Heidari <[email protected]>2019-11-21 00:11:41 +0000
commit358a1bcd708c622836723e5201b6de77cc9ff327 (patch)
treeaeff9c96a6059fa2b02e7c87ec88753bc7993d8d /crates/ra_hir/src/code_model.rs
parent1e2d090ab8a9bda18f148b894b7948eb05b976e6 (diff)
parent612a72fc4ea4376920f2a7da7b3c334227c1716c (diff)
Merge branch 'master' of https://github.com/rust-analyzer/rust-analyzer into feature/themes
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r--crates/ra_hir/src/code_model.rs278
1 files changed, 106 insertions, 172 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 731cc1fff..a132d128b 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -10,8 +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 traits::TraitData,
13 type_ref::{Mutability, TypeRef}, 14 type_ref::{Mutability, TypeRef},
14 CrateModuleId, ImplId, LocalEnumVariantId, LocalStructFieldId, ModuleId, UnionId, 15 AssocItemId, ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId,
16 LocalStructFieldId, Lookup, ModuleId, UnionId,
15}; 17};
16use hir_expand::{ 18use hir_expand::{
17 diagnostics::DiagnosticSink, 19 diagnostics::DiagnosticSink,
@@ -21,7 +23,6 @@ use ra_db::{CrateId, Edition};
21use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; 23use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
22 24
23use crate::{ 25use crate::{
24 adt::VariantDef,
25 db::{AstDatabase, DefDatabase, HirDatabase}, 26 db::{AstDatabase, DefDatabase, HirDatabase},
26 expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId}, 27 expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId},
27 generics::{GenericDef, HasGenericParams}, 28 generics::{GenericDef, HasGenericParams},
@@ -29,8 +30,7 @@ use crate::{
29 AstItemDef, ConstId, EnumId, FunctionId, MacroDefId, StaticId, StructId, TraitId, 30 AstItemDef, ConstId, EnumId, FunctionId, MacroDefId, StaticId, StructId, TraitId,
30 TypeAliasId, 31 TypeAliasId,
31 }, 32 },
32 resolve::{Resolver, Scope, TypeNs}, 33 resolve::{HasResolver, TypeNs},
33 traits::TraitData,
34 ty::{InferenceResult, Namespace, TraitRef}, 34 ty::{InferenceResult, Namespace, TraitRef},
35 Either, HasSource, ImportId, Name, ScopeDef, Source, Ty, 35 Either, HasSource, ImportId, Name, ScopeDef, Source, Ty,
36}; 36};
@@ -139,7 +139,7 @@ impl Module {
139 ) -> Either<ast::UseTree, ast::ExternCrateItem> { 139 ) -> Either<ast::UseTree, ast::ExternCrateItem> {
140 let src = self.definition_source(db); 140 let src = self.definition_source(db);
141 let (_, source_map) = db.raw_items_with_source_map(src.file_id); 141 let (_, source_map) = db.raw_items_with_source_map(src.file_id);
142 source_map.get(&src.ast, import) 142 source_map.get(&src.value, import)
143 } 143 }
144 144
145 /// Returns the crate this module is part of. 145 /// Returns the crate this module is part of.
@@ -206,7 +206,7 @@ impl Module {
206 crate::ModuleDef::Function(f) => f.diagnostics(db, sink), 206 crate::ModuleDef::Function(f) => f.diagnostics(db, sink),
207 crate::ModuleDef::Module(m) => { 207 crate::ModuleDef::Module(m) => {
208 // Only add diagnostics from inline modules 208 // Only add diagnostics from inline modules
209 if let ModuleSource::Module(_) = m.definition_source(db).ast { 209 if let ModuleSource::Module(_) = m.definition_source(db).value {
210 m.diagnostics(db, sink) 210 m.diagnostics(db, sink)
211 } 211 }
212 } 212 }
@@ -223,22 +223,9 @@ impl Module {
223 } 223 }
224 } 224 }
225 225
226 pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver {
227 let def_map = db.crate_def_map(self.id.krate);
228 Resolver::default().push_module_scope(def_map, self.id.module_id)
229 }
230
231 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> { 226 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> {
232 let def_map = db.crate_def_map(self.id.krate); 227 let def_map = db.crate_def_map(self.id.krate);
233 def_map[self.id.module_id] 228 def_map[self.id.module_id].scope.declarations().map(ModuleDef::from).collect()
234 .scope
235 .entries()
236 .filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None })
237 .flat_map(|per_ns| {
238 per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter())
239 })
240 .map(ModuleDef::from)
241 .collect()
242 } 229 }
243 230
244 pub fn impl_blocks(self, db: &impl DefDatabase) -> Vec<ImplBlock> { 231 pub fn impl_blocks(self, db: &impl DefDatabase) -> Vec<ImplBlock> {
@@ -323,15 +310,8 @@ impl Struct {
323 db.type_for_def(self.into(), Namespace::Values) 310 db.type_for_def(self.into(), Namespace::Values)
324 } 311 }
325 312
326 // FIXME move to a more general type 313 fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
327 /// Builds a resolver for type references inside this struct. 314 db.struct_data(self.id.into()).variant_data.clone()
328 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
329 // take the outer scope...
330 let r = self.module(db).resolver(db);
331 // ...and add generic params, if present
332 let p = self.generic_params(db);
333 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
334 r
335 } 315 }
336} 316}
337 317
@@ -345,24 +325,13 @@ impl Union {
345 db.struct_data(self.id.into()).name.clone() 325 db.struct_data(self.id.into()).name.clone()
346 } 326 }
347 327
348 pub fn module(self, db: &impl HirDatabase) -> Module { 328 pub fn module(self, db: &impl DefDatabase) -> Module {
349 Module { id: self.id.0.module(db) } 329 Module { id: self.id.0.module(db) }
350 } 330 }
351 331
352 pub fn ty(self, db: &impl HirDatabase) -> Ty { 332 pub fn ty(self, db: &impl HirDatabase) -> Ty {
353 db.type_for_def(self.into(), Namespace::Types) 333 db.type_for_def(self.into(), Namespace::Types)
354 } 334 }
355
356 // FIXME move to a more general type
357 /// Builds a resolver for type references inside this union.
358 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
359 // take the outer scope...
360 let r = self.module(db).resolver(db);
361 // ...and add generic params, if present
362 let p = self.generic_params(db);
363 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
364 r
365 }
366} 335}
367 336
368#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 337#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -402,17 +371,6 @@ impl Enum {
402 pub fn ty(self, db: &impl HirDatabase) -> Ty { 371 pub fn ty(self, db: &impl HirDatabase) -> Ty {
403 db.type_for_def(self.into(), Namespace::Types) 372 db.type_for_def(self.into(), Namespace::Types)
404 } 373 }
405
406 // FIXME: move to a more general type
407 /// Builds a resolver for type references inside this struct.
408 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
409 // take the outer scope...
410 let r = self.module(db).resolver(db);
411 // ...and add generic params, if present
412 let p = self.generic_params(db);
413 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
414 r.push_scope(Scope::AdtScope(self.into()))
415 }
416} 374}
417 375
418#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 376#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -474,22 +432,52 @@ impl Adt {
474 } 432 }
475 } 433 }
476 434
435 pub fn module(self, db: &impl DefDatabase) -> Module {
436 match self {
437 Adt::Struct(s) => s.module(db),
438 Adt::Union(s) => s.module(db),
439 Adt::Enum(e) => e.module(db),
440 }
441 }
442
477 pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> { 443 pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> {
478 Some( 444 Some(self.module(db).krate())
479 match self { 445 }
480 Adt::Struct(s) => s.module(db), 446}
481 Adt::Union(s) => s.module(db), 447
482 Adt::Enum(e) => e.module(db), 448#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
483 } 449pub enum VariantDef {
484 .krate(), 450 Struct(Struct),
485 ) 451 EnumVariant(EnumVariant),
452}
453impl_froms!(VariantDef: Struct, EnumVariant);
454
455impl VariantDef {
456 pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
457 match self {
458 VariantDef::Struct(it) => it.fields(db),
459 VariantDef::EnumVariant(it) => it.fields(db),
460 }
461 }
462
463 pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
464 match self {
465 VariantDef::Struct(it) => it.field(db, name),
466 VariantDef::EnumVariant(it) => it.field(db, name),
467 }
468 }
469
470 pub fn module(self, db: &impl HirDatabase) -> Module {
471 match self {
472 VariantDef::Struct(it) => it.module(db),
473 VariantDef::EnumVariant(it) => it.module(db),
474 }
486 } 475 }
487 476
488 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { 477 pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
489 match self { 478 match self {
490 Adt::Struct(it) => it.resolver(db), 479 VariantDef::Struct(it) => it.variant_data(db),
491 Adt::Union(it) => it.resolver(db), 480 VariantDef::EnumVariant(it) => it.variant_data(db),
492 Adt::Enum(it) => it.resolver(db),
493 } 481 }
494 } 482 }
495} 483}
@@ -505,15 +493,6 @@ pub enum DefWithBody {
505impl_froms!(DefWithBody: Function, Const, Static); 493impl_froms!(DefWithBody: Function, Const, Static);
506 494
507impl DefWithBody { 495impl DefWithBody {
508 /// Builds a resolver for code inside this item.
509 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
510 match self {
511 DefWithBody::Const(c) => c.resolver(db),
512 DefWithBody::Function(f) => f.resolver(db),
513 DefWithBody::Static(s) => s.resolver(db),
514 }
515 }
516
517 pub(crate) fn krate(self, db: &impl HirDatabase) -> Option<Crate> { 496 pub(crate) fn krate(self, db: &impl HirDatabase) -> Option<Crate> {
518 match self { 497 match self {
519 DefWithBody::Const(c) => c.krate(db), 498 DefWithBody::Const(c) => c.krate(db),
@@ -598,10 +577,10 @@ impl FnData {
598 func: Function, 577 func: Function,
599 ) -> Arc<FnData> { 578 ) -> Arc<FnData> {
600 let src = func.source(db); 579 let src = func.source(db);
601 let name = src.ast.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); 580 let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing);
602 let mut params = Vec::new(); 581 let mut params = Vec::new();
603 let mut has_self_param = false; 582 let mut has_self_param = false;
604 if let Some(param_list) = src.ast.param_list() { 583 if let Some(param_list) = src.value.param_list() {
605 if let Some(self_param) = param_list.self_param() { 584 if let Some(self_param) = param_list.self_param() {
606 let self_type = if let Some(type_ref) = self_param.ascribed_type() { 585 let self_type = if let Some(type_ref) = self_param.ascribed_type() {
607 TypeRef::from_ast(type_ref) 586 TypeRef::from_ast(type_ref)
@@ -625,7 +604,7 @@ impl FnData {
625 params.push(type_ref); 604 params.push(type_ref);
626 } 605 }
627 } 606 }
628 let ret_type = if let Some(type_ref) = src.ast.ret_type().and_then(|rt| rt.type_ref()) { 607 let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) {
629 TypeRef::from_ast(type_ref) 608 TypeRef::from_ast(type_ref)
630 } else { 609 } else {
631 TypeRef::unit() 610 TypeRef::unit()
@@ -655,7 +634,7 @@ impl FnData {
655 634
656impl Function { 635impl Function {
657 pub fn module(self, db: &impl DefDatabase) -> Module { 636 pub fn module(self, db: &impl DefDatabase) -> Module {
658 Module { id: self.id.module(db) } 637 self.id.lookup(db).module(db).into()
659 } 638 }
660 639
661 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 640 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
@@ -688,35 +667,28 @@ impl Function {
688 667
689 /// The containing impl block, if this is a method. 668 /// The containing impl block, if this is a method.
690 pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { 669 pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> {
691 ImplBlock::containing(db, self.into()) 670 match self.container(db) {
671 Some(Container::ImplBlock(it)) => Some(it),
672 _ => None,
673 }
692 } 674 }
693 675
694 /// The containing trait, if this is a trait method definition. 676 /// The containing trait, if this is a trait method definition.
695 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { 677 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> {
696 db.trait_items_index(self.module(db)).get_parent_trait(self.into()) 678 match self.container(db) {
679 Some(Container::Trait(it)) => Some(it),
680 _ => None,
681 }
697 } 682 }
698 683
699 pub fn container(self, db: &impl DefDatabase) -> Option<Container> { 684 pub fn container(self, db: &impl DefDatabase) -> Option<Container> {
700 if let Some(impl_block) = self.impl_block(db) { 685 match self.id.lookup(db).container {
701 Some(impl_block.into()) 686 ContainerId::TraitId(it) => Some(Container::Trait(it.into())),
702 } else if let Some(trait_) = self.parent_trait(db) { 687 ContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())),
703 Some(trait_.into()) 688 ContainerId::ModuleId(_) => None,
704 } else {
705 None
706 } 689 }
707 } 690 }
708 691
709 // FIXME: move to a more general type for 'body-having' items
710 /// Builds a resolver for code inside this item.
711 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
712 // take the outer scope...
713 let r = self.container(db).map_or_else(|| self.module(db).resolver(db), |c| c.resolver(db));
714 // ...and add generic params, if present
715 let p = self.generic_params(db);
716 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
717 r
718 }
719
720 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { 692 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) {
721 let infer = self.infer(db); 693 let infer = self.infer(db);
722 infer.add_diagnostics(db, self, sink); 694 infer.add_diagnostics(db, self, sink);
@@ -732,7 +704,7 @@ pub struct Const {
732 704
733impl Const { 705impl Const {
734 pub fn module(self, db: &impl DefDatabase) -> Module { 706 pub fn module(self, db: &impl DefDatabase) -> Module {
735 Module { id: self.id.module(db) } 707 Module { id: self.id.lookup(db).module(db) }
736 } 708 }
737 709
738 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 710 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
@@ -751,35 +723,29 @@ impl Const {
751 db.infer(self.into()) 723 db.infer(self.into())
752 } 724 }
753 725
754 /// The containing impl block, if this is a method. 726 /// The containing impl block, if this is a type alias.
755 pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { 727 pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> {
756 ImplBlock::containing(db, self.into()) 728 match self.container(db) {
729 Some(Container::ImplBlock(it)) => Some(it),
730 _ => None,
731 }
757 } 732 }
758 733
734 /// The containing trait, if this is a trait type alias definition.
759 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { 735 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> {
760 db.trait_items_index(self.module(db)).get_parent_trait(self.into()) 736 match self.container(db) {
737 Some(Container::Trait(it)) => Some(it),
738 _ => None,
739 }
761 } 740 }
762 741
763 pub fn container(self, db: &impl DefDatabase) -> Option<Container> { 742 pub fn container(self, db: &impl DefDatabase) -> Option<Container> {
764 if let Some(impl_block) = self.impl_block(db) { 743 match self.id.lookup(db).container {
765 Some(impl_block.into()) 744 ContainerId::TraitId(it) => Some(Container::Trait(it.into())),
766 } else if let Some(trait_) = self.parent_trait(db) { 745 ContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())),
767 Some(trait_.into()) 746 ContainerId::ModuleId(_) => None,
768 } else {
769 None
770 } 747 }
771 } 748 }
772
773 // FIXME: move to a more general type for 'body-having' items
774 /// Builds a resolver for code inside this item.
775 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
776 // take the outer scope...
777 let r = self
778 .impl_block(db)
779 .map(|ib| ib.resolver(db))
780 .unwrap_or_else(|| self.module(db).resolver(db));
781 r
782 }
783} 749}
784 750
785#[derive(Debug, Clone, PartialEq, Eq)] 751#[derive(Debug, Clone, PartialEq, Eq)]
@@ -801,7 +767,7 @@ impl ConstData {
801 db: &(impl DefDatabase + AstDatabase), 767 db: &(impl DefDatabase + AstDatabase),
802 konst: Const, 768 konst: Const,
803 ) -> Arc<ConstData> { 769 ) -> Arc<ConstData> {
804 let node = konst.source(db).ast; 770 let node = konst.source(db).value;
805 const_data_for(&node) 771 const_data_for(&node)
806 } 772 }
807 773
@@ -809,7 +775,7 @@ impl ConstData {
809 db: &(impl DefDatabase + AstDatabase), 775 db: &(impl DefDatabase + AstDatabase),
810 konst: Static, 776 konst: Static,
811 ) -> Arc<ConstData> { 777 ) -> Arc<ConstData> {
812 let node = konst.source(db).ast; 778 let node = konst.source(db).value;
813 const_data_for(&node) 779 const_data_for(&node)
814 } 780 }
815} 781}
@@ -839,12 +805,6 @@ impl Static {
839 db.static_data(self) 805 db.static_data(self)
840 } 806 }
841 807
842 /// Builds a resolver for code inside this item.
843 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
844 // take the outer scope...
845 self.module(db).resolver(db)
846 }
847
848 pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> { 808 pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
849 db.infer(self.into()) 809 db.infer(self.into())
850 } 810 }
@@ -861,11 +821,11 @@ impl Trait {
861 } 821 }
862 822
863 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 823 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
864 self.trait_data(db).name().clone() 824 self.trait_data(db).name.clone()
865 } 825 }
866 826
867 pub fn items(self, db: &impl DefDatabase) -> Vec<AssocItem> { 827 pub fn items(self, db: &impl DefDatabase) -> Vec<AssocItem> {
868 self.trait_data(db).items().to_vec() 828 self.trait_data(db).items.iter().map(|it| (*it).into()).collect()
869 } 829 }
870 830
871 fn direct_super_traits(self, db: &impl HirDatabase) -> Vec<Trait> { 831 fn direct_super_traits(self, db: &impl HirDatabase) -> Vec<Trait> {
@@ -912,10 +872,10 @@ impl Trait {
912 pub fn associated_type_by_name(self, db: &impl DefDatabase, name: &Name) -> Option<TypeAlias> { 872 pub fn associated_type_by_name(self, db: &impl DefDatabase, name: &Name) -> Option<TypeAlias> {
913 let trait_data = self.trait_data(db); 873 let trait_data = self.trait_data(db);
914 trait_data 874 trait_data
915 .items() 875 .items
916 .iter() 876 .iter()
917 .filter_map(|item| match item { 877 .filter_map(|item| match item {
918 AssocItem::TypeAlias(t) => Some(*t), 878 AssocItemId::TypeAliasId(t) => Some(TypeAlias::from(*t)),
919 _ => None, 879 _ => None,
920 }) 880 })
921 .find(|t| &t.name(db) == name) 881 .find(|t| &t.name(db) == name)
@@ -930,7 +890,7 @@ impl Trait {
930 } 890 }
931 891
932 pub(crate) fn trait_data(self, db: &impl DefDatabase) -> Arc<TraitData> { 892 pub(crate) fn trait_data(self, db: &impl DefDatabase) -> Arc<TraitData> {
933 db.trait_data(self) 893 db.trait_data(self.id)
934 } 894 }
935 895
936 pub fn trait_ref(self, db: &impl HirDatabase) -> TraitRef { 896 pub fn trait_ref(self, db: &impl HirDatabase) -> TraitRef {
@@ -938,15 +898,7 @@ impl Trait {
938 } 898 }
939 899
940 pub fn is_auto(self, db: &impl DefDatabase) -> bool { 900 pub fn is_auto(self, db: &impl DefDatabase) -> bool {
941 self.trait_data(db).is_auto() 901 self.trait_data(db).auto
942 }
943
944 pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver {
945 let r = self.module(db).resolver(db);
946 // add generic params, if present
947 let p = self.generic_params(db);
948 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
949 r
950 } 902 }
951} 903}
952 904
@@ -957,30 +909,34 @@ pub struct TypeAlias {
957 909
958impl TypeAlias { 910impl TypeAlias {
959 pub fn module(self, db: &impl DefDatabase) -> Module { 911 pub fn module(self, db: &impl DefDatabase) -> Module {
960 Module { id: self.id.module(db) } 912 Module { id: self.id.lookup(db).module(db) }
961 } 913 }
962 914
963 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 915 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
964 Some(self.module(db).krate()) 916 Some(self.module(db).krate())
965 } 917 }
966 918
967 /// The containing impl block, if this is a method. 919 /// The containing impl block, if this is a type alias.
968 pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { 920 pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> {
969 ImplBlock::containing(db, self.into()) 921 match self.container(db) {
922 Some(Container::ImplBlock(it)) => Some(it),
923 _ => None,
924 }
970 } 925 }
971 926
972 /// The containing trait, if this is a trait method definition. 927 /// The containing trait, if this is a trait type alias definition.
973 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { 928 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> {
974 db.trait_items_index(self.module(db)).get_parent_trait(self.into()) 929 match self.container(db) {
930 Some(Container::Trait(it)) => Some(it),
931 _ => None,
932 }
975 } 933 }
976 934
977 pub fn container(self, db: &impl DefDatabase) -> Option<Container> { 935 pub fn container(self, db: &impl DefDatabase) -> Option<Container> {
978 if let Some(impl_block) = self.impl_block(db) { 936 match self.id.lookup(db).container {
979 Some(impl_block.into()) 937 ContainerId::TraitId(it) => Some(Container::Trait(it.into())),
980 } else if let Some(trait_) = self.parent_trait(db) { 938 ContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())),
981 Some(trait_.into()) 939 ContainerId::ModuleId(_) => None,
982 } else {
983 None
984 } 940 }
985 } 941 }
986 942
@@ -995,19 +951,6 @@ impl TypeAlias {
995 pub fn name(self, db: &impl DefDatabase) -> Name { 951 pub fn name(self, db: &impl DefDatabase) -> Name {
996 db.type_alias_data(self).name.clone() 952 db.type_alias_data(self).name.clone()
997 } 953 }
998
999 /// Builds a resolver for the type references in this type alias.
1000 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
1001 // take the outer scope...
1002 let r = self
1003 .impl_block(db)
1004 .map(|ib| ib.resolver(db))
1005 .unwrap_or_else(|| self.module(db).resolver(db));
1006 // ...and add generic params, if present
1007 let p = self.generic_params(db);
1008 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
1009 r
1010 }
1011} 954}
1012 955
1013#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 956#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -1023,15 +966,6 @@ pub enum Container {
1023} 966}
1024impl_froms!(Container: Trait, ImplBlock); 967impl_froms!(Container: Trait, ImplBlock);
1025 968
1026impl Container {
1027 pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver {
1028 match self {
1029 Container::Trait(trait_) => trait_.resolver(db),
1030 Container::ImplBlock(impl_block) => impl_block.resolver(db),
1031 }
1032 }
1033}
1034
1035#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] 969#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1036pub enum AssocItem { 970pub enum AssocItem {
1037 Function(Function), 971 Function(Function),