@@ -3216,50 +3216,50 @@ pub fn (mut c Checker) enum_decl(decl ast.EnumDecl) {
3216
3216
}
3217
3217
}
3218
3218
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) {
3220
3220
c.expected_type = ast.none_type // TODO a hack to make `x := if ... work`
3221
3221
defer {
3222
3222
c.expected_type = ast.void_type
3223
3223
}
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
3226
3226
mut right_type0 := ast.void_type
3227
- for i, right in assign_stmt .right {
3227
+ for i, right in node .right {
3228
3228
if right is ast.CallExpr || right is ast.IfExpr || right is ast.LockExpr
3229
3229
|| right is ast.MatchExpr {
3230
3230
right_type := c.expr (right)
3231
3231
if i == 0 {
3232
3232
right_type0 = right_type
3233
- assign_stmt .right_types = [
3233
+ node .right_types = [
3234
3234
c.check_expr_opt_call (right, right_type0 ),
3235
3235
]
3236
3236
}
3237
3237
right_type_sym := c.table.get_type_symbol (right_type)
3238
3238
if right_type_sym.kind == .multi_return {
3239
- if assign_stmt .right.len > 1 {
3239
+ if node .right.len > 1 {
3240
3240
c.error ('cannot use multi-value $right_type_sym.name in single-value context' ,
3241
3241
right.position ())
3242
3242
}
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
3245
3245
} else if right_type == ast.void_type {
3246
3246
right_len = 0
3247
3247
}
3248
3248
}
3249
3249
}
3250
- if assign_stmt .left.len != right_len {
3250
+ if node .left.len != right_len {
3251
3251
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)
3254
3254
} 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)
3257
3257
}
3258
3258
return
3259
3259
}
3260
3260
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 {
3263
3263
if left is ast.CallExpr {
3264
3264
c.error ('cannot call function `${left.name} ()` on the left side of an assignment' ,
3265
3265
left.pos)
@@ -3274,30 +3274,29 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
3274
3274
c.expected_type = c.unwrap_generic (left_type)
3275
3275
// `map = {}`
3276
3276
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 {
3278
3278
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{}
3281
3281
}
3282
3282
}
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
3284
3284
old_inside_ref_lit := c.inside_ref_lit
3285
3285
if left is ast.Ident {
3286
3286
if left.info is ast.IdentVar {
3287
3287
c.inside_ref_lit = c.inside_ref_lit || left.info.share == .shared_t
3288
3288
}
3289
3289
}
3290
3290
c.inside_decl_rhs = is_decl
3291
- right_type := c.expr (assign_stmt .right[i])
3291
+ right_type := c.expr (node .right[i])
3292
3292
c.inside_decl_rhs = false
3293
3293
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)
3297
3296
}
3298
3297
}
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]
3301
3300
if is_decl {
3302
3301
// check generic struct init and return unwrap generic struct type
3303
3302
if right is ast.StructInit {
@@ -3351,13 +3350,13 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
3351
3350
}
3352
3351
}
3353
3352
}
3354
- assign_stmt .left_types << left_type
3353
+ node .left_types << left_type
3355
3354
match mut left {
3356
3355
ast.Ident {
3357
3356
if left.kind == .blank_ident {
3358
3357
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] {
3361
3360
c.error ('cannot modify blank `_` identifier' , left.pos)
3362
3361
}
3363
3362
} else if left.info ! is ast.IdentVar {
@@ -3381,7 +3380,7 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
3381
3380
if ident_var_info.share == .atomic_t {
3382
3381
left_type = left_type.set_flag (.atomic_f)
3383
3382
}
3384
- assign_stmt .left_types[i] = left_type
3383
+ node .left_types[i] = left_type
3385
3384
ident_var_info.typ = left_type
3386
3385
left.info = ident_var_info
3387
3386
if left_type != 0 {
@@ -3418,7 +3417,7 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
3418
3417
if left.op == .mul {
3419
3418
if ! c.inside_unsafe {
3420
3419
c.error ('modifying variables via dereferencing can only be done in `unsafe` blocks' ,
3421
- assign_stmt .pos)
3420
+ node .pos)
3422
3421
} else {
3423
3422
// mark `p` in `*p = val` as used:
3424
3423
match mut left.right {
@@ -3463,28 +3462,28 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
3463
3462
// TODO replace all c.pref.translated checks with `$if !translated` for performance
3464
3463
continue
3465
3464
}
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]
3467
3466
&& right_sym.kind == .array && (left is ast.Ident && ! left.is_blank_ident ())
3468
3467
&& right is ast.Ident {
3469
3468
// 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)
3472
3471
}
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 ())
3475
3474
|| ! right_type.is_ptr ()) && ! left.is_blank_ident () && right.is_lvalue () {
3476
3475
// Do not allow `a = b`
3477
3476
c.error ('cannot copy map: call `move` or `clone` method (or use a reference)' ,
3478
3477
right.position ())
3479
3478
}
3480
3479
left_is_ptr := left_type.is_ptr () || left_sym.is_pointer ()
3481
3480
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] {
3483
3482
// 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)
3485
3484
}
3486
3485
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 () {
3488
3487
c.error ('cannot assign to `$left `: ' +
3489
3488
c.expected_msg (right_type_unwrapped, left_type_unwrapped), right.position ())
3490
3489
}
@@ -3496,69 +3495,69 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
3496
3495
rtype = rtype.deref ()
3497
3496
}
3498
3497
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)
3500
3499
}
3501
3500
}
3502
3501
// Single side check
3503
- match assign_stmt .op {
3502
+ match node .op {
3504
3503
.assign {} // No need to do single side check for =. But here put it first for speed.
3505
3504
.plus_assign, .minus_assign {
3506
3505
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 `' ,
3509
3508
left.position ())
3510
3509
}
3511
3510
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 ' ,
3513
3512
right.position ())
3514
3513
}
3515
3514
} else if ! left_sym.is_number ()
3516
3515
&& 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 `' ,
3518
3517
left.position ())
3519
3518
} else if ! right_sym.is_number ()
3520
3519
&& 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 ' ,
3522
3521
right.position ())
3523
3522
}
3524
3523
}
3525
3524
.mult_assign, .div_assign {
3526
3525
if ! left_sym.is_number ()
3527
3526
&& ! c.table.get_final_type_symbol (left_type_unwrapped).is_int ()
3528
3527
&& 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 `' ,
3530
3529
left.position ())
3531
3530
} else if ! right_sym.is_number ()
3532
3531
&& ! c.table.get_final_type_symbol (left_type_unwrapped).is_int ()
3533
3532
&& 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 `' ,
3535
3534
right.position ())
3536
3535
}
3537
3536
}
3538
3537
.and_assign, .or_assign, .xor_assign, .mod_assign, .left_shift_assign,
3539
3538
.right_shift_assign {
3540
3539
if ! left_sym.is_int ()
3541
3540
&& ! 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 `' ,
3543
3542
left.position ())
3544
3543
} else if ! right_sym.is_int ()
3545
3544
&& ! 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 `' ,
3547
3546
right.position ())
3548
3547
}
3549
3548
}
3550
3549
else {}
3551
3550
}
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]
3553
3552
&& ((left_sym.kind == .struct_ && right_sym.kind == .struct_)
3554
3553
|| left_sym.kind == .alias) {
3555
3554
left_name := c.table.type_to_str (left_type)
3556
3555
right_name := c.table.type_to_str (right_type)
3557
3556
parent_sym := c.table.get_final_type_symbol (left_type)
3558
3557
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)
3560
3559
}
3561
- extracted_op := match assign_stmt .op {
3560
+ extracted_op := match node .op {
3562
3561
.plus_assign { '+' }
3563
3562
.minus_assign { '-' }
3564
3563
.div_assign { '/' }
@@ -3569,18 +3568,18 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
3569
3568
if method := left_sym.find_method (extracted_op) {
3570
3569
if method.return_type != left_type {
3571
3570
c.error ('operator `$extracted_op ` must return `$left_name ` to be used as an assignment operator' ,
3572
- assign_stmt .pos)
3571
+ node .pos)
3573
3572
}
3574
3573
} else {
3575
3574
if parent_sym.is_primitive () {
3576
3575
c.error ('cannot use operator methods on type alias for `$parent_sym.name `' ,
3577
- assign_stmt .pos)
3576
+ node .pos)
3578
3577
}
3579
3578
if left_name == right_name {
3580
3579
c.error ('undefined operation `$left_name ` $extracted_op `$right_name `' ,
3581
- assign_stmt .pos)
3580
+ node .pos)
3582
3581
} 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)
3584
3583
}
3585
3584
}
3586
3585
}
@@ -3590,10 +3589,10 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
3590
3589
c.check_expected (right_type_unwrapped, left_type_unwrapped) or {
3591
3590
// allow for ptr += 2
3592
3591
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] {
3594
3593
if ! c.inside_unsafe {
3595
3594
c.warn ('pointer arithmetic is only allowed in `unsafe` blocks' ,
3596
- assign_stmt .pos)
3595
+ node .pos)
3597
3596
}
3598
3597
} else {
3599
3598
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) {
3608
3607
// so that ident.obj is set
3609
3608
// Check `x := &y` and `mut x := <-ch`
3610
3609
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 ]
3613
3612
if left_first is ast.Ident {
3614
3613
assigned_var := left_first
3615
3614
mut is_shared := false
3616
3615
if left_first.info is ast.IdentVar {
3617
3616
is_shared = left_first.info.share == .shared_t
3618
3617
}
3619
3618
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)
3622
3621
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
3627
3626
right_type0 = v.typ
3628
3627
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)
3631
3630
}
3632
- } else if node .right.obj is ast.ConstField {
3631
+ } else if right_node .right.obj is ast.ConstField {
3633
3632
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)
3636
3635
}
3637
3636
}
3638
3637
}
3639
3638
}
3640
- if node .op == .arrow {
3639
+ if right_node .op == .arrow {
3641
3640
if assigned_var.is_mut {
3642
3641
right_sym := c.table.get_type_symbol (right_type0 )
3643
3642
if right_sym.kind == .chan {
3644
3643
chan_info := right_sym.chan_info ()
3645
3644
if chan_info.elem_type.is_ptr () && ! chan_info.is_mut {
3646
3645
c.error ('cannot have a mutable reference to object from `$right_sym.name `' ,
3647
- node .pos)
3646
+ right_node .pos)
3648
3647
}
3649
3648
}
3650
3649
}
3651
3650
}
3652
3651
}
3653
3652
}
3654
- // right_sym := c.table.get_type_symbol(right_type_unwrapped)
3655
3653
}
3656
3654
3657
3655
fn scope_register_it (mut s ast.Scope, pos token.Position, typ ast.Type) {
0 commit comments