diff options
author | Florian Diebold <[email protected]> | 2019-12-15 17:56:38 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2019-12-15 17:56:38 +0000 |
commit | 6e1c2d0df89a390be33c81b6e03a5ad352763593 (patch) | |
tree | f3ba344e185e17e6e62ce650462fd63af9eddd26 /crates/ra_hir_ty/src/infer.rs | |
parent | ac961b261458bfeb23f7d4e896d5f957b0854a3a (diff) |
Handle impl Trait more correctly
When calling a function, argument-position impl Trait is transparent; same for
return-position impl Trait when inside the function. So in these cases, we need
to represent that type not by `Ty::Opaque`, but by a type variable that can be
unified with whatever flows into there.
Diffstat (limited to 'crates/ra_hir_ty/src/infer.rs')
-rw-r--r-- | crates/ra_hir_ty/src/infer.rs | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs index af42854cc..1aa1330a6 100644 --- a/crates/ra_hir_ty/src/infer.rs +++ b/crates/ra_hir_ty/src/infer.rs | |||
@@ -274,6 +274,28 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
274 | self.normalize_associated_types_in(ty) | 274 | self.normalize_associated_types_in(ty) |
275 | } | 275 | } |
276 | 276 | ||
277 | /// Replaces `impl Trait` in `ty` by type variables and obligations for | ||
278 | /// those variables. This is done for function arguments when calling a | ||
279 | /// function, and for return types when inside the function body, i.e. in | ||
280 | /// the cases where the `impl Trait` is 'transparent'. In other cases, `impl | ||
281 | /// Trait` is represented by `Ty::Opaque`. | ||
282 | fn insert_vars_for_impl_trait(&mut self, ty: Ty) -> Ty { | ||
283 | ty.fold(&mut |ty| match ty { | ||
284 | Ty::Opaque(preds) => { | ||
285 | let var = self.table.new_type_var(); | ||
286 | let var_subst = Substs::builder(1).push(var.clone()).build(); | ||
287 | self.obligations.extend( | ||
288 | preds | ||
289 | .iter() | ||
290 | .map(|pred| pred.clone().subst_bound_vars(&var_subst)) | ||
291 | .filter_map(Obligation::from_predicate), | ||
292 | ); | ||
293 | var | ||
294 | } | ||
295 | _ => ty, | ||
296 | }) | ||
297 | } | ||
298 | |||
277 | /// Replaces Ty::Unknown by a new type var, so we can maybe still infer it. | 299 | /// Replaces Ty::Unknown by a new type var, so we can maybe still infer it. |
278 | fn insert_type_vars_shallow(&mut self, ty: Ty) -> Ty { | 300 | fn insert_type_vars_shallow(&mut self, ty: Ty) -> Ty { |
279 | match ty { | 301 | match ty { |
@@ -414,7 +436,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
414 | 436 | ||
415 | self.infer_pat(*pat, &ty, BindingMode::default()); | 437 | self.infer_pat(*pat, &ty, BindingMode::default()); |
416 | } | 438 | } |
417 | self.return_ty = self.make_ty(&data.ret_type); | 439 | let return_ty = self.make_ty(&data.ret_type); |
440 | self.return_ty = self.insert_vars_for_impl_trait(return_ty); | ||
418 | } | 441 | } |
419 | 442 | ||
420 | fn infer_body(&mut self) { | 443 | fn infer_body(&mut self) { |