From 5fd03383995ac3f7e2fcb4b136c9b82cb29a10bd Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 4 Jul 2023 21:29:11 +0800 Subject: [PATCH] checker: change smartcast(expr_ ast.Expr,..) to smartcast(mut expr ast.Expr,..) (#18777) --- vlib/v/checker/checker.v | 3 +-- vlib/v/checker/for.v | 2 +- vlib/v/checker/if.v | 21 +++++++++++---------- vlib/v/checker/match.v | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 7505c553b9ca32..182dcdb433a3d4 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3572,14 +3572,13 @@ fn (mut c Checker) concat_expr(mut node ast.ConcatExpr) ast.Type { } // smartcast takes the expression with the current type which should be smartcasted to the target type in the given scope -fn (mut c Checker) smartcast(expr_ ast.Expr, cur_type ast.Type, to_type_ ast.Type, mut scope ast.Scope) { +fn (mut c Checker) smartcast(mut expr ast.Expr, cur_type ast.Type, to_type_ ast.Type, mut scope ast.Scope) { sym := c.table.sym(cur_type) to_type := if sym.kind == .interface_ && c.table.sym(to_type_).kind != .interface_ { to_type_.ref() } else { to_type_ } - mut expr := unsafe { expr_ } match mut expr { ast.SelectorExpr { mut is_mut := false diff --git a/vlib/v/checker/for.v b/vlib/v/checker/for.v index 22378aceb85baa..fa0b521b2fe76d 100644 --- a/vlib/v/checker/for.v +++ b/vlib/v/checker/for.v @@ -290,7 +290,7 @@ fn (mut c Checker) for_stmt(mut node ast.ForStmt) { if node.cond.op == .key_is { if node.cond.right is ast.TypeNode && node.cond.left in [ast.Ident, ast.SelectorExpr] { if c.table.type_kind(node.cond.left_type) in [.sum_type, .interface_] { - c.smartcast(node.cond.left, node.cond.left_type, node.cond.right_type, mut + c.smartcast(mut node.cond.left, node.cond.left_type, node.cond.right_type, mut node.scope) } } diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index 50ec92492d4f8b..e90a2d900c84c4 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -310,7 +310,7 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { } } else { // smartcast sumtypes and interfaces when using `is` - c.smartcast_if_conds(branch.cond, mut branch.scope) + c.smartcast_if_conds(mut branch.cond, mut branch.scope) if node_is_expr { c.stmts_ending_with_expression(mut branch.stmts) } else { @@ -465,11 +465,11 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { return node.typ } -fn (mut c Checker) smartcast_if_conds(node ast.Expr, mut scope ast.Scope) { - if node is ast.InfixExpr { +fn (mut c Checker) smartcast_if_conds(mut node ast.Expr, mut scope ast.Scope) { + if mut node is ast.InfixExpr { if node.op == .and { - c.smartcast_if_conds(node.left, mut scope) - c.smartcast_if_conds(node.right, mut scope) + c.smartcast_if_conds(mut node.left, mut scope) + c.smartcast_if_conds(mut node.right, mut scope) } else if node.op == .key_is { right_expr := node.right right_type := match right_expr { @@ -501,7 +501,7 @@ fn (mut c Checker) smartcast_if_conds(node ast.Expr, mut scope ast.Scope) { c.error('cannot use type `${expect_str}` as type `${expr_str}`', node.pos) } if node.left in [ast.Ident, ast.SelectorExpr] && node.right is ast.TypeNode { - is_variable := if node.left is ast.Ident { + is_variable := if mut node.left is ast.Ident { node.left.kind == .variable } else { true @@ -512,7 +512,7 @@ fn (mut c Checker) smartcast_if_conds(node ast.Expr, mut scope ast.Scope) { c.fail_if_immutable(node.left) } // TODO: Add check for sum types in a way that it doesn't break a lot of compiler code - if node.left is ast.Ident + if mut node.left is ast.Ident && (left_sym.kind == .interface_ && right_sym.kind != .interface_) { v := scope.find_var(node.left.name) or { &ast.Var{} } if v.is_mut && !node.left.is_mut { @@ -521,14 +521,15 @@ fn (mut c Checker) smartcast_if_conds(node ast.Expr, mut scope ast.Scope) { } } if left_sym.kind in [.interface_, .sum_type] { - c.smartcast(node.left, node.left_type, right_type, mut scope) + c.smartcast(mut node.left, node.left_type, right_type, mut + scope) } } } } } - } else if node is ast.Likely { - c.smartcast_if_conds(node.expr, mut scope) + } else if mut node is ast.Likely { + c.smartcast_if_conds(mut node.expr, mut scope) } } diff --git a/vlib/v/checker/match.v b/vlib/v/checker/match.v index 5647260cc637f2..c4efa01ab88e4e 100644 --- a/vlib/v/checker/match.v +++ b/vlib/v/checker/match.v @@ -357,7 +357,7 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, cond_type_sym ast.TypeSym expr_type = expr_types[0].typ } - c.smartcast(node.cond, node.cond_type, expr_type, mut branch.scope) + c.smartcast(mut node.cond, node.cond_type, expr_type, mut branch.scope) } } }