diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-26 21:41:27 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-26 21:41:27 +0000 |
commit | e40d8d40321b191ee82b8b07910f8a0898c8914c (patch) | |
tree | 7adcdf5fd1a09bdb18776e210a4686342979cbe6 /crates/ra_hir/src | |
parent | 99b032b0f6b3f718ee9500655f1149dc33eac610 (diff) | |
parent | 619af1e22cb71b981fde4cedbf6ebce9b3488028 (diff) |
Merge #683
683: fix AST for if expressions r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/expr.rs | 16 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/snapshots/tests__infer_in_elseif.snap | 17 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 17 |
3 files changed, 48 insertions, 2 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 29469af2c..60d997bbe 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
@@ -498,7 +498,13 @@ impl ExprCollector { | |||
498 | let then_branch = self.collect_block_opt(e.then_branch()); | 498 | let then_branch = self.collect_block_opt(e.then_branch()); |
499 | let else_branch = e | 499 | let else_branch = e |
500 | .else_branch() | 500 | .else_branch() |
501 | .map(|e| self.collect_block(e)) | 501 | .map(|b| match b { |
502 | ast::ElseBranchFlavor::Block(it) => self.collect_block(it), | ||
503 | ast::ElseBranchFlavor::IfExpr(elif) => { | ||
504 | let expr: &ast::Expr = ast::Expr::cast(elif.syntax()).unwrap(); | ||
505 | self.collect_expr(expr) | ||
506 | } | ||
507 | }) | ||
502 | .unwrap_or_else(|| self.empty_block()); | 508 | .unwrap_or_else(|| self.empty_block()); |
503 | let placeholder_pat = self.pats.alloc(Pat::Missing); | 509 | let placeholder_pat = self.pats.alloc(Pat::Missing); |
504 | let arms = vec![ | 510 | let arms = vec![ |
@@ -521,7 +527,13 @@ impl ExprCollector { | |||
521 | } else { | 527 | } else { |
522 | let condition = self.collect_expr_opt(e.condition().and_then(|c| c.expr())); | 528 | let condition = self.collect_expr_opt(e.condition().and_then(|c| c.expr())); |
523 | let then_branch = self.collect_block_opt(e.then_branch()); | 529 | let then_branch = self.collect_block_opt(e.then_branch()); |
524 | let else_branch = e.else_branch().map(|e| self.collect_block(e)); | 530 | let else_branch = e.else_branch().map(|b| match b { |
531 | ast::ElseBranchFlavor::Block(it) => self.collect_block(it), | ||
532 | ast::ElseBranchFlavor::IfExpr(elif) => { | ||
533 | let expr: &ast::Expr = ast::Expr::cast(elif.syntax()).unwrap(); | ||
534 | self.collect_expr(expr) | ||
535 | } | ||
536 | }); | ||
525 | self.alloc_expr( | 537 | self.alloc_expr( |
526 | Expr::If { | 538 | Expr::If { |
527 | condition, | 539 | condition, |
diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_in_elseif.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_in_elseif.snap new file mode 100644 index 000000000..6a435e5cf --- /dev/null +++ b/crates/ra_hir/src/ty/snapshots/tests__infer_in_elseif.snap | |||
@@ -0,0 +1,17 @@ | |||
1 | --- | ||
2 | created: "2019-01-26T21:36:52.714121185+00:00" | ||
3 | creator: [email protected] | ||
4 | expression: "&result" | ||
5 | source: crates/ra_hir/src/ty/tests.rs | ||
6 | --- | ||
7 | [35; 38) 'foo': Foo | ||
8 | [45; 109) '{ ... } }': () | ||
9 | [51; 107) 'if tru... }': () | ||
10 | [54; 58) 'true': bool | ||
11 | [59; 67) '{ }': () | ||
12 | [73; 107) 'if fal... }': i32 | ||
13 | [76; 81) 'false': bool | ||
14 | [82; 107) '{ ... }': i32 | ||
15 | [92; 95) 'foo': Foo | ||
16 | [92; 101) 'foo.field': i32 | ||
17 | |||
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index e0b0689f8..e1165f682 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -285,6 +285,23 @@ fn test() { | |||
285 | } | 285 | } |
286 | 286 | ||
287 | #[test] | 287 | #[test] |
288 | fn infer_in_elseif() { | ||
289 | check_inference( | ||
290 | "infer_in_elseif", | ||
291 | r#" | ||
292 | struct Foo { field: i32 } | ||
293 | fn main(foo: Foo) { | ||
294 | if true { | ||
295 | |||
296 | } else if false { | ||
297 | foo.field | ||
298 | } | ||
299 | } | ||
300 | "#, | ||
301 | ) | ||
302 | } | ||
303 | |||
304 | #[test] | ||
288 | fn infer_inherent_method() { | 305 | fn infer_inherent_method() { |
289 | check_inference( | 306 | check_inference( |
290 | "infer_inherent_method", | 307 | "infer_inherent_method", |