Skip to content

Commit fedf07d

Browse files
authored
checker: minor cleanup of assign_stmt() (#10314)
1 parent 86d70fa commit fedf07d

File tree

1 file changed

+72
-74
lines changed

1 file changed

+72
-74
lines changed

vlib/v/checker/checker.v

Lines changed: 72 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3216,50 +3216,50 @@ pub fn (mut c Checker) enum_decl(decl ast.EnumDecl) {
32163216
}
32173217
}
32183218

3219-
pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
3219+
pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
32203220
c.expected_type = ast.none_type // TODO a hack to make `x := if ... work`
32213221
defer {
32223222
c.expected_type = ast.void_type
32233223
}
3224-
right_first := assign_stmt.right[0]
3225-
mut right_len := assign_stmt.right.len
3224+
right_first := node.right[0]
3225+
mut right_len := node.right.len
32263226
mut right_type0 := ast.void_type
3227-
for i, right in assign_stmt.right {
3227+
for i, right in node.right {
32283228
if right is ast.CallExpr || right is ast.IfExpr || right is ast.LockExpr
32293229
|| right is ast.MatchExpr {
32303230
right_type := c.expr(right)
32313231
if i == 0 {
32323232
right_type0 = right_type
3233-
assign_stmt.right_types = [
3233+
node.right_types = [
32343234
c.check_expr_opt_call(right, right_type0),
32353235
]
32363236
}
32373237
right_type_sym := c.table.get_type_symbol(right_type)
32383238
if right_type_sym.kind == .multi_return {
3239-
if assign_stmt.right.len > 1 {
3239+
if node.right.len > 1 {
32403240
c.error('cannot use multi-value $right_type_sym.name in single-value context',
32413241
right.position())
32423242
}
3243-
assign_stmt.right_types = right_type_sym.mr_info().types
3244-
right_len = assign_stmt.right_types.len
3243+
node.right_types = right_type_sym.mr_info().types
3244+
right_len = node.right_types.len
32453245
} else if right_type == ast.void_type {
32463246
right_len = 0
32473247
}
32483248
}
32493249
}
3250-
if assign_stmt.left.len != right_len {
3250+
if node.left.len != right_len {
32513251
if right_first is ast.CallExpr {
3252-
c.error('assignment mismatch: $assign_stmt.left.len variable(s) but `${right_first.name}()` returns $right_len value(s)',
3253-
assign_stmt.pos)
3252+
c.error('assignment mismatch: $node.left.len variable(s) but `${right_first.name}()` returns $right_len value(s)',
3253+
node.pos)
32543254
} else {
3255-
c.error('assignment mismatch: $assign_stmt.left.len variable(s) $right_len value(s)',
3256-
assign_stmt.pos)
3255+
c.error('assignment mismatch: $node.left.len variable(s) $right_len value(s)',
3256+
node.pos)
32573257
}
32583258
return
32593259
}
32603260

3261-
is_decl := assign_stmt.op == .decl_assign
3262-
for i, left in assign_stmt.left {
3261+
is_decl := node.op == .decl_assign
3262+
for i, left in node.left {
32633263
if left is ast.CallExpr {
32643264
c.error('cannot call function `${left.name}()` on the left side of an assignment',
32653265
left.pos)
@@ -3274,30 +3274,29 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
32743274
c.expected_type = c.unwrap_generic(left_type)
32753275
// `map = {}`
32763276
sym := c.table.get_type_symbol(left_type)
3277-
if sym.kind == .map && assign_stmt.right[i] is ast.StructInit {
3277+
if sym.kind == .map && node.right[i] is ast.StructInit {
32783278
c.warn('assigning a struct literal to a map is deprecated - use `map{}` instead',
3279-
assign_stmt.right[i].position())
3280-
assign_stmt.right[i] = ast.MapInit{}
3279+
node.right[i].position())
3280+
node.right[i] = ast.MapInit{}
32813281
}
32823282
}
3283-
if assign_stmt.right_types.len < assign_stmt.left.len { // first type or multi return types added above
3283+
if node.right_types.len < node.left.len { // first type or multi return types added above
32843284
old_inside_ref_lit := c.inside_ref_lit
32853285
if left is ast.Ident {
32863286
if left.info is ast.IdentVar {
32873287
c.inside_ref_lit = c.inside_ref_lit || left.info.share == .shared_t
32883288
}
32893289
}
32903290
c.inside_decl_rhs = is_decl
3291-
right_type := c.expr(assign_stmt.right[i])
3291+
right_type := c.expr(node.right[i])
32923292
c.inside_decl_rhs = false
32933293
c.inside_ref_lit = old_inside_ref_lit
3294-
if assign_stmt.right_types.len == i {
3295-
assign_stmt.right_types << c.check_expr_opt_call(assign_stmt.right[i],
3296-
right_type)
3294+
if node.right_types.len == i {
3295+
node.right_types << c.check_expr_opt_call(node.right[i], right_type)
32973296
}
32983297
}
3299-
right := if i < assign_stmt.right.len { assign_stmt.right[i] } else { assign_stmt.right[0] }
3300-
mut right_type := assign_stmt.right_types[i]
3298+
right := if i < node.right.len { node.right[i] } else { node.right[0] }
3299+
mut right_type := node.right_types[i]
33013300
if is_decl {
33023301
// check generic struct init and return unwrap generic struct type
33033302
if right is ast.StructInit {
@@ -3351,13 +3350,13 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
33513350
}
33523351
}
33533352
}
3354-
assign_stmt.left_types << left_type
3353+
node.left_types << left_type
33553354
match mut left {
33563355
ast.Ident {
33573356
if left.kind == .blank_ident {
33583357
left_type = right_type
3359-
assign_stmt.left_types[i] = right_type
3360-
if assign_stmt.op !in [.assign, .decl_assign] {
3358+
node.left_types[i] = right_type
3359+
if node.op !in [.assign, .decl_assign] {
33613360
c.error('cannot modify blank `_` identifier', left.pos)
33623361
}
33633362
} else if left.info !is ast.IdentVar {
@@ -3381,7 +3380,7 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
33813380
if ident_var_info.share == .atomic_t {
33823381
left_type = left_type.set_flag(.atomic_f)
33833382
}
3384-
assign_stmt.left_types[i] = left_type
3383+
node.left_types[i] = left_type
33853384
ident_var_info.typ = left_type
33863385
left.info = ident_var_info
33873386
if left_type != 0 {
@@ -3418,7 +3417,7 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
34183417
if left.op == .mul {
34193418
if !c.inside_unsafe {
34203419
c.error('modifying variables via dereferencing can only be done in `unsafe` blocks',
3421-
assign_stmt.pos)
3420+
node.pos)
34223421
} else {
34233422
// mark `p` in `*p = val` as used:
34243423
match mut left.right {
@@ -3463,28 +3462,28 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
34633462
// TODO replace all c.pref.translated checks with `$if !translated` for performance
34643463
continue
34653464
}
3466-
if left_sym.kind == .array && !c.inside_unsafe && assign_stmt.op in [.assign, .decl_assign]
3465+
if left_sym.kind == .array && !c.inside_unsafe && node.op in [.assign, .decl_assign]
34673466
&& right_sym.kind == .array && (left is ast.Ident && !left.is_blank_ident())
34683467
&& right is ast.Ident {
34693468
// Do not allow `a = b`, only `a = b.clone()`
3470-
c.error('use `array2 $assign_stmt.op.str() array1.clone()` instead of `array2 $assign_stmt.op.str() array1` (or use `unsafe`)',
3471-
assign_stmt.pos)
3469+
c.error('use `array2 $node.op.str() array1.clone()` instead of `array2 $node.op.str() array1` (or use `unsafe`)',
3470+
node.pos)
34723471
}
3473-
if left_sym.kind == .map && assign_stmt.op in [.assign, .decl_assign]
3474-
&& right_sym.kind == .map && ((right is ast.Ident && right.is_auto_deref_var())
3472+
if left_sym.kind == .map && node.op in [.assign, .decl_assign] && right_sym.kind == .map
3473+
&& ((right is ast.Ident && right.is_auto_deref_var())
34753474
|| !right_type.is_ptr()) && !left.is_blank_ident() && right.is_lvalue() {
34763475
// Do not allow `a = b`
34773476
c.error('cannot copy map: call `move` or `clone` method (or use a reference)',
34783477
right.position())
34793478
}
34803479
left_is_ptr := left_type.is_ptr() || left_sym.is_pointer()
34813480
if left_is_ptr && !left.is_auto_deref_var() {
3482-
if !c.inside_unsafe && assign_stmt.op !in [.assign, .decl_assign] {
3481+
if !c.inside_unsafe && node.op !in [.assign, .decl_assign] {
34833482
// ptr op=
3484-
c.warn('pointer arithmetic is only allowed in `unsafe` blocks', assign_stmt.pos)
3483+
c.warn('pointer arithmetic is only allowed in `unsafe` blocks', node.pos)
34853484
}
34863485
right_is_ptr := right_type.is_ptr() || right_sym.is_pointer()
3487-
if !right_is_ptr && assign_stmt.op == .assign && right_type_unwrapped.is_number() {
3486+
if !right_is_ptr && node.op == .assign && right_type_unwrapped.is_number() {
34883487
c.error('cannot assign to `$left`: ' +
34893488
c.expected_msg(right_type_unwrapped, left_type_unwrapped), right.position())
34903489
}
@@ -3496,69 +3495,69 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
34963495
rtype = rtype.deref()
34973496
}
34983497
right_name := c.table.type_to_str(rtype)
3499-
c.error('mismatched types `$left_name` and `$right_name`', assign_stmt.pos)
3498+
c.error('mismatched types `$left_name` and `$right_name`', node.pos)
35003499
}
35013500
}
35023501
// Single side check
3503-
match assign_stmt.op {
3502+
match node.op {
35043503
.assign {} // No need to do single side check for =. But here put it first for speed.
35053504
.plus_assign, .minus_assign {
35063505
if left_type == ast.string_type {
3507-
if assign_stmt.op != .plus_assign {
3508-
c.error('operator `$assign_stmt.op` not defined on left operand type `$left_sym.name`',
3506+
if node.op != .plus_assign {
3507+
c.error('operator `$node.op` not defined on left operand type `$left_sym.name`',
35093508
left.position())
35103509
}
35113510
if right_type != ast.string_type {
3512-
c.error('invalid right operand: $left_sym.name $assign_stmt.op $right_sym.name',
3511+
c.error('invalid right operand: $left_sym.name $node.op $right_sym.name',
35133512
right.position())
35143513
}
35153514
} else if !left_sym.is_number()
35163515
&& left_sym.kind !in [.byteptr, .charptr, .struct_, .alias] {
3517-
c.error('operator `$assign_stmt.op` not defined on left operand type `$left_sym.name`',
3516+
c.error('operator `$node.op` not defined on left operand type `$left_sym.name`',
35183517
left.position())
35193518
} else if !right_sym.is_number()
35203519
&& left_sym.kind !in [.byteptr, .charptr, .struct_, .alias] {
3521-
c.error('invalid right operand: $left_sym.name $assign_stmt.op $right_sym.name',
3520+
c.error('invalid right operand: $left_sym.name $node.op $right_sym.name',
35223521
right.position())
35233522
}
35243523
}
35253524
.mult_assign, .div_assign {
35263525
if !left_sym.is_number()
35273526
&& !c.table.get_final_type_symbol(left_type_unwrapped).is_int()
35283527
&& left_sym.kind !in [.struct_, .alias] {
3529-
c.error('operator $assign_stmt.op.str() not defined on left operand type `$left_sym.name`',
3528+
c.error('operator $node.op.str() not defined on left operand type `$left_sym.name`',
35303529
left.position())
35313530
} else if !right_sym.is_number()
35323531
&& !c.table.get_final_type_symbol(left_type_unwrapped).is_int()
35333532
&& left_sym.kind !in [.struct_, .alias] {
3534-
c.error('operator $assign_stmt.op.str() not defined on right operand type `$right_sym.name`',
3533+
c.error('operator $node.op.str() not defined on right operand type `$right_sym.name`',
35353534
right.position())
35363535
}
35373536
}
35383537
.and_assign, .or_assign, .xor_assign, .mod_assign, .left_shift_assign,
35393538
.right_shift_assign {
35403539
if !left_sym.is_int()
35413540
&& !c.table.get_final_type_symbol(left_type_unwrapped).is_int() {
3542-
c.error('operator $assign_stmt.op.str() not defined on left operand type `$left_sym.name`',
3541+
c.error('operator $node.op.str() not defined on left operand type `$left_sym.name`',
35433542
left.position())
35443543
} else if !right_sym.is_int()
35453544
&& !c.table.get_final_type_symbol(right_type_unwrapped).is_int() {
3546-
c.error('operator $assign_stmt.op.str() not defined on right operand type `$right_sym.name`',
3545+
c.error('operator $node.op.str() not defined on right operand type `$right_sym.name`',
35473546
right.position())
35483547
}
35493548
}
35503549
else {}
35513550
}
3552-
if assign_stmt.op in [.plus_assign, .minus_assign, .mod_assign, .mult_assign, .div_assign]
3551+
if node.op in [.plus_assign, .minus_assign, .mod_assign, .mult_assign, .div_assign]
35533552
&& ((left_sym.kind == .struct_ && right_sym.kind == .struct_)
35543553
|| left_sym.kind == .alias) {
35553554
left_name := c.table.type_to_str(left_type)
35563555
right_name := c.table.type_to_str(right_type)
35573556
parent_sym := c.table.get_final_type_symbol(left_type)
35583557
if left_sym.kind == .alias && right_sym.kind != .alias {
3559-
c.error('mismatched types `$left_name` and `$right_name`', assign_stmt.pos)
3558+
c.error('mismatched types `$left_name` and `$right_name`', node.pos)
35603559
}
3561-
extracted_op := match assign_stmt.op {
3560+
extracted_op := match node.op {
35623561
.plus_assign { '+' }
35633562
.minus_assign { '-' }
35643563
.div_assign { '/' }
@@ -3569,18 +3568,18 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
35693568
if method := left_sym.find_method(extracted_op) {
35703569
if method.return_type != left_type {
35713570
c.error('operator `$extracted_op` must return `$left_name` to be used as an assignment operator',
3572-
assign_stmt.pos)
3571+
node.pos)
35733572
}
35743573
} else {
35753574
if parent_sym.is_primitive() {
35763575
c.error('cannot use operator methods on type alias for `$parent_sym.name`',
3577-
assign_stmt.pos)
3576+
node.pos)
35783577
}
35793578
if left_name == right_name {
35803579
c.error('undefined operation `$left_name` $extracted_op `$right_name`',
3581-
assign_stmt.pos)
3580+
node.pos)
35823581
} else {
3583-
c.error('mismatched types `$left_name` and `$right_name`', assign_stmt.pos)
3582+
c.error('mismatched types `$left_name` and `$right_name`', node.pos)
35843583
}
35853584
}
35863585
}
@@ -3590,10 +3589,10 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
35903589
c.check_expected(right_type_unwrapped, left_type_unwrapped) or {
35913590
// allow for ptr += 2
35923591
if left_type_unwrapped.is_ptr() && right_type_unwrapped.is_int()
3593-
&& assign_stmt.op in [.plus_assign, .minus_assign] {
3592+
&& node.op in [.plus_assign, .minus_assign] {
35943593
if !c.inside_unsafe {
35953594
c.warn('pointer arithmetic is only allowed in `unsafe` blocks',
3596-
assign_stmt.pos)
3595+
node.pos)
35973596
}
35983597
} else {
35993598
c.error('cannot assign to `$left`: $err.msg', right.position())
@@ -3608,50 +3607,49 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
36083607
// so that ident.obj is set
36093608
// Check `x := &y` and `mut x := <-ch`
36103609
if right_first is ast.PrefixExpr {
3611-
node := right_first
3612-
left_first := assign_stmt.left[0]
3610+
right_node := right_first
3611+
left_first := node.left[0]
36133612
if left_first is ast.Ident {
36143613
assigned_var := left_first
36153614
mut is_shared := false
36163615
if left_first.info is ast.IdentVar {
36173616
is_shared = left_first.info.share == .shared_t
36183617
}
36193618
old_inside_ref_lit := c.inside_ref_lit
3620-
c.inside_ref_lit = (c.inside_ref_lit || node.op == .amp || is_shared)
3621-
c.expr(node.right)
3619+
c.inside_ref_lit = (c.inside_ref_lit || right_node.op == .amp || is_shared)
3620+
c.expr(right_node.right)
36223621
c.inside_ref_lit = old_inside_ref_lit
3623-
if node.op == .amp {
3624-
if node.right is ast.Ident {
3625-
if node.right.obj is ast.Var {
3626-
v := node.right.obj
3622+
if right_node.op == .amp {
3623+
if right_node.right is ast.Ident {
3624+
if right_node.right.obj is ast.Var {
3625+
v := right_node.right.obj
36273626
right_type0 = v.typ
36283627
if !v.is_mut && assigned_var.is_mut && !c.inside_unsafe {
3629-
c.error('`$node.right.name` is immutable, cannot have a mutable reference to it',
3630-
node.pos)
3628+
c.error('`$right_node.right.name` is immutable, cannot have a mutable reference to it',
3629+
right_node.pos)
36313630
}
3632-
} else if node.right.obj is ast.ConstField {
3631+
} else if right_node.right.obj is ast.ConstField {
36333632
if assigned_var.is_mut && !c.inside_unsafe {
3634-
c.error('`$node.right.name` is immutable, cannot have a mutable reference to it',
3635-
node.pos)
3633+
c.error('`$right_node.right.name` is immutable, cannot have a mutable reference to it',
3634+
right_node.pos)
36363635
}
36373636
}
36383637
}
36393638
}
3640-
if node.op == .arrow {
3639+
if right_node.op == .arrow {
36413640
if assigned_var.is_mut {
36423641
right_sym := c.table.get_type_symbol(right_type0)
36433642
if right_sym.kind == .chan {
36443643
chan_info := right_sym.chan_info()
36453644
if chan_info.elem_type.is_ptr() && !chan_info.is_mut {
36463645
c.error('cannot have a mutable reference to object from `$right_sym.name`',
3647-
node.pos)
3646+
right_node.pos)
36483647
}
36493648
}
36503649
}
36513650
}
36523651
}
36533652
}
3654-
// right_sym := c.table.get_type_symbol(right_type_unwrapped)
36553653
}
36563654

36573655
fn scope_register_it(mut s ast.Scope, pos token.Position, typ ast.Type) {

0 commit comments

Comments
 (0)