diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-12-20 17:18:52 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-12-20 17:18:52 +0000 |
commit | 99d6f544f2a590d3feef47ce62d5cd9a8b463a47 (patch) | |
tree | ae4e2dd0ae2c62baf79ec1197f5ec5f94a7645e5 /crates/ra_hir_ty/src | |
parent | cfc50ff160d0af2ce5cd931c6d41161abfdb2fbd (diff) | |
parent | 2a8c9100bfb1294a469bc039a5b9597eabed7073 (diff) |
Merge #2616
2616: Handle closure return types r=flodiebold a=flodiebold
Fixes #2547.
Co-authored-by: Florian Diebold <[email protected]>
Diffstat (limited to 'crates/ra_hir_ty/src')
-rw-r--r-- | crates/ra_hir_ty/src/infer.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/infer/expr.rs | 17 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/tests/coercion.rs | 31 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/tests/simple.rs | 55 |
4 files changed, 106 insertions, 4 deletions
diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs index bbbc391c4..9f2ed830e 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. |
diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs index 8be567917..253332c30 100644 --- a/crates/ra_hir_ty/src/infer/expr.rs +++ b/crates/ra_hir_ty/src/infer/expr.rs | |||
@@ -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 | } |
diff --git a/crates/ra_hir_ty/src/tests/coercion.rs b/crates/ra_hir_ty/src/tests/coercion.rs index ac9e3872a..33d6ca403 100644 --- a/crates/ra_hir_ty/src/tests/coercion.rs +++ b/crates/ra_hir_ty/src/tests/coercion.rs | |||
@@ -440,3 +440,34 @@ fn test() { | |||
440 | "### | 440 | "### |
441 | ); | 441 | ); |
442 | } | 442 | } |
443 | |||
444 | #[test] | ||
445 | fn closure_return_coerce() { | ||
446 | assert_snapshot!( | ||
447 | infer_with_mismatches(r#" | ||
448 | fn foo() { | ||
449 | let x = || { | ||
450 | if true { | ||
451 | return &1u32; | ||
452 | } | ||
453 | &&1u32 | ||
454 | }; | ||
455 | } | ||
456 | "#, true), | ||
457 | @r###" | ||
458 | [10; 106) '{ ... }; }': () | ||
459 | [20; 21) 'x': || -> &u32 | ||
460 | [24; 103) '|| { ... }': || -> &u32 | ||
461 | [27; 103) '{ ... }': &u32 | ||
462 | [37; 82) 'if tru... }': () | ||
463 | [40; 44) 'true': bool | ||
464 | [45; 82) '{ ... }': ! | ||
465 | [59; 71) 'return &1u32': ! | ||
466 | [66; 71) '&1u32': &u32 | ||
467 | [67; 71) '1u32': u32 | ||
468 | [91; 97) '&&1u32': &&u32 | ||
469 | [92; 97) '&1u32': &u32 | ||
470 | [93; 97) '1u32': u32 | ||
471 | "### | ||
472 | ); | ||
473 | } | ||
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 | } | ||