aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r--crates/ra_hir/src/code_model.rs92
1 files changed, 17 insertions, 75 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 9cbea024a..7ac1bf461 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -7,7 +7,6 @@ use std::sync::Arc;
7use either::Either; 7use either::Either;
8use hir_def::{ 8use hir_def::{
9 adt::VariantData, 9 adt::VariantData,
10 body::{Body, BodySourceMap},
11 builtin_type::BuiltinType, 10 builtin_type::BuiltinType,
12 docs::Documentation, 11 docs::Documentation,
13 expr::{BindingAnnotation, Pat, PatId}, 12 expr::{BindingAnnotation, Pat, PatId},
@@ -24,14 +23,15 @@ use hir_expand::{
24 name::{self, AsName}, 23 name::{self, AsName},
25 MacroDefId, 24 MacroDefId,
26}; 25};
27use hir_ty::expr::ExprValidator; 26use hir_ty::{
27 autoderef, display::HirFormatter, expr::ExprValidator, ApplicationTy, Canonical, InEnvironment,
28 TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk,
29};
28use ra_db::{CrateId, Edition, FileId}; 30use ra_db::{CrateId, Edition, FileId};
29use ra_syntax::ast; 31use ra_syntax::ast;
30 32
31use crate::{ 33use crate::{
32 db::{DefDatabase, HirDatabase}, 34 db::{DefDatabase, HirDatabase},
33 ty::display::HirFormatter,
34 ty::{self, InEnvironment, InferenceResult, TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk},
35 CallableDef, HirDisplay, InFile, Name, 35 CallableDef, HirDisplay, InFile, Name,
36}; 36};
37 37
@@ -88,10 +88,6 @@ impl Crate {
88 pub fn all(db: &impl DefDatabase) -> Vec<Crate> { 88 pub fn all(db: &impl DefDatabase) -> Vec<Crate> {
89 db.crate_graph().iter().map(|id| Crate { id }).collect() 89 db.crate_graph().iter().map(|id| Crate { id }).collect()
90 } 90 }
91
92 pub fn crate_id(self) -> CrateId {
93 self.id
94 }
95} 91}
96 92
97#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 93#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -511,44 +507,8 @@ impl Function {
511 db.function_data(self.id).params.clone() 507 db.function_data(self.id).params.clone()
512 } 508 }
513 509
514 pub fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
515 db.body_with_source_map(self.id.into()).1
516 }
517
518 pub fn body(self, db: &impl HirDatabase) -> Arc<Body> {
519 db.body(self.id.into())
520 }
521
522 pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
523 db.infer(self.id.into())
524 }
525
526 /// The containing impl block, if this is a method.
527 pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> {
528 match self.container(db) {
529 Some(Container::ImplBlock(it)) => Some(it),
530 _ => None,
531 }
532 }
533
534 /// The containing trait, if this is a trait method definition.
535 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> {
536 match self.container(db) {
537 Some(Container::Trait(it)) => Some(it),
538 _ => None,
539 }
540 }
541
542 pub fn container(self, db: &impl DefDatabase) -> Option<Container> {
543 match self.id.lookup(db).container {
544 ContainerId::TraitId(it) => Some(Container::Trait(it.into())),
545 ContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())),
546 ContainerId::ModuleId(_) => None,
547 }
548 }
549
550 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { 510 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) {
551 let infer = self.infer(db); 511 let infer = db.infer(self.id.into());
552 infer.add_diagnostics(db, self.id, sink); 512 infer.add_diagnostics(db, self.id, sink);
553 let mut validator = ExprValidator::new(self.id, infer, sink); 513 let mut validator = ExprValidator::new(self.id, infer, sink);
554 validator.validate_body(db); 514 validator.validate_body(db);
@@ -573,10 +533,6 @@ impl Const {
573 db.const_data(self.id).name.clone() 533 db.const_data(self.id).name.clone()
574 } 534 }
575 535
576 pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
577 db.infer(self.id.into())
578 }
579
580 /// The containing impl block, if this is a type alias. 536 /// The containing impl block, if this is a type alias.
581 pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { 537 pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> {
582 match self.container(db) { 538 match self.container(db) {
@@ -615,10 +571,6 @@ impl Static {
615 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 571 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
616 Some(self.module(db).krate()) 572 Some(self.module(db).krate())
617 } 573 }
618
619 pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
620 db.infer(self.id.into())
621 }
622} 574}
623 575
624#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 576#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -733,15 +685,6 @@ impl AssocItem {
733 AssocItem::TypeAlias(t) => t.module(db), 685 AssocItem::TypeAlias(t) => t.module(db),
734 } 686 }
735 } 687 }
736
737 pub fn container(self, db: &impl DefDatabase) -> Container {
738 match self {
739 AssocItem::Function(f) => f.container(db),
740 AssocItem::Const(c) => c.container(db),
741 AssocItem::TypeAlias(t) => t.container(db),
742 }
743 .expect("AssocItem without container")
744 }
745} 688}
746 689
747#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] 690#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
@@ -958,7 +901,7 @@ impl Type {
958 pub fn fields(&self, db: &impl HirDatabase) -> Vec<(StructField, Type)> { 901 pub fn fields(&self, db: &impl HirDatabase) -> Vec<(StructField, Type)> {
959 if let Ty::Apply(a_ty) = &self.ty.value { 902 if let Ty::Apply(a_ty) = &self.ty.value {
960 match a_ty.ctor { 903 match a_ty.ctor {
961 ty::TypeCtor::Adt(AdtId::StructId(s)) => { 904 TypeCtor::Adt(AdtId::StructId(s)) => {
962 let var_def = s.into(); 905 let var_def = s.into();
963 return db 906 return db
964 .field_types(var_def) 907 .field_types(var_def)
@@ -980,7 +923,7 @@ impl Type {
980 let mut res = Vec::new(); 923 let mut res = Vec::new();
981 if let Ty::Apply(a_ty) = &self.ty.value { 924 if let Ty::Apply(a_ty) = &self.ty.value {
982 match a_ty.ctor { 925 match a_ty.ctor {
983 ty::TypeCtor::Tuple { .. } => { 926 TypeCtor::Tuple { .. } => {
984 for ty in a_ty.parameters.iter() { 927 for ty in a_ty.parameters.iter() {
985 let ty = ty.clone().subst(&a_ty.parameters); 928 let ty = ty.clone().subst(&a_ty.parameters);
986 res.push(self.derived(ty)); 929 res.push(self.derived(ty));
@@ -1016,10 +959,10 @@ impl Type {
1016 pub fn autoderef<'a>(&'a self, db: &'a impl HirDatabase) -> impl Iterator<Item = Type> + 'a { 959 pub fn autoderef<'a>(&'a self, db: &'a impl HirDatabase) -> impl Iterator<Item = Type> + 'a {
1017 // There should be no inference vars in types passed here 960 // There should be no inference vars in types passed here
1018 // FIXME check that? 961 // FIXME check that?
1019 let canonical = crate::ty::Canonical { value: self.ty.value.clone(), num_vars: 0 }; 962 let canonical = Canonical { value: self.ty.value.clone(), num_vars: 0 };
1020 let environment = self.ty.environment.clone(); 963 let environment = self.ty.environment.clone();
1021 let ty = InEnvironment { value: canonical, environment: environment.clone() }; 964 let ty = InEnvironment { value: canonical, environment: environment.clone() };
1022 ty::autoderef(db, Some(self.krate), ty) 965 autoderef(db, Some(self.krate), ty)
1023 .map(|canonical| canonical.value) 966 .map(|canonical| canonical.value)
1024 .map(move |ty| self.derived(ty)) 967 .map(move |ty| self.derived(ty))
1025 } 968 }
@@ -1059,15 +1002,14 @@ impl Type {
1059 // FIXME: provide required accessors such that it becomes implementable from outside. 1002 // FIXME: provide required accessors such that it becomes implementable from outside.
1060 pub fn is_equal_for_find_impls(&self, other: &Type) -> bool { 1003 pub fn is_equal_for_find_impls(&self, other: &Type) -> bool {
1061 match (&self.ty.value, &other.ty.value) { 1004 match (&self.ty.value, &other.ty.value) {
1062 (Ty::Apply(a_original_ty), Ty::Apply(ty::ApplicationTy { ctor, parameters })) => { 1005 (Ty::Apply(a_original_ty), Ty::Apply(ApplicationTy { ctor, parameters })) => match ctor
1063 match ctor { 1006 {
1064 TypeCtor::Ref(..) => match parameters.as_single() { 1007 TypeCtor::Ref(..) => match parameters.as_single() {
1065 Ty::Apply(a_ty) => a_original_ty.ctor == a_ty.ctor, 1008 Ty::Apply(a_ty) => a_original_ty.ctor == a_ty.ctor,
1066 _ => false, 1009 _ => false,
1067 }, 1010 },
1068 _ => a_original_ty.ctor == *ctor, 1011 _ => a_original_ty.ctor == *ctor,
1069 } 1012 },
1070 }
1071 _ => false, 1013 _ => false,
1072 } 1014 }
1073 } 1015 }