aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/source_binder.rs8
-rw-r--r--crates/ra_hir/src/ty/infer.rs6
-rw-r--r--crates/ra_hir/src/ty/infer/coerce.rs2
-rw-r--r--crates/ra_hir/src/ty/infer/expr.rs5
-rw-r--r--crates/ra_hir/src/ty/infer/pat.rs10
-rw-r--r--crates/ra_hir/src/ty/infer/path.rs89
-rw-r--r--crates/ra_hir/src/ty/infer/unify.rs12
-rw-r--r--crates/ra_hir/src/ty/method_resolution.rs10
-rw-r--r--crates/ra_hir_def/src/lib.rs10
9 files changed, 90 insertions, 62 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 9f3e6c43f..b9d3a1713 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -11,7 +11,7 @@ use hir_def::{
11 expr::{ExprId, PatId}, 11 expr::{ExprId, PatId},
12 path::known, 12 path::known,
13 resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, 13 resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs},
14 DefWithBodyId, 14 AssocItemId, DefWithBodyId,
15}; 15};
16use hir_expand::{ 16use hir_expand::{
17 hygiene::Hygiene, name::AsName, AstId, HirFileId, MacroCallId, MacroFileKind, Source, 17 hygiene::Hygiene, name::AsName, AstId, HirFileId, MacroCallId, MacroFileKind, Source,
@@ -380,7 +380,7 @@ impl SourceAnalyzer {
380 name, 380 name,
381 method_resolution::LookupMode::MethodCall, 381 method_resolution::LookupMode::MethodCall,
382 |ty, it| match it { 382 |ty, it| match it {
383 AssocItem::Function(f) => callback(ty, f), 383 AssocItemId::FunctionId(f) => callback(ty, f.into()),
384 _ => None, 384 _ => None,
385 }, 385 },
386 ) 386 )
@@ -391,7 +391,7 @@ impl SourceAnalyzer {
391 db: &impl HirDatabase, 391 db: &impl HirDatabase,
392 ty: &Type, 392 ty: &Type,
393 name: Option<&Name>, 393 name: Option<&Name>,
394 callback: impl FnMut(&Ty, AssocItem) -> Option<T>, 394 mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>,
395 ) -> Option<T> { 395 ) -> Option<T> {
396 // There should be no inference vars in types passed here 396 // There should be no inference vars in types passed here
397 // FIXME check that? 397 // FIXME check that?
@@ -403,7 +403,7 @@ impl SourceAnalyzer {
403 &self.resolver, 403 &self.resolver,
404 name, 404 name,
405 method_resolution::LookupMode::Path, 405 method_resolution::LookupMode::Path,
406 callback, 406 |ty, it| callback(ty, it.into()),
407 ) 407 )
408 } 408 }
409 409
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs
index beb2efb7a..db9a8c9d1 100644
--- a/crates/ra_hir/src/ty/infer.rs
+++ b/crates/ra_hir/src/ty/infer.rs
@@ -26,7 +26,7 @@ use hir_def::{
26 path::known, 26 path::known,
27 resolver::{HasResolver, Resolver, TypeNs}, 27 resolver::{HasResolver, Resolver, TypeNs},
28 type_ref::{Mutability, TypeRef}, 28 type_ref::{Mutability, TypeRef},
29 AdtId, DefWithBodyId, 29 AdtId, AssocItemId, DefWithBodyId,
30}; 30};
31use hir_expand::{diagnostics::DiagnosticSink, name}; 31use hir_expand::{diagnostics::DiagnosticSink, name};
32use ra_arena::map::ArenaMap; 32use ra_arena::map::ArenaMap;
@@ -255,8 +255,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
255 self.result.variant_resolutions.insert(id, variant); 255 self.result.variant_resolutions.insert(id, variant);
256 } 256 }
257 257
258 fn write_assoc_resolution(&mut self, id: ExprOrPatId, item: AssocItem) { 258 fn write_assoc_resolution(&mut self, id: ExprOrPatId, item: AssocItemId) {
259 self.result.assoc_resolutions.insert(id, item); 259 self.result.assoc_resolutions.insert(id, item.into());
260 } 260 }
261 261
262 fn write_pat_ty(&mut self, pat: PatId, ty: Ty) { 262 fn write_pat_ty(&mut self, pat: PatId, ty: Ty) {
diff --git a/crates/ra_hir/src/ty/infer/coerce.rs b/crates/ra_hir/src/ty/infer/coerce.rs
index cf45ede7c..3fb5d8a83 100644
--- a/crates/ra_hir/src/ty/infer/coerce.rs
+++ b/crates/ra_hir/src/ty/infer/coerce.rs
@@ -7,6 +7,7 @@
7use hir_def::{ 7use hir_def::{
8 lang_item::LangItemTarget, 8 lang_item::LangItemTarget,
9 resolver::{HasResolver, Resolver}, 9 resolver::{HasResolver, Resolver},
10 type_ref::Mutability,
10 AdtId, 11 AdtId,
11}; 12};
12use rustc_hash::FxHashMap; 13use rustc_hash::FxHashMap;
@@ -15,7 +16,6 @@ use test_utils::tested_by;
15use crate::{ 16use crate::{
16 db::HirDatabase, 17 db::HirDatabase,
17 ty::{autoderef, Substs, TraitRef, Ty, TypeCtor, TypeWalk}, 18 ty::{autoderef, Substs, TraitRef, Ty, TypeCtor, TypeWalk},
18 Mutability,
19}; 19};
20 20
21use super::{InEnvironment, InferTy, InferenceContext, TypeVarValue}; 21use super::{InEnvironment, InferTy, InferenceContext, TypeVarValue};
diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs
index eb221d6bc..57f845dfa 100644
--- a/crates/ra_hir/src/ty/infer/expr.rs
+++ b/crates/ra_hir/src/ty/infer/expr.rs
@@ -5,22 +5,21 @@ use std::sync::Arc;
5 5
6use hir_def::{ 6use hir_def::{
7 builtin_type::Signedness, 7 builtin_type::Signedness,
8 expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
8 generics::GenericParams, 9 generics::GenericParams,
9 path::{GenericArg, GenericArgs}, 10 path::{GenericArg, GenericArgs},
10 resolver::resolver_for_expr, 11 resolver::resolver_for_expr,
11 AdtId, ContainerId, Lookup, StructFieldId, 12 AdtId, ContainerId, Lookup, StructFieldId,
12}; 13};
13use hir_expand::name; 14use hir_expand::name::{self, Name};
14 15
15use crate::{ 16use crate::{
16 db::HirDatabase, 17 db::HirDatabase,
17 expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
18 ty::{ 18 ty::{
19 autoderef, method_resolution, op, traits::InEnvironment, CallableDef, InferTy, IntTy, 19 autoderef, method_resolution, op, traits::InEnvironment, CallableDef, InferTy, IntTy,
20 Mutability, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, 20 Mutability, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor,
21 TypeWalk, Uncertain, 21 TypeWalk, Uncertain,
22 }, 22 },
23 Name,
24}; 23};
25 24
26use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; 25use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch};
diff --git a/crates/ra_hir/src/ty/infer/pat.rs b/crates/ra_hir/src/ty/infer/pat.rs
index 641d61e87..6dbf03eb2 100644
--- a/crates/ra_hir/src/ty/infer/pat.rs
+++ b/crates/ra_hir/src/ty/infer/pat.rs
@@ -3,14 +3,18 @@
3use std::iter::repeat; 3use std::iter::repeat;
4use std::sync::Arc; 4use std::sync::Arc;
5 5
6use hir_def::{
7 expr::{BindingAnnotation, Pat, PatId, RecordFieldPat},
8 path::Path,
9 type_ref::Mutability,
10};
11use hir_expand::name::Name;
6use test_utils::tested_by; 12use test_utils::tested_by;
7 13
8use super::{BindingMode, InferenceContext}; 14use super::{BindingMode, InferenceContext};
9use crate::{ 15use crate::{
10 db::HirDatabase, 16 db::HirDatabase,
11 expr::{BindingAnnotation, Pat, PatId, RecordFieldPat}, 17 ty::{Substs, Ty, TypeCtor, TypeWalk},
12 ty::{Mutability, Substs, Ty, TypeCtor, TypeWalk},
13 Name, Path,
14}; 18};
15 19
16impl<'a, D: HirDatabase> InferenceContext<'a, D> { 20impl<'a, D: HirDatabase> InferenceContext<'a, D> {
diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs
index be2067dd4..09ff79728 100644
--- a/crates/ra_hir/src/ty/infer/path.rs
+++ b/crates/ra_hir/src/ty/infer/path.rs
@@ -1,14 +1,15 @@
1//! Path expression resolution. 1//! Path expression resolution.
2 2
3use hir_def::{ 3use hir_def::{
4 path::PathSegment, 4 path::{Path, PathSegment},
5 resolver::{ResolveValueResult, Resolver, TypeNs, ValueNs}, 5 resolver::{HasResolver, ResolveValueResult, Resolver, TypeNs, ValueNs},
6 AssocItemId, ContainerId, Lookup,
6}; 7};
8use hir_expand::name::Name;
7 9
8use crate::{ 10use crate::{
9 db::HirDatabase, 11 db::HirDatabase,
10 ty::{method_resolution, Substs, Ty, TypeWalk, ValueTyDefId}, 12 ty::{method_resolution, Substs, Ty, TypeWalk, ValueTyDefId},
11 AssocItem, Container, Function, Name, Path,
12}; 13};
13 14
14use super::{ExprOrPatId, InferenceContext, TraitRef}; 15use super::{ExprOrPatId, InferenceContext, TraitRef};
@@ -142,31 +143,35 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
142 id: ExprOrPatId, 143 id: ExprOrPatId,
143 ) -> Option<(ValueNs, Option<Substs>)> { 144 ) -> Option<(ValueNs, Option<Substs>)> {
144 let trait_ = trait_ref.trait_; 145 let trait_ = trait_ref.trait_;
145 let item = 146 let item = self
146 self.db.trait_data(trait_).items.iter().map(|(_name, id)| (*id).into()).find_map( 147 .db
147 |item| match item { 148 .trait_data(trait_)
148 AssocItem::Function(func) => { 149 .items
149 if segment.name == func.name(self.db) { 150 .iter()
150 Some(AssocItem::Function(func)) 151 .map(|(_name, id)| (*id).into())
151 } else { 152 .find_map(|item| match item {
152 None 153 AssocItemId::FunctionId(func) => {
153 } 154 if segment.name == self.db.function_data(func).name {
155 Some(AssocItemId::FunctionId(func))
156 } else {
157 None
154 } 158 }
159 }
155 160
156 AssocItem::Const(konst) => { 161 AssocItemId::ConstId(konst) => {
157 if konst.name(self.db).map_or(false, |n| n == segment.name) { 162 if self.db.const_data(konst).name.as_ref().map_or(false, |n| n == &segment.name)
158 Some(AssocItem::Const(konst)) 163 {
159 } else { 164 Some(AssocItemId::ConstId(konst))
160 None 165 } else {
161 } 166 None
162 } 167 }
163 AssocItem::TypeAlias(_) => None, 168 }
164 }, 169 AssocItemId::TypeAliasId(_) => None,
165 )?; 170 })?;
166 let def = match item { 171 let def = match item {
167 AssocItem::Function(f) => ValueNs::FunctionId(f.id), 172 AssocItemId::FunctionId(f) => ValueNs::FunctionId(f),
168 AssocItem::Const(c) => ValueNs::ConstId(c.id), 173 AssocItemId::ConstId(c) => ValueNs::ConstId(c),
169 AssocItem::TypeAlias(_) => unreachable!(), 174 AssocItemId::TypeAliasId(_) => unreachable!(),
170 }; 175 };
171 let substs = Substs::build_for_def(self.db, item) 176 let substs = Substs::build_for_def(self.db, item)
172 .use_parent_substs(&trait_ref.substs) 177 .use_parent_substs(&trait_ref.substs)
@@ -196,16 +201,18 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
196 Some(name), 201 Some(name),
197 method_resolution::LookupMode::Path, 202 method_resolution::LookupMode::Path,
198 move |_ty, item| { 203 move |_ty, item| {
199 let def = match item { 204 let (def, container) = match item {
200 AssocItem::Function(f) => ValueNs::FunctionId(f.id), 205 AssocItemId::FunctionId(f) => {
201 AssocItem::Const(c) => ValueNs::ConstId(c.id), 206 (ValueNs::FunctionId(f), f.lookup(self.db).container)
202 AssocItem::TypeAlias(_) => unreachable!(), 207 }
208 AssocItemId::ConstId(c) => (ValueNs::ConstId(c), c.lookup(self.db).container),
209 AssocItemId::TypeAliasId(_) => unreachable!(),
203 }; 210 };
204 let substs = match item.container(self.db) { 211 let substs = match container {
205 Container::ImplBlock(_) => self.find_self_types(&def, ty.clone()), 212 ContainerId::ImplId(_) => self.find_self_types(&def, ty.clone()),
206 Container::Trait(t) => { 213 ContainerId::TraitId(trait_) => {
207 // we're picking this method 214 // we're picking this method
208 let trait_substs = Substs::build_for_def(self.db, t.id) 215 let trait_substs = Substs::build_for_def(self.db, trait_)
209 .push(ty.clone()) 216 .push(ty.clone())
210 .fill(std::iter::repeat_with(|| self.new_type_var())) 217 .fill(std::iter::repeat_with(|| self.new_type_var()))
211 .build(); 218 .build();
@@ -214,29 +221,35 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
214 .fill_with_params() 221 .fill_with_params()
215 .build(); 222 .build();
216 self.obligations.push(super::Obligation::Trait(TraitRef { 223 self.obligations.push(super::Obligation::Trait(TraitRef {
217 trait_: t.id, 224 trait_,
218 substs: trait_substs, 225 substs: trait_substs,
219 })); 226 }));
220 Some(substs) 227 Some(substs)
221 } 228 }
229 ContainerId::ModuleId(_) => None,
222 }; 230 };
223 231
224 self.write_assoc_resolution(id, item); 232 self.write_assoc_resolution(id, item.into());
225 Some((def, substs)) 233 Some((def, substs))
226 }, 234 },
227 ) 235 )
228 } 236 }
229 237
230 fn find_self_types(&self, def: &ValueNs, actual_def_ty: Ty) -> Option<Substs> { 238 fn find_self_types(&self, def: &ValueNs, actual_def_ty: Ty) -> Option<Substs> {
231 if let ValueNs::FunctionId(func) = def { 239 if let ValueNs::FunctionId(func) = *def {
232 let func = Function::from(*func);
233 // We only do the infer if parent has generic params 240 // We only do the infer if parent has generic params
234 let gen = self.db.generic_params(func.id.into()); 241 let gen = self.db.generic_params(func.into());
235 if gen.count_parent_params() == 0 { 242 if gen.count_parent_params() == 0 {
236 return None; 243 return None;
237 } 244 }
238 245
239 let impl_block = func.impl_block(self.db)?.target_ty(self.db); 246 let impl_id = match func.lookup(self.db).container {
247 ContainerId::ImplId(it) => it,
248 _ => return None,
249 };
250 let resolver = impl_id.resolver(self.db);
251 let impl_data = self.db.impl_data(impl_id);
252 let impl_block = Ty::from_hir(self.db, &resolver, &impl_data.target_type);
240 let impl_block_substs = impl_block.substs()?; 253 let impl_block_substs = impl_block.substs()?;
241 let actual_substs = actual_def_ty.substs()?; 254 let actual_substs = actual_def_ty.substs()?;
242 255
diff --git a/crates/ra_hir/src/ty/infer/unify.rs b/crates/ra_hir/src/ty/infer/unify.rs
index 64d9394cf..e27bb2f82 100644
--- a/crates/ra_hir/src/ty/infer/unify.rs
+++ b/crates/ra_hir/src/ty/infer/unify.rs
@@ -1,12 +1,14 @@
1//! Unification and canonicalization logic. 1//! Unification and canonicalization logic.
2 2
3use super::{InferenceContext, Obligation}; 3use super::{InferenceContext, Obligation};
4use crate::db::HirDatabase; 4use crate::{
5use crate::ty::{ 5 db::HirDatabase,
6 Canonical, InEnvironment, InferTy, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, 6 ty::{
7 TypeWalk, 7 Canonical, InEnvironment, InferTy, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty,
8 TypeWalk,
9 },
10 util::make_mut_slice,
8}; 11};
9use crate::util::make_mut_slice;
10 12
11impl<'a, D: HirDatabase> InferenceContext<'a, D> { 13impl<'a, D: HirDatabase> InferenceContext<'a, D> {
12 pub(super) fn canonicalizer<'b>(&'b mut self) -> Canonicalizer<'a, 'b, D> 14 pub(super) fn canonicalizer<'b>(&'b mut self) -> Canonicalizer<'a, 'b, D>
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs
index 7df2649c9..02e81fb34 100644
--- a/crates/ra_hir/src/ty/method_resolution.rs
+++ b/crates/ra_hir/src/ty/method_resolution.rs
@@ -18,7 +18,7 @@ use crate::{
18 db::HirDatabase, 18 db::HirDatabase,
19 ty::primitive::{FloatBitness, Uncertain}, 19 ty::primitive::{FloatBitness, Uncertain},
20 ty::{utils::all_super_traits, Ty, TypeCtor}, 20 ty::{utils::all_super_traits, Ty, TypeCtor},
21 AssocItem, Function, 21 Function,
22}; 22};
23 23
24use super::{autoderef, Canonical, InEnvironment, TraitEnvironment, TraitRef}; 24use super::{autoderef, Canonical, InEnvironment, TraitEnvironment, TraitRef};
@@ -157,7 +157,7 @@ pub(crate) fn lookup_method(
157) -> Option<(Ty, Function)> { 157) -> Option<(Ty, Function)> {
158 iterate_method_candidates(ty, db, resolver, Some(name), LookupMode::MethodCall, |ty, f| match f 158 iterate_method_candidates(ty, db, resolver, Some(name), LookupMode::MethodCall, |ty, f| match f
159 { 159 {
160 AssocItem::Function(f) => Some((ty.clone(), f)), 160 AssocItemId::FunctionId(f) => Some((ty.clone(), f.into())),
161 _ => None, 161 _ => None,
162 }) 162 })
163} 163}
@@ -183,7 +183,7 @@ pub(crate) fn iterate_method_candidates<T>(
183 resolver: &Resolver, 183 resolver: &Resolver,
184 name: Option<&Name>, 184 name: Option<&Name>,
185 mode: LookupMode, 185 mode: LookupMode,
186 mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>, 186 mut callback: impl FnMut(&Ty, AssocItemId) -> Option<T>,
187) -> Option<T> { 187) -> Option<T> {
188 let krate = resolver.krate()?; 188 let krate = resolver.krate()?;
189 match mode { 189 match mode {
@@ -239,7 +239,7 @@ fn iterate_trait_method_candidates<T>(
239 resolver: &Resolver, 239 resolver: &Resolver,
240 name: Option<&Name>, 240 name: Option<&Name>,
241 mode: LookupMode, 241 mode: LookupMode,
242 mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>, 242 mut callback: impl FnMut(&Ty, AssocItemId) -> Option<T>,
243) -> Option<T> { 243) -> Option<T> {
244 let krate = resolver.krate()?; 244 let krate = resolver.krate()?;
245 // FIXME: maybe put the trait_env behind a query (need to figure out good input parameters for that) 245 // FIXME: maybe put the trait_env behind a query (need to figure out good input parameters for that)
@@ -285,7 +285,7 @@ fn iterate_inherent_methods<T>(
285 name: Option<&Name>, 285 name: Option<&Name>,
286 mode: LookupMode, 286 mode: LookupMode,
287 krate: CrateId, 287 krate: CrateId,
288 mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>, 288 mut callback: impl FnMut(&Ty, AssocItemId) -> Option<T>,
289) -> Option<T> { 289) -> Option<T> {
290 for krate in ty.value.def_crates(db, krate)? { 290 for krate in ty.value.def_crates(db, krate)? {
291 let impls = db.impls_in_crate(krate); 291 let impls = db.impls_in_crate(krate);
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index 274dd1467..ea3f00bb3 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -398,6 +398,16 @@ impl_froms!(
398 ConstId 398 ConstId
399); 399);
400 400
401impl From<AssocItemId> for GenericDefId {
402 fn from(item: AssocItemId) -> Self {
403 match item {
404 AssocItemId::FunctionId(f) => f.into(),
405 AssocItemId::ConstId(c) => c.into(),
406 AssocItemId::TypeAliasId(t) => t.into(),
407 }
408 }
409}
410
401#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 411#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
402pub enum AttrDefId { 412pub enum AttrDefId {
403 ModuleId(ModuleId), 413 ModuleId(ModuleId),