diff options
Diffstat (limited to 'crates/ra_hir_ty/src')
-rw-r--r-- | crates/ra_hir_ty/src/autoderef.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/infer.rs | 29 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/infer/coerce.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/infer/expr.rs | 35 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/infer/path.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/lib.rs | 10 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/lower.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/method_resolution.rs | 14 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/test_db.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/tests.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/tests/coercion.rs | 86 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/tests/macros.rs | 42 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/tests/simple.rs | 55 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/tests/traits.rs | 42 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk.rs | 12 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/utils.rs | 8 |
16 files changed, 308 insertions, 49 deletions
diff --git a/crates/ra_hir_ty/src/autoderef.rs b/crates/ra_hir_ty/src/autoderef.rs index ee48fa537..f32d5786a 100644 --- a/crates/ra_hir_ty/src/autoderef.rs +++ b/crates/ra_hir_ty/src/autoderef.rs | |||
@@ -48,7 +48,7 @@ fn deref_by_trait( | |||
48 | krate: CrateId, | 48 | krate: CrateId, |
49 | ty: InEnvironment<&Canonical<Ty>>, | 49 | ty: InEnvironment<&Canonical<Ty>>, |
50 | ) -> Option<Canonical<Ty>> { | 50 | ) -> Option<Canonical<Ty>> { |
51 | let deref_trait = match db.lang_item(krate.into(), "deref".into())? { | 51 | let deref_trait = match db.lang_item(krate, "deref".into())? { |
52 | LangItemTarget::TraitId(it) => it, | 52 | LangItemTarget::TraitId(it) => it, |
53 | _ => return None, | 53 | _ => return None, |
54 | }; | 54 | }; |
@@ -78,7 +78,7 @@ fn deref_by_trait( | |||
78 | 78 | ||
79 | let canonical = super::Canonical { num_vars: 1 + ty.value.num_vars, value: in_env }; | 79 | let canonical = super::Canonical { num_vars: 1 + ty.value.num_vars, value: in_env }; |
80 | 80 | ||
81 | let solution = db.trait_solve(krate.into(), canonical)?; | 81 | let solution = db.trait_solve(krate, canonical)?; |
82 | 82 | ||
83 | match &solution { | 83 | match &solution { |
84 | Solution::Unique(vars) => { | 84 | Solution::Unique(vars) => { |
diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs index 98ba05fc2..e97b81473 100644 --- a/crates/ra_hir_ty/src/infer.rs +++ b/crates/ra_hir_ty/src/infer.rs | |||
@@ -196,7 +196,12 @@ struct InferenceContext<'a, D: HirDatabase> { | |||
196 | trait_env: Arc<TraitEnvironment>, | 196 | trait_env: Arc<TraitEnvironment>, |
197 | obligations: Vec<Obligation>, | 197 | obligations: Vec<Obligation>, |
198 | result: InferenceResult, | 198 | result: InferenceResult, |
199 | /// The return type of the function being inferred. | 199 | /// The return type of the function being inferred, or the closure if we're |
200 | /// currently within one. | ||
201 | /// | ||
202 | /// We might consider using a nested inference context for checking | ||
203 | /// closures, but currently this is the only field that will change there, | ||
204 | /// so it doesn't make sense. | ||
200 | return_ty: Ty, | 205 | return_ty: Ty, |
201 | 206 | ||
202 | /// Impls of `CoerceUnsized` used in coercion. | 207 | /// Impls of `CoerceUnsized` used in coercion. |
@@ -363,14 +368,26 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
363 | } | 368 | } |
364 | 369 | ||
365 | fn resolve_associated_type(&mut self, inner_ty: Ty, assoc_ty: Option<TypeAliasId>) -> Ty { | 370 | fn resolve_associated_type(&mut self, inner_ty: Ty, assoc_ty: Option<TypeAliasId>) -> Ty { |
371 | self.resolve_associated_type_with_params(inner_ty, assoc_ty, &[]) | ||
372 | } | ||
373 | |||
374 | fn resolve_associated_type_with_params( | ||
375 | &mut self, | ||
376 | inner_ty: Ty, | ||
377 | assoc_ty: Option<TypeAliasId>, | ||
378 | params: &[Ty], | ||
379 | ) -> Ty { | ||
366 | match assoc_ty { | 380 | match assoc_ty { |
367 | Some(res_assoc_ty) => { | 381 | Some(res_assoc_ty) => { |
368 | let ty = self.table.new_type_var(); | 382 | let ty = self.table.new_type_var(); |
383 | let builder = Substs::build_for_def(self.db, res_assoc_ty) | ||
384 | .push(inner_ty) | ||
385 | .fill(params.iter().cloned()); | ||
369 | let projection = ProjectionPredicate { | 386 | let projection = ProjectionPredicate { |
370 | ty: ty.clone(), | 387 | ty: ty.clone(), |
371 | projection_ty: ProjectionTy { | 388 | projection_ty: ProjectionTy { |
372 | associated_ty: res_assoc_ty, | 389 | associated_ty: res_assoc_ty, |
373 | parameters: Substs::single(inner_ty), | 390 | parameters: builder.build(), |
374 | }, | 391 | }, |
375 | }; | 392 | }; |
376 | self.obligations.push(Obligation::Projection(projection)); | 393 | self.obligations.push(Obligation::Projection(projection)); |
@@ -443,7 +460,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
443 | } | 460 | } |
444 | 461 | ||
445 | fn infer_body(&mut self) { | 462 | fn infer_body(&mut self) { |
446 | self.infer_expr(self.body.body_expr, &Expectation::has_type(self.return_ty.clone())); | 463 | self.infer_expr_coerce(self.body.body_expr, &Expectation::has_type(self.return_ty.clone())); |
447 | } | 464 | } |
448 | 465 | ||
449 | fn resolve_into_iter_item(&self) -> Option<TypeAliasId> { | 466 | fn resolve_into_iter_item(&self) -> Option<TypeAliasId> { |
@@ -517,6 +534,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
517 | let struct_ = self.resolver.resolve_known_struct(self.db, &path)?; | 534 | let struct_ = self.resolver.resolve_known_struct(self.db, &path)?; |
518 | Some(struct_.into()) | 535 | Some(struct_.into()) |
519 | } | 536 | } |
537 | |||
538 | fn resolve_ops_index_output(&self) -> Option<TypeAliasId> { | ||
539 | let path = path![std::ops::Index]; | ||
540 | let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; | ||
541 | self.db.trait_data(trait_).associated_type_by_name(&name![Output]) | ||
542 | } | ||
520 | } | 543 | } |
521 | 544 | ||
522 | /// The kinds of placeholders we need during type inference. There's separate | 545 | /// The kinds of placeholders we need during type inference. There's separate |
diff --git a/crates/ra_hir_ty/src/infer/coerce.rs b/crates/ra_hir_ty/src/infer/coerce.rs index 0f4dac45e..83c0c2c3f 100644 --- a/crates/ra_hir_ty/src/infer/coerce.rs +++ b/crates/ra_hir_ty/src/infer/coerce.rs | |||
@@ -134,6 +134,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
134 | } | 134 | } |
135 | } | 135 | } |
136 | 136 | ||
137 | (ty_app!(TypeCtor::Closure { .. }, params), ty_app!(TypeCtor::FnPtr { .. })) => { | ||
138 | from_ty = params[0].clone(); | ||
139 | } | ||
140 | |||
137 | _ => {} | 141 | _ => {} |
138 | } | 142 | } |
139 | 143 | ||
diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs index 924ad3e81..3af05394c 100644 --- a/crates/ra_hir_ty/src/infer/expr.rs +++ b/crates/ra_hir_ty/src/infer/expr.rs | |||
@@ -8,7 +8,7 @@ use hir_def::{ | |||
8 | expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, | 8 | expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, |
9 | path::{GenericArg, GenericArgs}, | 9 | path::{GenericArg, GenericArgs}, |
10 | resolver::resolver_for_expr, | 10 | resolver::resolver_for_expr, |
11 | AdtId, ContainerId, Lookup, StructFieldId, | 11 | AdtId, AssocContainerId, Lookup, StructFieldId, |
12 | }; | 12 | }; |
13 | use hir_expand::name::{name, Name}; | 13 | use hir_expand::name::{name, Name}; |
14 | use ra_syntax::ast::RangeOp; | 14 | use ra_syntax::ast::RangeOp; |
@@ -41,7 +41,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
41 | 41 | ||
42 | /// Infer type of expression with possibly implicit coerce to the expected type. | 42 | /// Infer type of expression with possibly implicit coerce to the expected type. |
43 | /// Return the type after possible coercion. | 43 | /// Return the type after possible coercion. |
44 | fn infer_expr_coerce(&mut self, expr: ExprId, expected: &Expectation) -> Ty { | 44 | pub(super) fn infer_expr_coerce(&mut self, expr: ExprId, expected: &Expectation) -> Ty { |
45 | let ty = self.infer_expr_inner(expr, &expected); | 45 | let ty = self.infer_expr_inner(expr, &expected); |
46 | let ty = if !self.coerce(&ty, &expected.ty) { | 46 | let ty = if !self.coerce(&ty, &expected.ty) { |
47 | self.result | 47 | self.result |
@@ -102,7 +102,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
102 | self.infer_expr(*body, &Expectation::has_type(Ty::unit())); | 102 | self.infer_expr(*body, &Expectation::has_type(Ty::unit())); |
103 | Ty::unit() | 103 | Ty::unit() |
104 | } | 104 | } |
105 | Expr::Lambda { body, args, arg_types } => { | 105 | Expr::Lambda { body, args, ret_type, arg_types } => { |
106 | assert_eq!(args.len(), arg_types.len()); | 106 | assert_eq!(args.len(), arg_types.len()); |
107 | 107 | ||
108 | let mut sig_tys = Vec::new(); | 108 | let mut sig_tys = Vec::new(); |
@@ -118,7 +118,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
118 | } | 118 | } |
119 | 119 | ||
120 | // add return type | 120 | // add return type |
121 | let ret_ty = self.table.new_type_var(); | 121 | let ret_ty = match ret_type { |
122 | Some(type_ref) => self.make_ty(type_ref), | ||
123 | None => self.table.new_type_var(), | ||
124 | }; | ||
122 | sig_tys.push(ret_ty.clone()); | 125 | sig_tys.push(ret_ty.clone()); |
123 | let sig_ty = Ty::apply( | 126 | let sig_ty = Ty::apply( |
124 | TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1 }, | 127 | TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1 }, |
@@ -134,7 +137,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
134 | // infer the body. | 137 | // infer the body. |
135 | self.coerce(&closure_ty, &expected.ty); | 138 | self.coerce(&closure_ty, &expected.ty); |
136 | 139 | ||
137 | self.infer_expr(*body, &Expectation::has_type(ret_ty)); | 140 | let prev_ret_ty = std::mem::replace(&mut self.return_ty, ret_ty.clone()); |
141 | |||
142 | self.infer_expr_coerce(*body, &Expectation::has_type(ret_ty)); | ||
143 | |||
144 | self.return_ty = prev_ret_ty; | ||
145 | |||
138 | closure_ty | 146 | closure_ty |
139 | } | 147 | } |
140 | Expr::Call { callee, args } => { | 148 | Expr::Call { callee, args } => { |
@@ -192,6 +200,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
192 | Expr::Return { expr } => { | 200 | Expr::Return { expr } => { |
193 | if let Some(expr) = expr { | 201 | if let Some(expr) = expr { |
194 | self.infer_expr_coerce(*expr, &Expectation::has_type(self.return_ty.clone())); | 202 | self.infer_expr_coerce(*expr, &Expectation::has_type(self.return_ty.clone())); |
203 | } else { | ||
204 | let unit = Ty::unit(); | ||
205 | self.coerce(&unit, &self.return_ty.clone()); | ||
195 | } | 206 | } |
196 | Ty::simple(TypeCtor::Never) | 207 | Ty::simple(TypeCtor::Never) |
197 | } | 208 | } |
@@ -422,10 +433,14 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
422 | } | 433 | } |
423 | } | 434 | } |
424 | Expr::Index { base, index } => { | 435 | Expr::Index { base, index } => { |
425 | let _base_ty = self.infer_expr_inner(*base, &Expectation::none()); | 436 | let base_ty = self.infer_expr_inner(*base, &Expectation::none()); |
426 | let _index_ty = self.infer_expr(*index, &Expectation::none()); | 437 | let index_ty = self.infer_expr(*index, &Expectation::none()); |
427 | // FIXME: use `std::ops::Index::Output` to figure out the real return type | 438 | |
428 | Ty::Unknown | 439 | self.resolve_associated_type_with_params( |
440 | base_ty, | ||
441 | self.resolve_ops_index_output(), | ||
442 | &[index_ty], | ||
443 | ) | ||
429 | } | 444 | } |
430 | Expr::Tuple { exprs } => { | 445 | Expr::Tuple { exprs } => { |
431 | let mut tys = match &expected.ty { | 446 | let mut tys = match &expected.ty { |
@@ -672,7 +687,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
672 | // add obligation for trait implementation, if this is a trait method | 687 | // add obligation for trait implementation, if this is a trait method |
673 | match def { | 688 | match def { |
674 | CallableDef::FunctionId(f) => { | 689 | CallableDef::FunctionId(f) => { |
675 | if let ContainerId::TraitId(trait_) = f.lookup(self.db).container { | 690 | if let AssocContainerId::TraitId(trait_) = f.lookup(self.db).container { |
676 | // construct a TraitDef | 691 | // construct a TraitDef |
677 | let substs = | 692 | let substs = |
678 | a_ty.parameters.prefix(generics(self.db, trait_.into()).len()); | 693 | a_ty.parameters.prefix(generics(self.db, trait_.into()).len()); |
diff --git a/crates/ra_hir_ty/src/infer/path.rs b/crates/ra_hir_ty/src/infer/path.rs index 402a89386..ffd358367 100644 --- a/crates/ra_hir_ty/src/infer/path.rs +++ b/crates/ra_hir_ty/src/infer/path.rs | |||
@@ -5,7 +5,7 @@ use std::iter; | |||
5 | use hir_def::{ | 5 | use hir_def::{ |
6 | path::{Path, PathSegment}, | 6 | path::{Path, PathSegment}, |
7 | resolver::{ResolveValueResult, Resolver, TypeNs, ValueNs}, | 7 | resolver::{ResolveValueResult, Resolver, TypeNs, ValueNs}, |
8 | AssocItemId, ContainerId, Lookup, | 8 | AssocContainerId, AssocItemId, Lookup, |
9 | }; | 9 | }; |
10 | use hir_expand::name::Name; | 10 | use hir_expand::name::Name; |
11 | 11 | ||
@@ -209,7 +209,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
209 | AssocItemId::TypeAliasId(_) => unreachable!(), | 209 | AssocItemId::TypeAliasId(_) => unreachable!(), |
210 | }; | 210 | }; |
211 | let substs = match container { | 211 | let substs = match container { |
212 | ContainerId::ImplId(impl_id) => { | 212 | AssocContainerId::ImplId(impl_id) => { |
213 | let impl_substs = Substs::build_for_def(self.db, impl_id) | 213 | let impl_substs = Substs::build_for_def(self.db, impl_id) |
214 | .fill(iter::repeat_with(|| self.table.new_type_var())) | 214 | .fill(iter::repeat_with(|| self.table.new_type_var())) |
215 | .build(); | 215 | .build(); |
@@ -221,7 +221,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
221 | self.unify(&impl_self_ty, &ty); | 221 | self.unify(&impl_self_ty, &ty); |
222 | Some(substs) | 222 | Some(substs) |
223 | } | 223 | } |
224 | ContainerId::TraitId(trait_) => { | 224 | AssocContainerId::TraitId(trait_) => { |
225 | // we're picking this method | 225 | // we're picking this method |
226 | let trait_substs = Substs::build_for_def(self.db, trait_) | 226 | let trait_substs = Substs::build_for_def(self.db, trait_) |
227 | .push(ty.clone()) | 227 | .push(ty.clone()) |
@@ -237,7 +237,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
237 | })); | 237 | })); |
238 | Some(substs) | 238 | Some(substs) |
239 | } | 239 | } |
240 | ContainerId::ModuleId(_) => None, | 240 | AssocContainerId::ContainerId(_) => None, |
241 | }; | 241 | }; |
242 | 242 | ||
243 | self.write_assoc_resolution(id, item.into()); | 243 | self.write_assoc_resolution(id, item.into()); |
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index 7ca9e6b8a..48abf97c9 100644 --- a/crates/ra_hir_ty/src/lib.rs +++ b/crates/ra_hir_ty/src/lib.rs | |||
@@ -44,8 +44,8 @@ use std::sync::Arc; | |||
44 | use std::{fmt, iter, mem}; | 44 | use std::{fmt, iter, mem}; |
45 | 45 | ||
46 | use hir_def::{ | 46 | use hir_def::{ |
47 | expr::ExprId, type_ref::Mutability, AdtId, ContainerId, DefWithBodyId, GenericDefId, HasModule, | 47 | expr::ExprId, type_ref::Mutability, AdtId, AssocContainerId, DefWithBodyId, GenericDefId, |
48 | Lookup, TraitId, TypeAliasId, | 48 | HasModule, Lookup, TraitId, TypeAliasId, |
49 | }; | 49 | }; |
50 | use hir_expand::name::Name; | 50 | use hir_expand::name::Name; |
51 | use ra_db::{impl_intern_key, salsa, CrateId}; | 51 | use ra_db::{impl_intern_key, salsa, CrateId}; |
@@ -251,7 +251,7 @@ impl ProjectionTy { | |||
251 | 251 | ||
252 | fn trait_(&self, db: &impl HirDatabase) -> TraitId { | 252 | fn trait_(&self, db: &impl HirDatabase) -> TraitId { |
253 | match self.associated_ty.lookup(db).container { | 253 | match self.associated_ty.lookup(db).container { |
254 | ContainerId::TraitId(it) => it, | 254 | AssocContainerId::TraitId(it) => it, |
255 | _ => panic!("projection ty without parent trait"), | 255 | _ => panic!("projection ty without parent trait"), |
256 | } | 256 | } |
257 | } | 257 | } |
@@ -919,7 +919,7 @@ impl HirDisplay for ApplicationTy { | |||
919 | { | 919 | { |
920 | Option::None => self.parameters.0.as_ref(), | 920 | Option::None => self.parameters.0.as_ref(), |
921 | Option::Some(default_parameters) => { | 921 | Option::Some(default_parameters) => { |
922 | for (i, parameter) in self.parameters.into_iter().enumerate() { | 922 | for (i, parameter) in self.parameters.iter().enumerate() { |
923 | match (parameter, default_parameters.get(i)) { | 923 | match (parameter, default_parameters.get(i)) { |
924 | (&Ty::Unknown, _) | (_, None) => { | 924 | (&Ty::Unknown, _) | (_, None) => { |
925 | non_default_parameters.push(parameter.clone()) | 925 | non_default_parameters.push(parameter.clone()) |
@@ -943,7 +943,7 @@ impl HirDisplay for ApplicationTy { | |||
943 | } | 943 | } |
944 | TypeCtor::AssociatedType(type_alias) => { | 944 | TypeCtor::AssociatedType(type_alias) => { |
945 | let trait_ = match type_alias.lookup(f.db).container { | 945 | let trait_ = match type_alias.lookup(f.db).container { |
946 | ContainerId::TraitId(it) => it, | 946 | AssocContainerId::TraitId(it) => it, |
947 | _ => panic!("not an associated type"), | 947 | _ => panic!("not an associated type"), |
948 | }; | 948 | }; |
949 | let trait_name = f.db.trait_data(trait_).name.clone(); | 949 | let trait_name = f.db.trait_data(trait_).name.clone(); |
diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs index 2b84309d7..af3db2e1d 100644 --- a/crates/ra_hir_ty/src/lower.rs +++ b/crates/ra_hir_ty/src/lower.rs | |||
@@ -697,8 +697,8 @@ impl CallableDef { | |||
697 | pub fn krate(self, db: &impl HirDatabase) -> CrateId { | 697 | pub fn krate(self, db: &impl HirDatabase) -> CrateId { |
698 | match self { | 698 | match self { |
699 | CallableDef::FunctionId(f) => f.lookup(db).module(db), | 699 | CallableDef::FunctionId(f) => f.lookup(db).module(db), |
700 | CallableDef::StructId(s) => s.lookup(db).container, | 700 | CallableDef::StructId(s) => s.lookup(db).container.module(db), |
701 | CallableDef::EnumVariantId(e) => e.parent.lookup(db).container, | 701 | CallableDef::EnumVariantId(e) => e.parent.lookup(db).container.module(db), |
702 | } | 702 | } |
703 | .krate | 703 | .krate |
704 | } | 704 | } |
diff --git a/crates/ra_hir_ty/src/method_resolution.rs b/crates/ra_hir_ty/src/method_resolution.rs index 848e306e9..888dc3116 100644 --- a/crates/ra_hir_ty/src/method_resolution.rs +++ b/crates/ra_hir_ty/src/method_resolution.rs | |||
@@ -6,8 +6,8 @@ use std::sync::Arc; | |||
6 | 6 | ||
7 | use arrayvec::ArrayVec; | 7 | use arrayvec::ArrayVec; |
8 | use hir_def::{ | 8 | use hir_def::{ |
9 | lang_item::LangItemTarget, resolver::Resolver, type_ref::Mutability, AssocItemId, FunctionId, | 9 | lang_item::LangItemTarget, resolver::Resolver, type_ref::Mutability, AssocContainerId, |
10 | HasModule, ImplId, Lookup, TraitId, | 10 | AssocItemId, FunctionId, HasModule, ImplId, Lookup, TraitId, |
11 | }; | 11 | }; |
12 | use hir_expand::name::Name; | 12 | use hir_expand::name::Name; |
13 | use ra_db::CrateId; | 13 | use ra_db::CrateId; |
@@ -58,7 +58,7 @@ impl CrateImplBlocks { | |||
58 | 58 | ||
59 | let crate_def_map = db.crate_def_map(krate); | 59 | let crate_def_map = db.crate_def_map(krate); |
60 | for (_module_id, module_data) in crate_def_map.modules.iter() { | 60 | for (_module_id, module_data) in crate_def_map.modules.iter() { |
61 | for &impl_id in module_data.impls.iter() { | 61 | for impl_id in module_data.scope.impls() { |
62 | match db.impl_trait(impl_id) { | 62 | match db.impl_trait(impl_id) { |
63 | Some(tr) => { | 63 | Some(tr) => { |
64 | res.impls_by_trait.entry(tr.trait_).or_default().push(impl_id); | 64 | res.impls_by_trait.entry(tr.trait_).or_default().push(impl_id); |
@@ -134,7 +134,7 @@ impl Ty { | |||
134 | LangItemTarget::ImplBlockId(it) => Some(it), | 134 | LangItemTarget::ImplBlockId(it) => Some(it), |
135 | _ => None, | 135 | _ => None, |
136 | }) | 136 | }) |
137 | .map(|it| it.lookup(db).container.krate) | 137 | .map(|it| it.lookup(db).container.module(db).krate) |
138 | .collect(); | 138 | .collect(); |
139 | Some(res) | 139 | Some(res) |
140 | } | 140 | } |
@@ -451,12 +451,12 @@ fn transform_receiver_ty( | |||
451 | self_ty: &Canonical<Ty>, | 451 | self_ty: &Canonical<Ty>, |
452 | ) -> Option<Ty> { | 452 | ) -> Option<Ty> { |
453 | let substs = match function_id.lookup(db).container { | 453 | let substs = match function_id.lookup(db).container { |
454 | hir_def::ContainerId::TraitId(_) => Substs::build_for_def(db, function_id) | 454 | AssocContainerId::TraitId(_) => Substs::build_for_def(db, function_id) |
455 | .push(self_ty.value.clone()) | 455 | .push(self_ty.value.clone()) |
456 | .fill_with_unknown() | 456 | .fill_with_unknown() |
457 | .build(), | 457 | .build(), |
458 | hir_def::ContainerId::ImplId(impl_id) => inherent_impl_substs(db, impl_id, &self_ty)?, | 458 | AssocContainerId::ImplId(impl_id) => inherent_impl_substs(db, impl_id, &self_ty)?, |
459 | hir_def::ContainerId::ModuleId(_) => unreachable!(), | 459 | AssocContainerId::ContainerId(_) => unreachable!(), |
460 | }; | 460 | }; |
461 | let sig = db.callable_item_signature(function_id.into()); | 461 | let sig = db.callable_item_signature(function_id.into()); |
462 | Some(sig.params()[0].clone().subst(&substs)) | 462 | Some(sig.params()[0].clone().subst(&substs)) |
diff --git a/crates/ra_hir_ty/src/test_db.rs b/crates/ra_hir_ty/src/test_db.rs index 476f6df52..1a31b587b 100644 --- a/crates/ra_hir_ty/src/test_db.rs +++ b/crates/ra_hir_ty/src/test_db.rs | |||
@@ -98,7 +98,7 @@ impl TestDB { | |||
98 | } | 98 | } |
99 | } | 99 | } |
100 | 100 | ||
101 | for &impl_id in crate_def_map[module_id].impls.iter() { | 101 | for impl_id in crate_def_map[module_id].scope.impls() { |
102 | let impl_data = self.impl_data(impl_id); | 102 | let impl_data = self.impl_data(impl_id); |
103 | for item in impl_data.items.iter() { | 103 | for item in impl_data.items.iter() { |
104 | if let AssocItemId::FunctionId(f) = item { | 104 | if let AssocItemId::FunctionId(f) = item { |
diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs index d724ee122..d447b4571 100644 --- a/crates/ra_hir_ty/src/tests.rs +++ b/crates/ra_hir_ty/src/tests.rs | |||
@@ -182,7 +182,7 @@ fn visit_module( | |||
182 | _ => (), | 182 | _ => (), |
183 | } | 183 | } |
184 | } | 184 | } |
185 | for &impl_id in crate_def_map[module_id].impls.iter() { | 185 | for impl_id in crate_def_map[module_id].scope.impls() { |
186 | let impl_data = db.impl_data(impl_id); | 186 | let impl_data = db.impl_data(impl_id); |
187 | for &item in impl_data.items.iter() { | 187 | for &item in impl_data.items.iter() { |
188 | match item { | 188 | match item { |
diff --git a/crates/ra_hir_ty/src/tests/coercion.rs b/crates/ra_hir_ty/src/tests/coercion.rs index ac9e3872a..7e99a42ed 100644 --- a/crates/ra_hir_ty/src/tests/coercion.rs +++ b/crates/ra_hir_ty/src/tests/coercion.rs | |||
@@ -370,6 +370,22 @@ fn test() { | |||
370 | } | 370 | } |
371 | 371 | ||
372 | #[test] | 372 | #[test] |
373 | fn return_coerce_unknown() { | ||
374 | assert_snapshot!( | ||
375 | infer_with_mismatches(r#" | ||
376 | fn foo() -> u32 { | ||
377 | return unknown; | ||
378 | } | ||
379 | "#, true), | ||
380 | @r###" | ||
381 | [17; 40) '{ ...own; }': ! | ||
382 | [23; 37) 'return unknown': ! | ||
383 | [30; 37) 'unknown': u32 | ||
384 | "### | ||
385 | ); | ||
386 | } | ||
387 | |||
388 | #[test] | ||
373 | fn coerce_autoderef() { | 389 | fn coerce_autoderef() { |
374 | assert_snapshot!( | 390 | assert_snapshot!( |
375 | infer_with_mismatches(r#" | 391 | infer_with_mismatches(r#" |
@@ -440,3 +456,73 @@ fn test() { | |||
440 | "### | 456 | "### |
441 | ); | 457 | ); |
442 | } | 458 | } |
459 | |||
460 | #[test] | ||
461 | fn closure_return_coerce() { | ||
462 | assert_snapshot!( | ||
463 | infer_with_mismatches(r#" | ||
464 | fn foo() { | ||
465 | let x = || { | ||
466 | if true { | ||
467 | return &1u32; | ||
468 | } | ||
469 | &&1u32 | ||
470 | }; | ||
471 | } | ||
472 | "#, true), | ||
473 | @r###" | ||
474 | [10; 106) '{ ... }; }': () | ||
475 | [20; 21) 'x': || -> &u32 | ||
476 | [24; 103) '|| { ... }': || -> &u32 | ||
477 | [27; 103) '{ ... }': &u32 | ||
478 | [37; 82) 'if tru... }': () | ||
479 | [40; 44) 'true': bool | ||
480 | [45; 82) '{ ... }': ! | ||
481 | [59; 71) 'return &1u32': ! | ||
482 | [66; 71) '&1u32': &u32 | ||
483 | [67; 71) '1u32': u32 | ||
484 | [91; 97) '&&1u32': &&u32 | ||
485 | [92; 97) '&1u32': &u32 | ||
486 | [93; 97) '1u32': u32 | ||
487 | "### | ||
488 | ); | ||
489 | } | ||
490 | |||
491 | #[test] | ||
492 | fn coerce_fn_item_to_fn_ptr() { | ||
493 | assert_snapshot!( | ||
494 | infer_with_mismatches(r#" | ||
495 | fn foo(x: u32) -> isize { 1 } | ||
496 | fn test() { | ||
497 | let f: fn(u32) -> isize = foo; | ||
498 | } | ||
499 | "#, true), | ||
500 | @r###" | ||
501 | [8; 9) 'x': u32 | ||
502 | [25; 30) '{ 1 }': isize | ||
503 | [27; 28) '1': isize | ||
504 | [41; 79) '{ ...foo; }': () | ||
505 | [51; 52) 'f': fn(u32) -> isize | ||
506 | [73; 76) 'foo': fn foo(u32) -> isize | ||
507 | "### | ||
508 | ); | ||
509 | } | ||
510 | |||
511 | #[test] | ||
512 | fn coerce_closure_to_fn_ptr() { | ||
513 | assert_snapshot!( | ||
514 | infer_with_mismatches(r#" | ||
515 | fn test() { | ||
516 | let f: fn(u32) -> isize = |x| { 1 }; | ||
517 | } | ||
518 | "#, true), | ||
519 | @r###" | ||
520 | [11; 55) '{ ...1 }; }': () | ||
521 | [21; 22) 'f': fn(u32) -> isize | ||
522 | [43; 52) '|x| { 1 }': |u32| -> isize | ||
523 | [44; 45) 'x': u32 | ||
524 | [47; 52) '{ 1 }': isize | ||
525 | [49; 50) '1': isize | ||
526 | "### | ||
527 | ); | ||
528 | } | ||
diff --git a/crates/ra_hir_ty/src/tests/macros.rs b/crates/ra_hir_ty/src/tests/macros.rs index 812f171db..69c695cc8 100644 --- a/crates/ra_hir_ty/src/tests/macros.rs +++ b/crates/ra_hir_ty/src/tests/macros.rs | |||
@@ -183,6 +183,48 @@ fn test() { S.foo()<|>; } | |||
183 | } | 183 | } |
184 | 184 | ||
185 | #[test] | 185 | #[test] |
186 | fn infer_impl_items_generated_by_macros() { | ||
187 | let t = type_at( | ||
188 | r#" | ||
189 | //- /main.rs | ||
190 | macro_rules! m { | ||
191 | () => (fn foo(&self) -> u128 {0}) | ||
192 | } | ||
193 | struct S; | ||
194 | impl S { | ||
195 | m!(); | ||
196 | } | ||
197 | |||
198 | fn test() { S.foo()<|>; } | ||
199 | "#, | ||
200 | ); | ||
201 | assert_eq!(t, "u128"); | ||
202 | } | ||
203 | |||
204 | #[test] | ||
205 | fn infer_impl_items_generated_by_macros_chain() { | ||
206 | let t = type_at( | ||
207 | r#" | ||
208 | //- /main.rs | ||
209 | macro_rules! m_inner { | ||
210 | () => {fn foo(&self) -> u128 {0}} | ||
211 | } | ||
212 | macro_rules! m { | ||
213 | () => {m_inner!();} | ||
214 | } | ||
215 | |||
216 | struct S; | ||
217 | impl S { | ||
218 | m!(); | ||
219 | } | ||
220 | |||
221 | fn test() { S.foo()<|>; } | ||
222 | "#, | ||
223 | ); | ||
224 | assert_eq!(t, "u128"); | ||
225 | } | ||
226 | |||
227 | #[test] | ||
186 | fn infer_macro_with_dollar_crate_is_correct_in_expr() { | 228 | fn infer_macro_with_dollar_crate_is_correct_in_expr() { |
187 | let (db, pos) = TestDB::with_position( | 229 | let (db, pos) = TestDB::with_position( |
188 | r#" | 230 | r#" |
diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs index 18976c9ae..6fe647a5e 100644 --- a/crates/ra_hir_ty/src/tests/simple.rs +++ b/crates/ra_hir_ty/src/tests/simple.rs | |||
@@ -1606,3 +1606,58 @@ fn main() { | |||
1606 | ); | 1606 | ); |
1607 | assert_eq!(t, "u32"); | 1607 | assert_eq!(t, "u32"); |
1608 | } | 1608 | } |
1609 | |||
1610 | #[test] | ||
1611 | fn closure_return() { | ||
1612 | assert_snapshot!( | ||
1613 | infer(r#" | ||
1614 | fn foo() -> u32 { | ||
1615 | let x = || -> usize { return 1; }; | ||
1616 | } | ||
1617 | "#), | ||
1618 | @r###" | ||
1619 | [17; 59) '{ ...; }; }': () | ||
1620 | [27; 28) 'x': || -> usize | ||
1621 | [31; 56) '|| -> ...n 1; }': || -> usize | ||
1622 | [43; 56) '{ return 1; }': ! | ||
1623 | [45; 53) 'return 1': ! | ||
1624 | [52; 53) '1': usize | ||
1625 | "### | ||
1626 | ); | ||
1627 | } | ||
1628 | |||
1629 | #[test] | ||
1630 | fn closure_return_unit() { | ||
1631 | assert_snapshot!( | ||
1632 | infer(r#" | ||
1633 | fn foo() -> u32 { | ||
1634 | let x = || { return; }; | ||
1635 | } | ||
1636 | "#), | ||
1637 | @r###" | ||
1638 | [17; 48) '{ ...; }; }': () | ||
1639 | [27; 28) 'x': || -> () | ||
1640 | [31; 45) '|| { return; }': || -> () | ||
1641 | [34; 45) '{ return; }': ! | ||
1642 | [36; 42) 'return': ! | ||
1643 | "### | ||
1644 | ); | ||
1645 | } | ||
1646 | |||
1647 | #[test] | ||
1648 | fn closure_return_inferred() { | ||
1649 | assert_snapshot!( | ||
1650 | infer(r#" | ||
1651 | fn foo() -> u32 { | ||
1652 | let x = || { "test" }; | ||
1653 | } | ||
1654 | "#), | ||
1655 | @r###" | ||
1656 | [17; 47) '{ ..." }; }': () | ||
1657 | [27; 28) 'x': || -> &str | ||
1658 | [31; 44) '|| { "test" }': || -> &str | ||
1659 | [34; 44) '{ "test" }': &str | ||
1660 | [36; 42) '"test"': &str | ||
1661 | "### | ||
1662 | ); | ||
1663 | } | ||
diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs index 802937cb0..76e2198b6 100644 --- a/crates/ra_hir_ty/src/tests/traits.rs +++ b/crates/ra_hir_ty/src/tests/traits.rs | |||
@@ -393,11 +393,11 @@ fn test() -> u64 { | |||
393 | [54; 55) 'a': S | 393 | [54; 55) 'a': S |
394 | [58; 59) 'S': S(fn(u32) -> u64) -> S | 394 | [58; 59) 'S': S(fn(u32) -> u64) -> S |
395 | [58; 68) 'S(|i| 2*i)': S | 395 | [58; 68) 'S(|i| 2*i)': S |
396 | [60; 67) '|i| 2*i': |i32| -> i32 | 396 | [60; 67) '|i| 2*i': |u32| -> u64 |
397 | [61; 62) 'i': i32 | 397 | [61; 62) 'i': u32 |
398 | [64; 65) '2': i32 | 398 | [64; 65) '2': u32 |
399 | [64; 67) '2*i': i32 | 399 | [64; 67) '2*i': u32 |
400 | [66; 67) 'i': i32 | 400 | [66; 67) 'i': u32 |
401 | [78; 79) 'b': u64 | 401 | [78; 79) 'b': u64 |
402 | [82; 83) 'a': S | 402 | [82; 83) 'a': S |
403 | [82; 85) 'a.0': fn(u32) -> u64 | 403 | [82; 85) 'a.0': fn(u32) -> u64 |
@@ -427,6 +427,38 @@ fn indexing_arrays() { | |||
427 | } | 427 | } |
428 | 428 | ||
429 | #[test] | 429 | #[test] |
430 | fn infer_ops_index() { | ||
431 | let (db, pos) = TestDB::with_position( | ||
432 | r#" | ||
433 | //- /main.rs crate:main deps:std | ||
434 | |||
435 | struct Bar; | ||
436 | struct Foo; | ||
437 | |||
438 | impl std::ops::Index<u32> for Bar { | ||
439 | type Output = Foo; | ||
440 | } | ||
441 | |||
442 | fn test() { | ||
443 | let a = Bar; | ||
444 | let b = a[1]; | ||
445 | b<|>; | ||
446 | } | ||
447 | |||
448 | //- /std.rs crate:std | ||
449 | |||
450 | #[prelude_import] use ops::*; | ||
451 | mod ops { | ||
452 | pub trait Index<Idx> { | ||
453 | type Output; | ||
454 | } | ||
455 | } | ||
456 | "#, | ||
457 | ); | ||
458 | assert_eq!("Foo", type_at_pos(&db, pos)); | ||
459 | } | ||
460 | |||
461 | #[test] | ||
430 | fn deref_trait() { | 462 | fn deref_trait() { |
431 | let t = type_at( | 463 | let t = type_at( |
432 | r#" | 464 | r#" |
diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index fc21872b2..5eb032d86 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs | |||
@@ -9,7 +9,9 @@ use chalk_ir::{ | |||
9 | }; | 9 | }; |
10 | use chalk_rust_ir::{AssociatedTyDatum, AssociatedTyValue, ImplDatum, StructDatum, TraitDatum}; | 10 | use chalk_rust_ir::{AssociatedTyDatum, AssociatedTyValue, ImplDatum, StructDatum, TraitDatum}; |
11 | 11 | ||
12 | use hir_def::{AssocItemId, ContainerId, GenericDefId, ImplId, Lookup, TraitId, TypeAliasId}; | 12 | use hir_def::{ |
13 | AssocContainerId, AssocItemId, GenericDefId, HasModule, ImplId, Lookup, TraitId, TypeAliasId, | ||
14 | }; | ||
13 | use ra_db::{ | 15 | use ra_db::{ |
14 | salsa::{InternId, InternKey}, | 16 | salsa::{InternId, InternKey}, |
15 | CrateId, | 17 | CrateId, |
@@ -542,7 +544,7 @@ pub(crate) fn associated_ty_data_query( | |||
542 | debug!("associated_ty_data {:?}", id); | 544 | debug!("associated_ty_data {:?}", id); |
543 | let type_alias: TypeAliasId = from_chalk(db, id); | 545 | let type_alias: TypeAliasId = from_chalk(db, id); |
544 | let trait_ = match type_alias.lookup(db).container { | 546 | let trait_ = match type_alias.lookup(db).container { |
545 | ContainerId::TraitId(t) => t, | 547 | AssocContainerId::TraitId(t) => t, |
546 | _ => panic!("associated type not in trait"), | 548 | _ => panic!("associated type not in trait"), |
547 | }; | 549 | }; |
548 | let generic_params = generics(db, type_alias.into()); | 550 | let generic_params = generics(db, type_alias.into()); |
@@ -591,7 +593,7 @@ pub(crate) fn trait_datum_query( | |||
591 | let bound_vars = Substs::bound_vars(&generic_params); | 593 | let bound_vars = Substs::bound_vars(&generic_params); |
592 | let flags = chalk_rust_ir::TraitFlags { | 594 | let flags = chalk_rust_ir::TraitFlags { |
593 | auto: trait_data.auto, | 595 | auto: trait_data.auto, |
594 | upstream: trait_.lookup(db).container.krate != krate, | 596 | upstream: trait_.lookup(db).container.module(db).krate != krate, |
595 | non_enumerable: true, | 597 | non_enumerable: true, |
596 | coinductive: false, // only relevant for Chalk testing | 598 | coinductive: false, // only relevant for Chalk testing |
597 | // FIXME set these flags correctly | 599 | // FIXME set these flags correctly |
@@ -671,7 +673,7 @@ fn impl_block_datum( | |||
671 | let bound_vars = Substs::bound_vars(&generic_params); | 673 | let bound_vars = Substs::bound_vars(&generic_params); |
672 | let trait_ref = trait_ref.subst(&bound_vars); | 674 | let trait_ref = trait_ref.subst(&bound_vars); |
673 | let trait_ = trait_ref.trait_; | 675 | let trait_ = trait_ref.trait_; |
674 | let impl_type = if impl_id.lookup(db).container.krate == krate { | 676 | let impl_type = if impl_id.lookup(db).container.module(db).krate == krate { |
675 | chalk_rust_ir::ImplType::Local | 677 | chalk_rust_ir::ImplType::Local |
676 | } else { | 678 | } else { |
677 | chalk_rust_ir::ImplType::External | 679 | chalk_rust_ir::ImplType::External |
@@ -755,7 +757,7 @@ fn type_alias_associated_ty_value( | |||
755 | ) -> Arc<AssociatedTyValue<ChalkIr>> { | 757 | ) -> Arc<AssociatedTyValue<ChalkIr>> { |
756 | let type_alias_data = db.type_alias_data(type_alias); | 758 | let type_alias_data = db.type_alias_data(type_alias); |
757 | let impl_id = match type_alias.lookup(db).container { | 759 | let impl_id = match type_alias.lookup(db).container { |
758 | ContainerId::ImplId(it) => it, | 760 | AssocContainerId::ImplId(it) => it, |
759 | _ => panic!("assoc ty value should be in impl"), | 761 | _ => panic!("assoc ty value should be in impl"), |
760 | }; | 762 | }; |
761 | 763 | ||
diff --git a/crates/ra_hir_ty/src/utils.rs b/crates/ra_hir_ty/src/utils.rs index 29799a8cb..0b1806a84 100644 --- a/crates/ra_hir_ty/src/utils.rs +++ b/crates/ra_hir_ty/src/utils.rs | |||
@@ -9,7 +9,7 @@ use hir_def::{ | |||
9 | path::Path, | 9 | path::Path, |
10 | resolver::{HasResolver, TypeNs}, | 10 | resolver::{HasResolver, TypeNs}, |
11 | type_ref::TypeRef, | 11 | type_ref::TypeRef, |
12 | ContainerId, GenericDefId, Lookup, TraitId, TypeAliasId, TypeParamId, VariantId, | 12 | AssocContainerId, GenericDefId, Lookup, TraitId, TypeAliasId, TypeParamId, VariantId, |
13 | }; | 13 | }; |
14 | use hir_expand::name::{name, Name}; | 14 | use hir_expand::name::{name, Name}; |
15 | 15 | ||
@@ -155,8 +155,8 @@ fn parent_generic_def(db: &impl DefDatabase, def: GenericDefId) -> Option<Generi | |||
155 | }; | 155 | }; |
156 | 156 | ||
157 | match container { | 157 | match container { |
158 | ContainerId::ImplId(it) => Some(it.into()), | 158 | AssocContainerId::ImplId(it) => Some(it.into()), |
159 | ContainerId::TraitId(it) => Some(it.into()), | 159 | AssocContainerId::TraitId(it) => Some(it.into()), |
160 | ContainerId::ModuleId(_) => None, | 160 | AssocContainerId::ContainerId(_) => None, |
161 | } | 161 | } |
162 | } | 162 | } |