checker, cgen: minor cleanup of last expr stmt expression (#14057)
parent
5e98ea12c8
commit
f33bccc111
|
@ -1533,14 +1533,12 @@ fn (mut c Checker) check_or_last_stmt(stmt ast.Stmt, ret_type ast.Type, expr_ret
|
||||||
if stmt.typ == ast.void_type {
|
if stmt.typ == ast.void_type {
|
||||||
if stmt.expr is ast.IfExpr {
|
if stmt.expr is ast.IfExpr {
|
||||||
for branch in stmt.expr.branches {
|
for branch in stmt.expr.branches {
|
||||||
last_stmt := branch.stmts[branch.stmts.len - 1]
|
c.check_or_last_stmt(branch.stmts.last(), ret_type, expr_return_type)
|
||||||
c.check_or_last_stmt(last_stmt, ret_type, expr_return_type)
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
} else if stmt.expr is ast.MatchExpr {
|
} else if stmt.expr is ast.MatchExpr {
|
||||||
for branch in stmt.expr.branches {
|
for branch in stmt.expr.branches {
|
||||||
last_stmt := branch.stmts[branch.stmts.len - 1]
|
c.check_or_last_stmt(branch.stmts.last(), ret_type, expr_return_type)
|
||||||
c.check_or_last_stmt(last_stmt, ret_type, expr_return_type)
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1570,14 +1568,12 @@ fn (mut c Checker) check_or_last_stmt(stmt ast.Stmt, ret_type ast.Type, expr_ret
|
||||||
match stmt.expr {
|
match stmt.expr {
|
||||||
ast.IfExpr {
|
ast.IfExpr {
|
||||||
for branch in stmt.expr.branches {
|
for branch in stmt.expr.branches {
|
||||||
last_stmt := branch.stmts[branch.stmts.len - 1]
|
c.check_or_last_stmt(branch.stmts.last(), ret_type, expr_return_type)
|
||||||
c.check_or_last_stmt(last_stmt, ret_type, expr_return_type)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast.MatchExpr {
|
ast.MatchExpr {
|
||||||
for branch in stmt.expr.branches {
|
for branch in stmt.expr.branches {
|
||||||
last_stmt := branch.stmts[branch.stmts.len - 1]
|
c.check_or_last_stmt(branch.stmts.last(), ret_type, expr_return_type)
|
||||||
c.check_or_last_stmt(last_stmt, ret_type, expr_return_type)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -156,8 +156,8 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
|
||||||
c.smartcast_cond_pos = token.Pos{}
|
c.smartcast_cond_pos = token.Pos{}
|
||||||
}
|
}
|
||||||
if expr_required {
|
if expr_required {
|
||||||
if branch.stmts.len > 0 && branch.stmts[branch.stmts.len - 1] is ast.ExprStmt {
|
if branch.stmts.len > 0 && branch.stmts.last() is ast.ExprStmt {
|
||||||
mut last_expr := branch.stmts[branch.stmts.len - 1] as ast.ExprStmt
|
mut last_expr := branch.stmts.last() as ast.ExprStmt
|
||||||
c.expected_type = former_expected_type
|
c.expected_type = former_expected_type
|
||||||
if c.expected_type.has_flag(.optional) {
|
if c.expected_type.has_flag(.optional) {
|
||||||
if node.typ == ast.void_type {
|
if node.typ == ast.void_type {
|
||||||
|
|
|
@ -51,7 +51,7 @@ pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
|
||||||
// currently the last statement in a match branch does not have an
|
// currently the last statement in a match branch does not have an
|
||||||
// expected value set, so e.g. IfExpr.is_expr is not set.
|
// expected value set, so e.g. IfExpr.is_expr is not set.
|
||||||
// probably any mismatch will be caught by not producing a value instead
|
// probably any mismatch will be caught by not producing a value instead
|
||||||
for st in branch.stmts[0..branch.stmts.len - 1] {
|
for st in branch.stmts[..branch.stmts.len - 1] {
|
||||||
// must not contain C statements
|
// must not contain C statements
|
||||||
st.check_c_expr() or {
|
st.check_c_expr() or {
|
||||||
c.error('`match` expression branch has $err.msg()', st.pos)
|
c.error('`match` expression branch has $err.msg()', st.pos)
|
||||||
|
@ -64,7 +64,7 @@ pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
|
||||||
}
|
}
|
||||||
// If the last statement is an expression, return its type
|
// If the last statement is an expression, return its type
|
||||||
if branch.stmts.len > 0 {
|
if branch.stmts.len > 0 {
|
||||||
mut stmt := branch.stmts[branch.stmts.len - 1]
|
mut stmt := branch.stmts.last()
|
||||||
if mut stmt is ast.ExprStmt {
|
if mut stmt is ast.ExprStmt {
|
||||||
if node.is_expr {
|
if node.is_expr {
|
||||||
c.expected_type = node.expected_type
|
c.expected_type = node.expected_type
|
||||||
|
|
|
@ -263,14 +263,14 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) {
|
||||||
if node.is_expr {
|
if node.is_expr {
|
||||||
len := branch.stmts.len
|
len := branch.stmts.len
|
||||||
if len > 0 {
|
if len > 0 {
|
||||||
last := branch.stmts[len - 1] as ast.ExprStmt
|
last := branch.stmts.last() as ast.ExprStmt
|
||||||
if len > 1 {
|
if len > 1 {
|
||||||
tmp := g.new_tmp_var()
|
tmp := g.new_tmp_var()
|
||||||
styp := g.typ(last.typ)
|
styp := g.typ(last.typ)
|
||||||
g.indent++
|
g.indent++
|
||||||
g.writeln('$styp $tmp;')
|
g.writeln('$styp $tmp;')
|
||||||
g.writeln('{')
|
g.writeln('{')
|
||||||
g.stmts(branch.stmts[0..len - 1])
|
g.stmts(branch.stmts[..len - 1])
|
||||||
g.write('\t$tmp = ')
|
g.write('\t$tmp = ')
|
||||||
g.stmt(last)
|
g.stmt(last)
|
||||||
g.writeln('}')
|
g.writeln('}')
|
||||||
|
|
|
@ -148,7 +148,7 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
|
||||||
} else {
|
} else {
|
||||||
mut is_auto_heap := false
|
mut is_auto_heap := false
|
||||||
if branch.stmts.len > 0 {
|
if branch.stmts.len > 0 {
|
||||||
scope := g.file.scope.innermost(ast.Node(branch.stmts[branch.stmts.len - 1]).pos().pos)
|
scope := g.file.scope.innermost(ast.Node(branch.stmts.last()).pos().pos)
|
||||||
if v := scope.find_var(branch.cond.vars[0].name) {
|
if v := scope.find_var(branch.cond.vars[0].name) {
|
||||||
is_auto_heap = v.is_auto_heap
|
is_auto_heap = v.is_auto_heap
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,13 +28,13 @@ fn (mut g JsGen) comptime_if(node ast.IfExpr) {
|
||||||
print('$branch.stmts')
|
print('$branch.stmts')
|
||||||
len := branch.stmts.len
|
len := branch.stmts.len
|
||||||
if len > 0 {
|
if len > 0 {
|
||||||
last := branch.stmts[len - 1] as ast.ExprStmt
|
last := branch.stmts.last() as ast.ExprStmt
|
||||||
if len > 1 {
|
if len > 1 {
|
||||||
tmp := g.new_tmp_var()
|
tmp := g.new_tmp_var()
|
||||||
g.inc_indent()
|
g.inc_indent()
|
||||||
g.writeln('let $tmp;')
|
g.writeln('let $tmp;')
|
||||||
g.writeln('{')
|
g.writeln('{')
|
||||||
g.stmts(branch.stmts[0..len - 1])
|
g.stmts(branch.stmts[..len - 1])
|
||||||
g.write('\t$tmp = ')
|
g.write('\t$tmp = ')
|
||||||
g.stmt(last)
|
g.stmt(last)
|
||||||
g.writeln('}')
|
g.writeln('}')
|
||||||
|
|
Loading…
Reference in New Issue