aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/lang_item.rs6
-rw-r--r--crates/ra_hir/src/resolve.rs10
-rw-r--r--crates/ra_hir/src/ty/infer.rs54
-rw-r--r--crates/ra_hir/src/ty/method_resolution.rs28
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs4
5 files changed, 46 insertions, 56 deletions
diff --git a/crates/ra_hir/src/lang_item.rs b/crates/ra_hir/src/lang_item.rs
index adcc682a2..a25d419e7 100644
--- a/crates/ra_hir/src/lang_item.rs
+++ b/crates/ra_hir/src/lang_item.rs
@@ -11,7 +11,7 @@ use crate::{
11pub enum LangItemTarget { 11pub enum LangItemTarget {
12 Enum(Enum), 12 Enum(Enum),
13 Function(Function), 13 Function(Function),
14 Impl(ImplBlock), 14 ImplBlock(ImplBlock),
15 Static(Static), 15 Static(Static),
16 Struct(Struct), 16 Struct(Struct),
17 Trait(Trait), 17 Trait(Trait),
@@ -22,7 +22,7 @@ impl LangItemTarget {
22 match self { 22 match self {
23 LangItemTarget::Enum(e) => e.module(db).krate(db), 23 LangItemTarget::Enum(e) => e.module(db).krate(db),
24 LangItemTarget::Function(f) => f.module(db).krate(db), 24 LangItemTarget::Function(f) => f.module(db).krate(db),
25 LangItemTarget::Impl(i) => i.module().krate(db), 25 LangItemTarget::ImplBlock(i) => i.module().krate(db),
26 LangItemTarget::Static(s) => s.module(db).krate(db), 26 LangItemTarget::Static(s) => s.module(db).krate(db),
27 LangItemTarget::Struct(s) => s.module(db).krate(db), 27 LangItemTarget::Struct(s) => s.module(db).krate(db),
28 LangItemTarget::Trait(t) => t.module(db).krate(db), 28 LangItemTarget::Trait(t) => t.module(db).krate(db),
@@ -65,7 +65,7 @@ impl LangItems {
65 .nth(0); 65 .nth(0);
66 if let Some(lang_item_name) = lang_item_name { 66 if let Some(lang_item_name) = lang_item_name {
67 let imp = ImplBlock::from_id(*module, impl_id); 67 let imp = ImplBlock::from_id(*module, impl_id);
68 self.items.entry(lang_item_name).or_insert(LangItemTarget::Impl(imp)); 68 self.items.entry(lang_item_name).or_insert(LangItemTarget::ImplBlock(imp));
69 } 69 }
70 } 70 }
71 71
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs
index 61925e832..f2c85eb66 100644
--- a/crates/ra_hir/src/resolve.rs
+++ b/crates/ra_hir/src/resolve.rs
@@ -5,13 +5,15 @@ use rustc_hash::FxHashMap;
5 5
6use crate::{ 6use crate::{
7 ModuleDef, 7 ModuleDef,
8 code_model_api::Crate,
8 db::HirDatabase, 9 db::HirDatabase,
9 name::{Name, KnownName}, 10 name::{Name, KnownName},
10 nameres::{PerNs, CrateDefMap, CrateModuleId}, 11 nameres::{PerNs, CrateDefMap, CrateModuleId},
11 generics::GenericParams, 12 generics::GenericParams,
12 expr::{scope::{ExprScopes, ScopeId}, PatId}, 13 expr::{scope::{ExprScopes, ScopeId}, PatId},
13 impl_block::ImplBlock, 14 impl_block::ImplBlock,
14 path::Path, Trait 15 path::Path,
16 Trait
15}; 17};
16 18
17#[derive(Debug, Clone, Default)] 19#[derive(Debug, Clone, Default)]
@@ -190,13 +192,17 @@ impl Resolver {
190 .flatten() 192 .flatten()
191 } 193 }
192 194
193 pub(crate) fn module(&self) -> Option<(&CrateDefMap, CrateModuleId)> { 195 fn module(&self) -> Option<(&CrateDefMap, CrateModuleId)> {
194 self.scopes.iter().rev().find_map(|scope| match scope { 196 self.scopes.iter().rev().find_map(|scope| match scope {
195 Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)), 197 Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)),
196 198
197 _ => None, 199 _ => None,
198 }) 200 })
199 } 201 }
202
203 pub(crate) fn krate(&self) -> Option<Crate> {
204 self.module().map(|t| t.0.krate())
205 }
200} 206}
201 207
202impl Resolver { 208impl Resolver {
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs
index 2275ac151..c7772a7f6 100644
--- a/crates/ra_hir/src/ty/infer.rs
+++ b/crates/ra_hir/src/ty/infer.rs
@@ -462,7 +462,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
462 let remaining_index = remaining_index.unwrap_or(path.segments.len()); 462 let remaining_index = remaining_index.unwrap_or(path.segments.len());
463 let mut actual_def_ty: Option<Ty> = None; 463 let mut actual_def_ty: Option<Ty> = None;
464 464
465 let krate = resolver.module().map(|t| t.0.krate()); 465 let krate = resolver.krate()?;
466 // resolve intermediate segments 466 // resolve intermediate segments
467 for (i, segment) in path.segments[remaining_index..].iter().enumerate() { 467 for (i, segment) in path.segments[remaining_index..].iter().enumerate() {
468 let ty = match resolved { 468 let ty = match resolved {
@@ -504,38 +504,36 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
504 504
505 actual_def_ty = Some(ty.clone()); 505 actual_def_ty = Some(ty.clone());
506 506
507 let item: crate::ModuleDef = krate.and_then(|k| { 507 let item: crate::ModuleDef = ty.iterate_impl_items(self.db, krate, |item| {
508 ty.iterate_impl_items(self.db, k, |item| { 508 let matching_def: Option<crate::ModuleDef> = match item {
509 let matching_def: Option<crate::ModuleDef> = match item { 509 crate::ImplItem::Method(func) => {
510 crate::ImplItem::Method(func) => { 510 let sig = func.signature(self.db);
511 let sig = func.signature(self.db); 511 if segment.name == *sig.name() {
512 if segment.name == *sig.name() { 512 Some(func.into())
513 Some(func.into()) 513 } else {
514 } else { 514 None
515 None
516 }
517 } 515 }
516 }
518 517
519 crate::ImplItem::Const(konst) => { 518 crate::ImplItem::Const(konst) => {
520 let sig = konst.signature(self.db); 519 let sig = konst.signature(self.db);
521 if segment.name == *sig.name() { 520 if segment.name == *sig.name() {
522 Some(konst.into()) 521 Some(konst.into())
523 } else { 522 } else {
524 None 523 None
525 }
526 } 524 }
525 }
527 526
528 // FIXME: Resolve associated types 527 // FIXME: Resolve associated types
529 crate::ImplItem::TypeAlias(_) => None, 528 crate::ImplItem::TypeAlias(_) => None,
530 }; 529 };
531 match matching_def { 530 match matching_def {
532 Some(_) => { 531 Some(_) => {
533 self.write_assoc_resolution(id, item); 532 self.write_assoc_resolution(id, item);
534 return matching_def; 533 return matching_def;
535 }
536 None => None,
537 } 534 }
538 }) 535 None => None,
536 }
539 })?; 537 })?;
540 538
541 resolved = Resolution::Def(item.into()); 539 resolved = Resolution::Def(item.into());
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs
index ba516313c..dca56a434 100644
--- a/crates/ra_hir/src/ty/method_resolution.rs
+++ b/crates/ra_hir/src/ty/method_resolution.rs
@@ -112,33 +112,19 @@ impl CrateImplBlocks {
112 } 112 }
113} 113}
114 114
115/// Rudimentary check whether an impl exists for a given type and trait; this 115fn def_crate(db: &impl HirDatabase, cur_crate: Crate, ty: &Ty) -> Option<Crate> {
116/// will actually be done by chalk.
117pub(crate) fn implements(db: &impl HirDatabase, trait_ref: TraitRef) -> bool {
118 // FIXME use all trait impls in the whole crate graph
119 let krate = trait_ref.trait_.module(db).krate(db);
120 let krate = match krate {
121 Some(krate) => krate,
122 None => return false,
123 };
124 let crate_impl_blocks = db.impls_in_crate(krate);
125 let mut impl_blocks = crate_impl_blocks.lookup_impl_blocks_for_trait(&trait_ref.trait_);
126 impl_blocks.any(|impl_block| &impl_block.target_ty(db) == trait_ref.self_ty())
127}
128
129fn def_crate(db: &impl HirDatabase, cur_krate: Crate, ty: &Ty) -> Option<Crate> {
130 match ty { 116 match ty {
131 Ty::Apply(a_ty) => match a_ty.ctor { 117 Ty::Apply(a_ty) => match a_ty.ctor {
132 TypeCtor::Adt(def_id) => def_id.krate(db), 118 TypeCtor::Adt(def_id) => def_id.krate(db),
133 TypeCtor::Bool => lang_item_lookup(db, cur_krate, "bool")?.krate(db), 119 TypeCtor::Bool => lang_item_lookup(db, cur_crate, "bool")?.krate(db),
134 TypeCtor::Char => lang_item_lookup(db, cur_krate, "char")?.krate(db), 120 TypeCtor::Char => lang_item_lookup(db, cur_crate, "char")?.krate(db),
135 TypeCtor::Float(UncertainFloatTy::Known(f)) => { 121 TypeCtor::Float(UncertainFloatTy::Known(f)) => {
136 lang_item_lookup(db, cur_krate, f.ty_to_string())?.krate(db) 122 lang_item_lookup(db, cur_crate, f.ty_to_string())?.krate(db)
137 } 123 }
138 TypeCtor::Int(UncertainIntTy::Known(i)) => { 124 TypeCtor::Int(UncertainIntTy::Known(i)) => {
139 lang_item_lookup(db, cur_krate, i.ty_to_string())?.krate(db) 125 lang_item_lookup(db, cur_crate, i.ty_to_string())?.krate(db)
140 } 126 }
141 TypeCtor::Str => lang_item_lookup(db, cur_krate, "str")?.krate(db), 127 TypeCtor::Str => lang_item_lookup(db, cur_crate, "str")?.krate(db),
142 _ => None, 128 _ => None,
143 }, 129 },
144 _ => None, 130 _ => None,
@@ -175,7 +161,7 @@ impl Ty {
175 // find in the end takes &self, we still do the autoderef step (just as 161 // find in the end takes &self, we still do the autoderef step (just as
176 // rustc does an autoderef and then autoref again). 162 // rustc does an autoderef and then autoref again).
177 163
178 let krate = resolver.module().map(|t| t.0.krate())?; 164 let krate = resolver.krate()?;
179 for derefed_ty in self.autoderef(db) { 165 for derefed_ty in self.autoderef(db) {
180 if let Some(result) = 166 if let Some(result) =
181 derefed_ty.iterate_inherent_methods(db, name, krate, &mut callback) 167 derefed_ty.iterate_inherent_methods(db, name, krate, &mut callback)
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs
index c49147b9e..c41752ae7 100644
--- a/crates/ra_ide_api/src/completion/complete_path.rs
+++ b/crates/ra_ide_api/src/completion/complete_path.rs
@@ -39,7 +39,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
39 hir::ModuleDef::Struct(s) => { 39 hir::ModuleDef::Struct(s) => {
40 let ty = s.ty(ctx.db); 40 let ty = s.ty(ctx.db);
41 let krate = ctx.module.and_then(|m| m.krate(ctx.db)); 41 let krate = ctx.module.and_then(|m| m.krate(ctx.db));
42 krate.map_or((), |krate| { 42 if let Some(krate) = krate {
43 ty.iterate_impl_items(ctx.db, krate, |item| { 43 ty.iterate_impl_items(ctx.db, krate, |item| {
44 match item { 44 match item {
45 hir::ImplItem::Method(func) => { 45 hir::ImplItem::Method(func) => {
@@ -53,7 +53,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
53 } 53 }
54 None::<()> 54 None::<()>
55 }); 55 });
56 }); 56 }
57 } 57 }
58 _ => return, 58 _ => return,
59 }; 59 };