checker: fix match expected_type

pull/3884/head
Alexander Medvednikov 2020-02-29 18:34:25 +01:00
parent a15dcbfb2d
commit ee05b51f47
1 changed files with 13 additions and 5 deletions

View File

@ -431,6 +431,12 @@ fn (c mut Checker) stmt(node ast.Stmt) {
} }
} }
fn (c mut Checker) stmts(stmts []ast.Stmt) {
for stmt in stmts {
c.stmt(stmt)
}
}
pub fn (c mut Checker) expr(node ast.Expr) table.Type { pub fn (c mut Checker) expr(node ast.Expr) table.Type {
match mut node { match mut node {
ast.AssignExpr { ast.AssignExpr {
@ -610,12 +616,10 @@ pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type {
for i, block in node.blocks { for i, block in node.blocks {
if i < node.match_exprs.len { if i < node.match_exprs.len {
match_expr := node.match_exprs[i] match_expr := node.match_exprs[i]
c.expected_type = node.typ c.expected_type = t
c.expr(match_expr) c.expr(match_expr)
} }
for stmt in block.stmts { c.stmts(block.stmts)
c.stmt(stmt)
}
// If the last statement is an expression, return its type // If the last statement is an expression, return its type
if block.stmts.len > 0 { if block.stmts.len > 0 {
match block.stmts[block.stmts.len - 1] { match block.stmts[block.stmts.len - 1] {
@ -746,8 +750,12 @@ pub fn (c mut Checker) index_expr(node ast.IndexExpr) table.Type {
pub fn (c mut Checker) enum_val(node ast.EnumVal) table.Type { pub fn (c mut Checker) enum_val(node ast.EnumVal) table.Type {
typ_idx := if node.enum_name == '' { c.expected_type } else { // typ_idx := if node.enum_name == '' { c.expected_type } else { //
c.table.find_type_idx(node.enum_name) } c.table.find_type_idx(node.enum_name) }
// println('checker: enum_val: $node.enum_name typeidx=$typ_idx')
if typ_idx == 0 {
c.error('not an enum (type_idx=0)', node.pos)
}
typ := c.table.get_type_symbol(table.Type(typ_idx)) typ := c.table.get_type_symbol(table.Type(typ_idx))
// println('checker: enum_val: $node.enum_name typeidx=$typ_idx tname=$typ.name') // println('tname=$typ.name')
if typ.kind != .enum_ { if typ.kind != .enum_ {
c.error('not an enum', node.pos) c.error('not an enum', node.pos)
} }