autofree: fix scope position

pull/5853/head
Alexander Medvednikov 2020-07-16 19:40:14 +02:00
parent dc89a914ea
commit e4fac6ff97
2 changed files with 21 additions and 11 deletions

View File

@ -650,6 +650,13 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type {
c.error('infix expr: cannot use `$right.name` (right expression) as `$left.name`',
infix_expr.pos)
}
/*
if (infix_expr.left is ast.InfixExpr &&
(infix_expr.left as ast.InfixExpr).op == .inc) ||
(infix_expr.right is ast.InfixExpr && (infix_expr.right as ast.InfixExpr).op == .inc) {
c.warn('`++` and `--` are statements, not expressions', infix_expr.pos)
}
*/
return if infix_expr.op.is_relational() {
table.bool_type
} else {
@ -2589,15 +2596,17 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, type_sym table.TypeSymbol
mut low := 0
mut high := 0
c.expected_type = node.expected_type
if expr.low is ast.IntegerLiteral as low_expr {
if expr.high is ast.IntegerLiteral as high_expr {
low_expr := expr.low
high_expr := expr.high
if low_expr is ast.IntegerLiteral {
if high_expr is ast.IntegerLiteral {
low = low_expr.val.int()
high = high_expr.val.int()
} else {
c.error('mismatched range types', low_expr.pos)
}
} else if expr.low is ast.CharLiteral as low_expr {
if expr.high is ast.CharLiteral as high_expr {
} else if low_expr is ast.CharLiteral {
if high_expr is ast.CharLiteral {
low = low_expr.val[0]
high = high_expr.val[0]
} else {
@ -2607,8 +2616,7 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, type_sym table.TypeSymbol
typ := c.table.type_to_str(c.expr(expr.low))
c.error('cannot use type `$typ` in match range', branch.pos)
}
for i in low..high {
for i in low .. high {
key = i.str()
val := if key in branch_exprs { branch_exprs[key] } else { 0 }
if val == 1 {

View File

@ -629,9 +629,9 @@ fn (mut g Gen) stmts(stmts []ast.Stmt) {
if stmt !is ast.FnDecl {
// g.writeln('// autofree scope')
// g.writeln('// autofree_scope_vars($stmt.position().pos) | ${typeof(stmt)}')
// go back 1 position is important so we dont get the
// go back 1 position is important so we dont get the
// internal scope of for loops and possibly other nodes
g.autofree_scope_vars(stmt.position().pos-1)
g.autofree_scope_vars(stmt.position().pos - 1)
}
}
}
@ -2367,9 +2367,10 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
return
}
mut is_guard := false
mut guard_vars := []string{ len: node.branches.len }
mut guard_vars := []string{len: node.branches.len}
for i, branch in node.branches {
if branch.cond is ast.IfGuardExpr as cond {
cond := branch.cond
if cond is ast.IfGuardExpr {
if !is_guard {
is_guard = true
g.writeln('{ /* if guard */ ')
@ -2784,7 +2785,8 @@ fn (mut g Gen) return_statement(node ast.Return) {
g.write(')')
}
if free {
g.writeln(';')
g.writeln('; // free tmp exprs')
g.autofree_scope_vars(node.pos.pos + 1)
g.write('return $tmp')
}
} else {