Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Nov 6, 2024
1 parent f5200e0 commit f23dd9d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
10 changes: 0 additions & 10 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1371,11 +1371,6 @@ fn (mut c Checker) check_or_last_stmt(mut stmt ast.Stmt, ret_type ast.Type, expr
c.expected_type = ret_type
c.expected_or_type = ret_type.clear_option_and_result()
last_stmt_typ := c.expr(mut stmt.expr)
if (c.inside_return || c.inside_assign) && mut stmt.expr is ast.CallExpr
&& !stmt.expr.is_return_used {
// last expr on if/match is a callexpr
stmt.expr.is_return_used = true
}
if last_stmt_typ.has_flag(.option) || last_stmt_typ == ast.none_type {
if stmt.expr in [ast.Ident, ast.SelectorExpr, ast.CallExpr, ast.None, ast.CastExpr] {
expected_type_name := c.table.type_to_str(ret_type.clear_option_and_result())
Expand Down Expand Up @@ -2221,11 +2216,6 @@ fn (mut c Checker) stmt(mut node ast.Stmt) {
if !c.inside_return {
c.check_expr_option_or_result_call(node.expr, or_typ)
}
// if the last expr is a callexpr mark its return as used
if (c.inside_return || c.inside_assign) && c.is_last_stmt
&& mut node.expr is ast.CallExpr && !node.expr.is_return_used {
node.expr.is_return_used = true
}
// TODO: This should work, even if it's prolly useless .-.
// node.typ = c.check_expr_option_or_result_call(node.expr, ast.void_type)
}
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/parser/if_match.v
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
pos := branch_first_pos.extend_with_last_line(branch_last_pos, p.prev_tok.line_nr)
branch_pos := branch_first_pos.extend_with_last_line(p.tok.pos(), p.tok.line_nr)
post_comments := p.eat_comments()
if p.inside_assign_rhs && stmts.len > 0 && stmts.last() is ast.ExprStmt {
mut last_expr := stmts.last() as ast.ExprStmt
if mut last_expr.expr is ast.CallExpr {
last_expr.expr.is_return_used = true
}
}
branches << ast.MatchBranch{
exprs: exprs
ecmnts: ecmnts
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -3995,7 +3995,10 @@ fn (mut p Parser) return_stmt() ast.Return {
}
}
// return exprs
old_assign_rhs := p.inside_assign_rhs
p.inside_assign_rhs = true
exprs := p.expr_list(true)
p.inside_assign_rhs = old_assign_rhs
end_pos := exprs.last().pos()
return ast.Return{
exprs: exprs
Expand Down
14 changes: 6 additions & 8 deletions vlib/v2/types/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1490,18 +1490,16 @@ fn (mut c Checker) resolve_call_or_cast_expr(expr ast.CallOrCastExpr) ast.Expr {
// expr_type := c.expr(expr.expr)
if lhs_type is FnType {
return ast.CallExpr{
lhs: expr.lhs
args: [expr.expr]
pos: expr.pos
is_return_used: true
lhs: expr.lhs
args: [expr.expr]
pos: expr.pos
}
} else {
// c.log(expr)
return ast.CastExpr{
typ: expr.lhs
expr: expr.expr
pos: expr.pos
is_return_used: true
typ: expr.lhs
expr: expr.expr
pos: expr.pos
}
}
}
Expand Down

0 comments on commit f23dd9d

Please sign in to comment.