aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2020-02-07 14:13:00 +0000
committerFlorian Diebold <[email protected]>2020-02-07 17:28:10 +0000
commit6787f124b5557120cd1e4557cbdb59aa7f215be6 (patch)
tree810aa041297e9ff450dbb3a8a05f09a10c08f8a2 /crates/ra_hir_ty
parent0718682cffaae34e5c106c793c60f6706fc04b05 (diff)
Clean up RPIT a bit
Diffstat (limited to 'crates/ra_hir_ty')
-rw-r--r--crates/ra_hir_ty/src/infer.rs28
-rw-r--r--crates/ra_hir_ty/src/infer/expr.rs1
-rw-r--r--crates/ra_hir_ty/src/marks.rs1
-rw-r--r--crates/ra_hir_ty/src/tests/traits.rs24
4 files changed, 13 insertions, 41 deletions
diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs
index 8d5b7c943..dec6bd84c 100644
--- a/crates/ra_hir_ty/src/infer.rs
+++ b/crates/ra_hir_ty/src/infer.rs
@@ -34,7 +34,6 @@ use hir_expand::{diagnostics::DiagnosticSink, name::name};
34use ra_arena::map::ArenaMap; 34use ra_arena::map::ArenaMap;
35use ra_prof::profile; 35use ra_prof::profile;
36use ra_syntax::SmolStr; 36use ra_syntax::SmolStr;
37use test_utils::tested_by;
38 37
39use super::{ 38use super::{
40 primitive::{FloatTy, IntTy}, 39 primitive::{FloatTy, IntTy},
@@ -289,29 +288,6 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
289 self.make_ty_with_mode(type_ref, ImplTraitLoweringMode::Disallowed) 288 self.make_ty_with_mode(type_ref, ImplTraitLoweringMode::Disallowed)
290 } 289 }
291 290
292 /// Replaces `impl Trait` in `ty` by type variables and obligations for
293 /// those variables. This is done for function arguments when calling a
294 /// function, and for return types when inside the function body, i.e. in
295 /// the cases where the `impl Trait` is 'transparent'. In other cases, `impl
296 /// Trait` is represented by `Ty::Opaque`.
297 fn insert_vars_for_impl_trait(&mut self, ty: Ty) -> Ty {
298 ty.fold(&mut |ty| match ty {
299 Ty::Opaque(preds) => {
300 tested_by!(insert_vars_for_impl_trait);
301 let var = self.table.new_type_var();
302 let var_subst = Substs::builder(1).push(var.clone()).build();
303 self.obligations.extend(
304 preds
305 .iter()
306 .map(|pred| pred.clone().subst_bound_vars(&var_subst))
307 .filter_map(Obligation::from_predicate),
308 );
309 var
310 }
311 _ => ty,
312 })
313 }
314
315 /// Replaces Ty::Unknown by a new type var, so we can maybe still infer it. 291 /// Replaces Ty::Unknown by a new type var, so we can maybe still infer it.
316 fn insert_type_vars_shallow(&mut self, ty: Ty) -> Ty { 292 fn insert_type_vars_shallow(&mut self, ty: Ty) -> Ty {
317 match ty { 293 match ty {
@@ -487,8 +463,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
487 463
488 self.infer_pat(*pat, &ty, BindingMode::default()); 464 self.infer_pat(*pat, &ty, BindingMode::default());
489 } 465 }
490 let return_ty = self.make_ty_with_mode(&data.ret_type, ImplTraitLoweringMode::Variable); 466 let return_ty = self.make_ty_with_mode(&data.ret_type, ImplTraitLoweringMode::Disallowed); // FIXME implement RPIT
491 self.return_ty = self.insert_vars_for_impl_trait(return_ty); 467 self.return_ty = return_ty;
492 } 468 }
493 469
494 fn infer_body(&mut self) { 470 fn infer_body(&mut self) {
diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs
index b69785e55..6222bd90e 100644
--- a/crates/ra_hir_ty/src/infer/expr.rs
+++ b/crates/ra_hir_ty/src/infer/expr.rs
@@ -635,7 +635,6 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
635 continue; 635 continue;
636 } 636 }
637 637
638 let param_ty = self.insert_vars_for_impl_trait(param_ty);
639 let param_ty = self.normalize_associated_types_in(param_ty); 638 let param_ty = self.normalize_associated_types_in(param_ty);
640 self.infer_expr_coerce(arg, &Expectation::has_type(param_ty.clone())); 639 self.infer_expr_coerce(arg, &Expectation::has_type(param_ty.clone()));
641 } 640 }
diff --git a/crates/ra_hir_ty/src/marks.rs b/crates/ra_hir_ty/src/marks.rs
index fe74acf11..0f754eb9c 100644
--- a/crates/ra_hir_ty/src/marks.rs
+++ b/crates/ra_hir_ty/src/marks.rs
@@ -6,5 +6,4 @@ test_utils::marks!(
6 type_var_resolves_to_int_var 6 type_var_resolves_to_int_var
7 match_ergonomics_ref 7 match_ergonomics_ref
8 coerce_merge_fail_fallback 8 coerce_merge_fail_fallback
9 insert_vars_for_impl_trait
10); 9);
diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs
index bae5eae07..134cea8d8 100644
--- a/crates/ra_hir_ty/src/tests/traits.rs
+++ b/crates/ra_hir_ty/src/tests/traits.rs
@@ -1,7 +1,6 @@
1use insta::assert_snapshot; 1use insta::assert_snapshot;
2 2
3use ra_db::fixture::WithFixture; 3use ra_db::fixture::WithFixture;
4use test_utils::covers;
5 4
6use super::{infer, infer_with_mismatches, type_at, type_at_pos}; 5use super::{infer, infer_with_mismatches, type_at, type_at_pos};
7use crate::test_db::TestDB; 6use crate::test_db::TestDB;
@@ -1650,7 +1649,6 @@ fn test<T, U>() where T: Trait<U::Item>, U: Trait<T::Item> {
1650 1649
1651#[test] 1650#[test]
1652fn unify_impl_trait() { 1651fn unify_impl_trait() {
1653 covers!(insert_vars_for_impl_trait);
1654 assert_snapshot!( 1652 assert_snapshot!(
1655 infer_with_mismatches(r#" 1653 infer_with_mismatches(r#"
1656trait Trait<T> {} 1654trait Trait<T> {}
@@ -1682,26 +1680,26 @@ fn test() -> impl Trait<i32> {
1682 [172; 183) '{ loop {} }': T 1680 [172; 183) '{ loop {} }': T
1683 [174; 181) 'loop {}': ! 1681 [174; 181) 'loop {}': !
1684 [179; 181) '{}': () 1682 [179; 181) '{}': ()
1685 [214; 310) '{ ...t()) }': S<i32> 1683 [214; 310) '{ ...t()) }': S<{unknown}>
1686 [224; 226) 's1': S<u32> 1684 [224; 226) 's1': S<u32>
1687 [229; 230) 'S': S<u32>(T) -> S<T> 1685 [229; 230) 'S': S<u32>(u32) -> S<u32>
1688 [229; 241) 'S(default())': S<u32> 1686 [229; 241) 'S(default())': S<u32>
1689 [231; 238) 'default': fn default<u32>() -> T 1687 [231; 238) 'default': fn default<u32>() -> u32
1690 [231; 240) 'default()': u32 1688 [231; 240) 'default()': u32
1691 [247; 250) 'foo': fn foo(impl Trait<u32>) -> () 1689 [247; 250) 'foo': fn foo<S<u32>>(S<u32>) -> ()
1692 [247; 254) 'foo(s1)': () 1690 [247; 254) 'foo(s1)': ()
1693 [251; 253) 's1': S<u32> 1691 [251; 253) 's1': S<u32>
1694 [264; 265) 'x': i32 1692 [264; 265) 'x': i32
1695 [273; 276) 'bar': fn bar<i32>(impl Trait<T>) -> T 1693 [273; 276) 'bar': fn bar<i32, S<i32>>(S<i32>) -> i32
1696 [273; 290) 'bar(S(...lt()))': i32 1694 [273; 290) 'bar(S(...lt()))': i32
1697 [277; 278) 'S': S<i32>(T) -> S<T> 1695 [277; 278) 'S': S<i32>(i32) -> S<i32>
1698 [277; 289) 'S(default())': S<i32> 1696 [277; 289) 'S(default())': S<i32>
1699 [279; 286) 'default': fn default<i32>() -> T 1697 [279; 286) 'default': fn default<i32>() -> i32
1700 [279; 288) 'default()': i32 1698 [279; 288) 'default()': i32
1701 [296; 297) 'S': S<i32>(T) -> S<T> 1699 [296; 297) 'S': S<{unknown}>({unknown}) -> S<{unknown}>
1702 [296; 308) 'S(default())': S<i32> 1700 [296; 308) 'S(default())': S<{unknown}>
1703 [298; 305) 'default': fn default<i32>() -> T 1701 [298; 305) 'default': fn default<{unknown}>() -> {unknown}
1704 [298; 307) 'default()': i32 1702 [298; 307) 'default()': {unknown}
1705 "### 1703 "###
1706 ); 1704 );
1707} 1705}