683: fix AST for if expressions r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-01-26 21:41:27 +00:00
commit e40d8d4032
6 changed files with 112 additions and 5 deletions

View file

@ -498,7 +498,13 @@ impl ExprCollector {
let then_branch = self.collect_block_opt(e.then_branch());
let else_branch = e
.else_branch()
.map(|e| self.collect_block(e))
.map(|b| match b {
ast::ElseBranchFlavor::Block(it) => self.collect_block(it),
ast::ElseBranchFlavor::IfExpr(elif) => {
let expr: &ast::Expr = ast::Expr::cast(elif.syntax()).unwrap();
self.collect_expr(expr)
}
})
.unwrap_or_else(|| self.empty_block());
let placeholder_pat = self.pats.alloc(Pat::Missing);
let arms = vec![
@ -521,7 +527,13 @@ impl ExprCollector {
} else {
let condition = self.collect_expr_opt(e.condition().and_then(|c| c.expr()));
let then_branch = self.collect_block_opt(e.then_branch());
let else_branch = e.else_branch().map(|e| self.collect_block(e));
let else_branch = e.else_branch().map(|b| match b {
ast::ElseBranchFlavor::Block(it) => self.collect_block(it),
ast::ElseBranchFlavor::IfExpr(elif) => {
let expr: &ast::Expr = ast::Expr::cast(elif.syntax()).unwrap();
self.collect_expr(expr)
}
});
self.alloc_expr(
Expr::If {
condition,

View file

@ -0,0 +1,17 @@
---
created: "2019-01-26T21:36:52.714121185+00:00"
creator: insta@0.5.2
expression: "&result"
source: crates/ra_hir/src/ty/tests.rs
---
[35; 38) 'foo': Foo
[45; 109) '{ ... } }': ()
[51; 107) 'if tru... }': ()
[54; 58) 'true': bool
[59; 67) '{ }': ()
[73; 107) 'if fal... }': i32
[76; 81) 'false': bool
[82; 107) '{ ... }': i32
[92; 95) 'foo': Foo
[92; 101) 'foo.field': i32

View file

@ -284,6 +284,23 @@ fn test() {
);
}
#[test]
fn infer_in_elseif() {
check_inference(
"infer_in_elseif",
r#"
struct Foo { field: i32 }
fn main(foo: Foo) {
if true {
} else if false {
foo.field
}
}
"#,
)
}
#[test]
fn infer_inherent_method() {
check_inference(