aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-30 20:51:47 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-30 20:51:47 +0000
commit28fdb8d03caf1ab8b40ed0fcbe8e47451fe030d9 (patch)
treea795362bb8cd25edb87e8da3f9786d68e029c22b /crates/ra_hir/src
parentdb17e06c2eec892ab807fd191bc11b15d8da42e2 (diff)
parent13cb4a1b370038dee51ae739a42d6b98acaef385 (diff)
Merge #701
701: Minor type inference tweaks r=flodiebold a=marcusklaas Pass down expectation for reference expressions and type the guard in match expressions. I wasn't able to add a test for the former addition because the type variable previously introduced would always resolve to the right type in the things I tried! Co-authored-by: Marcus Klaas de Vries <[email protected]>
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/expr.rs8
-rw-r--r--crates/ra_hir/src/ty.rs16
-rw-r--r--crates/ra_hir/src/ty/snapshots/tests__infer_adt_pattern.snap23
-rw-r--r--crates/ra_hir/src/ty/snapshots/tests__infer_array.snap14
-rw-r--r--crates/ra_hir/src/ty/tests.rs4
5 files changed, 46 insertions, 19 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index 83e913e4a..37aa24677 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -219,7 +219,7 @@ pub use ra_syntax::ast::BinOp as BinaryOp;
219#[derive(Debug, Clone, Eq, PartialEq)] 219#[derive(Debug, Clone, Eq, PartialEq)]
220pub struct MatchArm { 220pub struct MatchArm {
221 pub pats: Vec<PatId>, 221 pub pats: Vec<PatId>,
222 // guard: Option<ExprId>, // TODO 222 pub guard: Option<ExprId>,
223 pub expr: ExprId, 223 pub expr: ExprId,
224} 224}
225 225
@@ -515,10 +515,12 @@ impl ExprCollector {
515 MatchArm { 515 MatchArm {
516 pats: vec![pat], 516 pats: vec![pat],
517 expr: then_branch, 517 expr: then_branch,
518 guard: None,
518 }, 519 },
519 MatchArm { 520 MatchArm {
520 pats: vec![placeholder_pat], 521 pats: vec![placeholder_pat],
521 expr: else_branch, 522 expr: else_branch,
523 guard: None,
522 }, 524 },
523 ]; 525 ];
524 self.alloc_expr( 526 self.alloc_expr(
@@ -617,6 +619,10 @@ impl ExprCollector {
617 .map(|arm| MatchArm { 619 .map(|arm| MatchArm {
618 pats: arm.pats().map(|p| self.collect_pat(p)).collect(), 620 pats: arm.pats().map(|p| self.collect_pat(p)).collect(),
619 expr: self.collect_expr_opt(arm.expr()), 621 expr: self.collect_expr_opt(arm.expr()),
622 guard: arm
623 .guard()
624 .and_then(|guard| guard.expr())
625 .map(|e| self.collect_expr(e)),
620 }) 626 })
621 .collect() 627 .collect()
622 } else { 628 } else {
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index 0472414a6..60c231e82 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -1488,7 +1488,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1488 for &pat in &arm.pats { 1488 for &pat in &arm.pats {
1489 let _pat_ty = self.infer_pat(pat, &input_ty); 1489 let _pat_ty = self.infer_pat(pat, &input_ty);
1490 } 1490 }
1491 // TODO type the guard 1491 if let Some(guard_expr) = arm.guard {
1492 self.infer_expr(guard_expr, &Expectation::has_type(Ty::Bool));
1493 }
1492 self.infer_expr(arm.expr, &expected); 1494 self.infer_expr(arm.expr, &expected);
1493 } 1495 }
1494 1496
@@ -1561,9 +1563,17 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1561 cast_ty 1563 cast_ty
1562 } 1564 }
1563 Expr::Ref { expr, mutability } => { 1565 Expr::Ref { expr, mutability } => {
1564 // TODO pass the expectation down 1566 let expectation = if let Ty::Ref(ref subty, expected_mutability) = expected.ty {
1565 let inner_ty = self.infer_expr(*expr, &Expectation::none()); 1567 if expected_mutability == Mutability::Mut && *mutability == Mutability::Shared {
1568 // TODO: throw type error - expected mut reference but found shared ref,
1569 // which cannot be coerced
1570 }
1571 Expectation::has_type((**subty).clone())
1572 } else {
1573 Expectation::none()
1574 };
1566 // TODO reference coercions etc. 1575 // TODO reference coercions etc.
1576 let inner_ty = self.infer_expr(*expr, &expectation);
1567 Ty::Ref(Arc::new(inner_ty), *mutability) 1577 Ty::Ref(Arc::new(inner_ty), *mutability)
1568 } 1578 }
1569 Expr::UnaryOp { expr, op } => { 1579 Expr::UnaryOp { expr, op } => {
diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_adt_pattern.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_adt_pattern.snap
index 2719f592e..48c83cbb2 100644
--- a/crates/ra_hir/src/ty/snapshots/tests__infer_adt_pattern.snap
+++ b/crates/ra_hir/src/ty/snapshots/tests__infer_adt_pattern.snap
@@ -1,10 +1,10 @@
1--- 1---
2created: "2019-01-22T14:44:59.880187500+00:00" 2created: "2019-01-28T21:58:55.559331849+00:00"
3creator: insta@0.4.0 3creator: insta@0.5.2
4expression: "&result" 4expression: "&result"
5source: "crates\\ra_hir\\src\\ty\\tests.rs" 5source: crates/ra_hir/src/ty/tests.rs
6--- 6---
7[68; 262) '{ ... d; }': () 7[68; 289) '{ ... d; }': ()
8[78; 79) 'e': E 8[78; 79) 'e': E
9[82; 95) 'E::A { x: 3 }': E 9[82; 95) 'E::A { x: 3 }': E
10[92; 93) '3': usize 10[92; 93) '3': usize
@@ -15,15 +15,18 @@ source: "crates\\ra_hir\\src\\ty\\tests.rs"
15[129; 148) 'E::A {..._var }': E 15[129; 148) 'E::A {..._var }': E
16[139; 146) 'new_var': usize 16[139; 146) 'new_var': usize
17[151; 152) 'e': E 17[151; 152) 'e': E
18[159; 218) 'match ... }': usize 18[159; 245) 'match ... }': usize
19[165; 166) 'e': E 19[165; 166) 'e': E
20[177; 187) 'E::A { x }': E 20[177; 187) 'E::A { x }': E
21[184; 185) 'x': usize 21[184; 185) 'x': usize
22[191; 192) 'x': usize 22[191; 192) 'x': usize
23[202; 206) 'E::B': E 23[202; 206) 'E::B': E
24[210; 211) '1': usize 24[210; 213) 'foo': bool
25[229; 248) 'ref d ...{ .. }': &E 25[217; 218) '1': usize
26[237; 248) 'E::A { .. }': E 26[228; 232) 'E::B': E
27[251; 252) 'e': E 27[236; 238) '10': usize
28[258; 259) 'd': &E 28[256; 275) 'ref d ...{ .. }': &E
29[264; 275) 'E::A { .. }': E
30[278; 279) 'e': E
31[285; 286) 'd': &E
29 32
diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_array.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_array.snap
index 3f2faa598..042248c35 100644
--- a/crates/ra_hir/src/ty/snapshots/tests__infer_array.snap
+++ b/crates/ra_hir/src/ty/snapshots/tests__infer_array.snap
@@ -1,12 +1,12 @@
1--- 1---
2created: "2019-01-22T14:44:59.880187500+00:00" 2created: "2019-01-30T20:08:05.185312835+00:00"
3creator: insta@0.4.0 3creator: insta@0.5.2
4expression: "&result" 4expression: "&result"
5source: "crates\\ra_hir\\src\\ty\\tests.rs" 5source: crates/ra_hir/src/ty/tests.rs
6--- 6---
7[9; 10) 'x': &str 7[9; 10) 'x': &str
8[18; 19) 'y': isize 8[18; 19) 'y': isize
9[28; 293) '{ ... []; }': () 9[28; 324) '{ ... 3]; }': ()
10[38; 39) 'a': [&str] 10[38; 39) 'a': [&str]
11[42; 45) '[x]': [&str] 11[42; 45) '[x]': [&str]
12[43; 44) 'x': &str 12[43; 44) 'x': &str
@@ -56,4 +56,10 @@ source: "crates\\ra_hir\\src\\ty\\tests.rs"
56[260; 263) '"b"': &str 56[260; 263) '"b"': &str
57[275; 276) 'x': [u8] 57[275; 276) 'x': [u8]
58[288; 290) '[]': [u8] 58[288; 290) '[]': [u8]
59[300; 301) 'z': &[u8]
60[311; 321) '&[1, 2, 3]': &[u8]
61[312; 321) '[1, 2, 3]': [u8]
62[313; 314) '1': u8
63[316; 317) '2': u8
64[319; 320) '3': u8
59 65
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index b36e6ec47..cb8d6351d 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -371,6 +371,7 @@ fn test(x: &str, y: isize) {
371 371
372 let b = [a, ["b"]]; 372 let b = [a, ["b"]];
373 let x: [u8; 0] = []; 373 let x: [u8; 0] = [];
374 let z: &[u8] = &[1, 2, 3];
374} 375}
375"#, 376"#,
376 ); 377 );
@@ -426,7 +427,8 @@ fn test() {
426 427
427 match e { 428 match e {
428 E::A { x } => x, 429 E::A { x } => x,
429 E::B => 1, 430 E::B if foo => 1,
431 E::B => 10,
430 }; 432 };
431 433
432 let ref d @ E::A { .. } = e; 434 let ref d @ E::A { .. } = e;