diff options
author | Emil Lauridsen <[email protected]> | 2019-12-13 11:44:07 +0000 |
---|---|---|
committer | Emil Lauridsen <[email protected]> | 2019-12-13 11:45:37 +0000 |
commit | 95dc2de8e979264e1c76ce5594e8a63547a7956e (patch) | |
tree | 1394f8a25960fd676301bde2273acd5753c96d45 | |
parent | 16cf6bcf4b614a7a1a3a3f3659f7bb3df7cff086 (diff) |
Add helper for resolving associated type of trait in infer
-rw-r--r-- | crates/ra_hir_ty/src/infer.rs | 18 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/infer/expr.rs | 54 |
2 files changed, 25 insertions, 47 deletions
diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs index d16f1eb46..62d5c8803 100644 --- a/crates/ra_hir_ty/src/infer.rs +++ b/crates/ra_hir_ty/src/infer.rs | |||
@@ -338,6 +338,24 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
338 | self.table.resolve_ty_shallow(ty) | 338 | self.table.resolve_ty_shallow(ty) |
339 | } | 339 | } |
340 | 340 | ||
341 | fn resolve_associated_type(&mut self, inner_ty: Ty, assoc_ty: Option<TypeAliasId>) -> Ty { | ||
342 | match assoc_ty { | ||
343 | Some(res_assoc_ty) => { | ||
344 | let ty = self.table.new_type_var(); | ||
345 | let projection = ProjectionPredicate { | ||
346 | ty: ty.clone(), | ||
347 | projection_ty: ProjectionTy { | ||
348 | associated_ty: res_assoc_ty, | ||
349 | parameters: Substs::single(inner_ty), | ||
350 | }, | ||
351 | }; | ||
352 | self.obligations.push(Obligation::Projection(projection)); | ||
353 | self.resolve_ty_as_possible(ty) | ||
354 | } | ||
355 | None => Ty::Unknown, | ||
356 | } | ||
357 | } | ||
358 | |||
341 | /// Recurses through the given type, normalizing associated types mentioned | 359 | /// Recurses through the given type, normalizing associated types mentioned |
342 | /// in it by replacing them by type variables and registering obligations to | 360 | /// in it by replacing them by type variables and registering obligations to |
343 | /// resolve later. This should be done once for every type we get from some | 361 | /// resolve later. This should be done once for every type we get from some |
diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs index 2c296987c..6110f5abd 100644 --- a/crates/ra_hir_ty/src/infer/expr.rs +++ b/crates/ra_hir_ty/src/infer/expr.rs | |||
@@ -19,8 +19,8 @@ use crate::{ | |||
19 | method_resolution, op, | 19 | method_resolution, op, |
20 | traits::InEnvironment, | 20 | traits::InEnvironment, |
21 | utils::{generics, variant_data, Generics}, | 21 | utils::{generics, variant_data, Generics}, |
22 | CallableDef, InferTy, IntTy, Mutability, Obligation, ProjectionPredicate, ProjectionTy, Substs, | 22 | ApplicationTy, CallableDef, InferTy, IntTy, Mutability, Obligation, Substs, TraitRef, Ty, |
23 | TraitRef, Ty, TypeCtor, TypeWalk, Uncertain, | 23 | TypeCtor, TypeWalk, Uncertain, |
24 | }; | 24 | }; |
25 | 25 | ||
26 | use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; | 26 | use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; |
@@ -95,21 +95,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
95 | Expr::For { iterable, body, pat } => { | 95 | Expr::For { iterable, body, pat } => { |
96 | let iterable_ty = self.infer_expr(*iterable, &Expectation::none()); | 96 | let iterable_ty = self.infer_expr(*iterable, &Expectation::none()); |
97 | 97 | ||
98 | let pat_ty = match self.resolve_into_iter_item() { | 98 | let pat_ty = |
99 | Some(into_iter_item_alias) => { | 99 | self.resolve_associated_type(iterable_ty, self.resolve_into_iter_item()); |
100 | let pat_ty = self.table.new_type_var(); | ||
101 | let projection = ProjectionPredicate { | ||
102 | ty: pat_ty.clone(), | ||
103 | projection_ty: ProjectionTy { | ||
104 | associated_ty: into_iter_item_alias, | ||
105 | parameters: Substs::single(iterable_ty), | ||
106 | }, | ||
107 | }; | ||
108 | self.obligations.push(Obligation::Projection(projection)); | ||
109 | self.resolve_ty_as_possible(pat_ty) | ||
110 | } | ||
111 | None => Ty::Unknown, | ||
112 | }; | ||
113 | 100 | ||
114 | self.infer_pat(*pat, &pat_ty, BindingMode::default()); | 101 | self.infer_pat(*pat, &pat_ty, BindingMode::default()); |
115 | self.infer_expr(*body, &Expectation::has_type(Ty::unit())); | 102 | self.infer_expr(*body, &Expectation::has_type(Ty::unit())); |
@@ -284,40 +271,13 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
284 | } | 271 | } |
285 | Expr::Await { expr } => { | 272 | Expr::Await { expr } => { |
286 | let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); | 273 | let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); |
287 | let ty = match self.resolve_future_future_output() { | 274 | let ty = |
288 | Some(future_future_output_alias) => { | 275 | self.resolve_associated_type(inner_ty, self.resolve_future_future_output()); |
289 | let ty = self.table.new_type_var(); | ||
290 | let projection = ProjectionPredicate { | ||
291 | ty: ty.clone(), | ||
292 | projection_ty: ProjectionTy { | ||
293 | associated_ty: future_future_output_alias, | ||
294 | parameters: Substs::single(inner_ty), | ||
295 | }, | ||
296 | }; | ||
297 | self.obligations.push(Obligation::Projection(projection)); | ||
298 | self.resolve_ty_as_possible(ty) | ||
299 | } | ||
300 | None => Ty::Unknown, | ||
301 | }; | ||
302 | ty | 276 | ty |
303 | } | 277 | } |
304 | Expr::Try { expr } => { | 278 | Expr::Try { expr } => { |
305 | let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); | 279 | let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); |
306 | let ty = match self.resolve_ops_try_ok() { | 280 | let ty = self.resolve_associated_type(inner_ty, self.resolve_ops_try_ok()); |
307 | Some(ops_try_ok_alias) => { | ||
308 | let ty = self.table.new_type_var(); | ||
309 | let projection = ProjectionPredicate { | ||
310 | ty: ty.clone(), | ||
311 | projection_ty: ProjectionTy { | ||
312 | associated_ty: ops_try_ok_alias, | ||
313 | parameters: Substs::single(inner_ty), | ||
314 | }, | ||
315 | }; | ||
316 | self.obligations.push(Obligation::Projection(projection)); | ||
317 | self.resolve_ty_as_possible(ty) | ||
318 | } | ||
319 | None => Ty::Unknown, | ||
320 | }; | ||
321 | ty | 281 | ty |
322 | } | 282 | } |
323 | Expr::Cast { expr, type_ref } => { | 283 | Expr::Cast { expr, type_ref } => { |