/
assign.v
835 lines (826 loc) · 29.6 KB
/
assign.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
module checker
import v.ast
import v.pref
// TODO 600 line function
fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
prev_inside_assign := c.inside_assign
c.inside_assign = true
c.expected_type = ast.none_type // TODO a hack to make `x := if ... work`
defer {
c.expected_type = ast.void_type
c.inside_assign = prev_inside_assign
}
is_decl := node.op == .decl_assign
mut right_first := node.right[0]
node.left_types = []
mut right_len := node.right.len
mut right_first_type := ast.void_type
for i, mut right in node.right {
if right in [ast.CallExpr, ast.IfExpr, ast.LockExpr, ast.MatchExpr, ast.DumpExpr,
ast.SelectorExpr, ast.ParExpr] {
if right in [ast.IfExpr, ast.MatchExpr] && node.left.len == node.right.len && !is_decl
&& node.left[i] in [ast.Ident, ast.SelectorExpr] && !node.left[i].is_blank_ident() {
mut expr := node.left[i]
c.expected_type = c.expr(mut expr)
}
mut right_type := c.expr(mut right)
if right in [ast.CallExpr, ast.IfExpr, ast.LockExpr, ast.MatchExpr, ast.DumpExpr] {
c.fail_if_unreadable(right, right_type, 'right-hand side of assignment')
}
right_type_sym := c.table.sym(right_type)
// fixed array returns an struct, but when assigning it must be the array type
right_type = c.cast_fixed_array_ret(right_type, right_type_sym)
if i == 0 {
right_first_type = right_type
node.right_types = [
c.check_expr_opt_call(right, right_first_type),
]
}
if right_type_sym.kind == .multi_return {
if node.right.len > 1 {
c.error('cannot use multi-value ${right_type_sym.name} in single-value context',
right.pos())
}
node.right_types = right_type_sym.mr_info().types
right_len = node.right_types.len
} else if right_type == ast.void_type {
right_len = 0
}
}
if mut right is ast.InfixExpr {
if right.op == .arrow {
c.error('cannot use `<-` on the right-hand side of an assignment, as it does not return any values',
right.pos)
}
}
if mut right is ast.Ident {
if right.is_mut {
c.error('unexpected `mut` on right-hand side of assignment', right.mut_pos)
}
}
if is_decl && mut right is ast.None {
c.error('cannot assign a `none` value to a variable', right.pos)
}
// Handle `left_name := unsafe { none }`
if mut right is ast.UnsafeExpr {
if mut right.expr is ast.None {
c.error('cannot use `none` in `unsafe` blocks', right.expr.pos)
}
}
if mut right is ast.AnonFn {
if right.decl.generic_names.len > 0 {
c.error('cannot assign generic function to a variable', right.decl.pos)
}
}
}
if node.left.len != right_len {
if mut right_first is ast.CallExpr {
if node.left_types.len > 0 && node.left_types[0] == ast.void_type {
// If it's a void type, it's an unknown variable, already had an error earlier.
return
}
c.error('assignment mismatch: ${node.left.len} variable(s) but `${right_first.get_name()}()` returns ${right_len} value(s)',
node.pos)
} else if mut right_first is ast.ParExpr {
mut right_next := right_first
for {
if mut right_next.expr is ast.CallExpr {
if right_next.expr.return_type == ast.void_type {
c.error('assignment mismatch: expected ${node.left.len} value(s) but `${right_next.expr.get_name()}()` returns ${right_len} value(s)',
node.pos)
}
break
} else if mut right_next.expr is ast.ParExpr {
right_next = right_next.expr
} else {
break
}
}
} else {
c.error('assignment mismatch: ${node.left.len} variable(s) ${right_len} value(s)',
node.pos)
}
return
}
for i, mut left in node.left {
if mut left is ast.CallExpr {
// ban `foo() = 10`
c.error('cannot call function `${left.name}()` on the left side of an assignment',
left.pos)
} else if mut left is ast.PrefixExpr {
// ban `*foo() = 10`
if left.right is ast.CallExpr && left.op == .mul {
c.error('cannot dereference a function call on the left side of an assignment, use a temporary variable',
left.pos)
}
} else if mut left is ast.IndexExpr {
if left.index is ast.RangeExpr {
c.error('cannot reassign using range expression on the left side of an assignment',
left.pos)
}
}
is_blank_ident := left.is_blank_ident()
mut left_type := ast.void_type
mut var_option := false
if !is_decl && !is_blank_ident {
if left in [ast.Ident, ast.SelectorExpr] {
c.prevent_sum_type_unwrapping_once = true
}
if left is ast.IndexExpr {
c.is_index_assign = true
}
left_type = c.expr(mut left)
c.is_index_assign = false
c.expected_type = c.unwrap_generic(left_type)
}
if c.inside_comptime_for_field && mut left is ast.ComptimeSelector {
left_type = c.comptime_fields_default_type
c.expected_type = c.unwrap_generic(left_type)
}
if node.right_types.len < node.left.len { // first type or multi return types added above
old_inside_ref_lit := c.inside_ref_lit
if mut left is ast.Ident {
if mut left.info is ast.IdentVar {
c.inside_ref_lit = c.inside_ref_lit || left.info.share == .shared_t
}
}
c.inside_decl_rhs = is_decl
mut expr := node.right[i]
right_type := c.expr(mut expr)
c.inside_decl_rhs = false
c.inside_ref_lit = old_inside_ref_lit
if node.right_types.len == i {
node.right_types << c.check_expr_opt_call(node.right[i], right_type)
}
}
mut right := if i < node.right.len { node.right[i] } else { node.right[0] }
mut right_type := node.right_types[i]
if mut right is ast.Ident {
// resolve shared right vairable
if right_type.has_flag(.shared_f) {
if c.fail_if_unreadable(right, right_type, 'right-hand side of assignment') {
return
}
}
right_sym := c.table.sym(right_type)
if right_sym.info is ast.Struct {
if right_sym.info.generic_types.len > 0 {
if obj := right.scope.find(right.name) {
right_type = obj.typ
}
}
}
if right.or_expr.kind in [.propagate_option, .block] {
right_type = right_type.clear_flag(.option)
}
} else if right is ast.ComptimeSelector {
right_type = c.comptime_fields_default_type
}
if is_decl {
// check generic struct init and return unwrap generic struct type
if mut right is ast.StructInit {
if right.typ.has_flag(.generic) {
c.expr(mut right)
right_type = right.typ
}
} else if mut right is ast.PrefixExpr {
if right.op == .amp && right.right is ast.StructInit {
right_type = c.expr(mut right)
} else if right.op == .arrow {
right_type = c.expr(mut right)
right_type = c.cast_fixed_array_ret(right_type, c.table.sym(right_type))
}
} else if mut right is ast.Ident {
if right.kind == .function {
c.expr(mut right)
}
}
if right.is_auto_deref_var() {
left_type = ast.mktyp(right_type.deref())
} else {
left_type = ast.mktyp(right_type)
}
if left_type == ast.int_type {
if mut right is ast.IntegerLiteral {
mut is_large := right.val.len > 13
if !is_large && right.val.len > 8 {
val := right.val.i64()
is_large = val > int_max || val < int_min
}
if is_large {
c.error('overflow in implicit type `int`, use explicit type casting instead',
right.pos)
}
}
}
if mut left is ast.Ident && mut right is ast.Ident {
if !c.inside_unsafe && left_type.is_ptr() && left.is_mut() && right_type.is_ptr()
&& !right.is_mut() {
c.error('`${right.name}` is immutable, cannot have a mutable reference to an immutable object',
right.pos)
}
}
} else {
// Make sure the variable is mutable
c.fail_if_immutable(mut left)
if !is_blank_ident && !left_type.has_flag(.option) && right_type.has_flag(.option) {
c.error('cannot assign an Option value to a non-option variable', right.pos())
}
// left_type = c.expr(left)
// if right is ast.None && !left_type.has_flag(.option) {
// println(left_type)
// c.error('cannot assign a `none` value to a non-option variable', right.pos())
// }
}
if mut left is ast.Ident && left.info is ast.IdentVar && right is ast.Ident
&& right.name in c.global_names {
ident_var_info := left.info as ast.IdentVar
if ident_var_info.share == .shared_t {
c.error('cannot assign global variable to shared variable', right.pos())
}
}
if right_type.is_ptr() && left_type.is_ptr() {
if mut right is ast.Ident {
c.fail_if_stack_struct_action_outside_unsafe(mut right, 'assigned')
}
}
// Do not allow `a := 0; b := 0; a = &b`
if !is_decl && left is ast.Ident && !is_blank_ident && !left_type.is_any_kind_of_pointer()
&& right_type.is_any_kind_of_pointer() && !right_type.has_flag(.shared_f) {
left_sym := c.table.sym(left_type)
if left_sym.kind !in [.function, .array] {
c.warn(
'cannot assign a reference to a value (this will be an error soon) left=${c.table.type_str(left_type)} ${left_type.is_ptr()} ' +
'right=${c.table.type_str(right_type)} ${right_type.is_any_kind_of_pointer()} ptr=${right_type.is_ptr()}',
node.pos)
}
}
node.left_types << left_type
match mut left {
ast.Ident {
if (is_decl || left.kind == .blank_ident) && left_type.is_ptr()
&& mut right is ast.PrefixExpr && right.right_type == ast.int_literal_type_idx {
if mut right.right is ast.Ident && right.right.obj is ast.ConstField {
const_name := right.right.name.all_after_last('.')
const_val := (right.right.obj as ast.ConstField).expr
c.add_error_detail('Specify the type for the constant value. Example:')
c.add_error_detail(' `const ${const_name} = int(${const_val})`')
c.error('cannot assign a pointer to a constant with an integer literal value',
right.right.pos)
}
} else if left.kind == .blank_ident {
left_type = right_type
node.left_types[i] = right_type
if node.op !in [.assign, .decl_assign] {
c.error('cannot modify blank `_` identifier', left.pos)
}
} else if left.info !is ast.IdentVar {
c.error('cannot assign to ${left.kind} `${left.name}`', left.pos)
} else {
if is_decl {
c.check_valid_snake_case(left.name, 'variable name', left.pos)
if reserved_type_names_chk.matches(left.name) {
c.error('invalid use of reserved type `${left.name}` as a variable name',
left.pos)
}
if right is ast.Nil && !c.inside_unsafe {
// `x := unsafe { nil }` is allowed,
// as well as:
// `unsafe {
// x := nil
// println(x)
// }`
c.error('use of untyped nil in assignment (use `unsafe` | ${c.inside_unsafe})',
right.pos())
}
}
mut ident_var_info := left.info as ast.IdentVar
if ident_var_info.share == .shared_t {
left_type = left_type.set_flag(.shared_f)
if is_decl {
if left_type.nr_muls() > 1 {
c.error('shared cannot be multi level reference', left.pos)
}
left_type = left_type.set_nr_muls(1)
}
} else if left_type.has_flag(.shared_f) {
left_type = left_type.clear_flag(.shared_f).deref()
}
if ident_var_info.share == .atomic_t {
left_type = left_type.set_flag(.atomic_f)
}
if ident_var_info.is_option {
var_option = true
}
node.left_types[i] = left_type
ident_var_info.typ = left_type
left.info = ident_var_info
if left_type != 0 {
match mut left.obj {
ast.Var {
left.obj.typ = left_type
if left.obj.is_auto_deref {
left.obj.is_used = true
}
if !left_type.is_ptr() {
if c.table.sym(left_type).is_heap() {
left.obj.is_auto_heap = true
}
}
if left_type in ast.unsigned_integer_type_idxs {
if mut right is ast.IntegerLiteral {
if right.val[0] == `-` {
c.error('cannot assign negative value to unsigned integer type',
right.pos)
}
}
}
if right is ast.ComptimeSelector {
if is_decl {
left.obj.ct_type_var = .field_var
left.obj.typ = c.comptime_fields_default_type
}
} else if mut right is ast.Ident && right.obj is ast.Var
&& right.or_expr.kind == .absent {
if (right.obj as ast.Var).ct_type_var != .no_comptime {
ctyp := c.get_comptime_var_type(right)
if ctyp != ast.void_type {
left.obj.ct_type_var = (right.obj as ast.Var).ct_type_var
left.obj.typ = ctyp
}
}
} else if right is ast.DumpExpr
&& right.expr is ast.ComptimeSelector {
left.obj.ct_type_var = .field_var
left.obj.typ = c.comptime_fields_default_type
}
}
ast.GlobalField {
left.obj.typ = left_type
}
else {}
}
}
if is_decl {
full_name := '${left.mod}.${left.name}'
if obj := c.file.global_scope.find(full_name) {
if obj is ast.ConstField {
c.warn('duplicate of a const name `${full_name}`', left.pos)
}
}
if left.name == left.mod && left.name != 'main' {
c.add_error_detail('Module name duplicates will become errors after 2023/10/31.')
c.note('duplicate of a module name `${left.name}`', left.pos)
}
// Check if variable name is already registered as imported module symbol
if c.check_import_sym_conflict(left.name) {
c.error('duplicate of an import symbol `${left.name}`', left.pos)
}
}
}
}
ast.PrefixExpr {
// Do now allow `*x = y` outside `unsafe`
if left.op == .mul {
if !c.inside_unsafe && !c.pref.translated && !c.file.is_translated {
c.error('modifying variables via dereferencing can only be done in `unsafe` blocks',
node.pos)
} else if mut left.right is ast.Ident {
// mark `p` in `*p = val` as used:
if mut left.right.obj is ast.Var {
left.right.obj.is_used = true
}
}
} else if left.op == .amp {
c.error('cannot use a reference on the left side of `${node.op}`',
left.pos)
} else {
c.error('cannot use `${left.op}` on the left of `${node.op}`', left.pos)
}
if is_decl {
c.error('non-name on the left side of `:=`', left.pos)
}
}
ast.SelectorExpr {
if mut left.expr is ast.IndexExpr {
if left.expr.is_map {
left.expr.is_setter = true
}
}
if left_type in ast.unsigned_integer_type_idxs {
if mut right is ast.IntegerLiteral {
if right.val[0] == `-` {
c.error('cannot assign negative value to unsigned integer type',
right.pos)
}
}
}
}
else {
if mut left is ast.IndexExpr {
// eprintln('>>> left.is_setter: ${left.is_setter:10} | left.is_map: ${left.is_map:10} | left.is_array: ${left.is_array:10}')
if left.is_map && left.is_setter {
left.recursive_mapset_is_setter(true)
}
}
if is_decl {
c.error('non-name `${left}` on left side of `:=`', left.pos())
}
if node.op == .assign && (left.is_literal() || left is ast.StructInit) {
c.error('non-name literal value `${left}` on left side of `=`', left.pos())
}
}
}
left_type_unwrapped := c.unwrap_generic(ast.mktyp(left_type))
right_type_unwrapped := c.unwrap_generic(right_type)
if right_type_unwrapped == 0 {
// right type was a generic `T`
continue
}
if c.pref.translated || c.file.is_translated {
// TODO fix this in C2V instead, for example cast enums to int before using `|` on them.
// TODO replace all c.pref.translated checks with `$if !translated` for performance
continue
}
if left_type_unwrapped == 0 {
continue
}
left_sym := c.table.sym(left_type_unwrapped)
right_sym := c.table.sym(right_type_unwrapped)
old_assign_error_condition := left_sym.kind == .array && !c.inside_unsafe
&& node.op in [.assign, .decl_assign] && right_sym.kind == .array && left is ast.Ident
&& !left.is_blank_ident() && right is ast.Ident
if old_assign_error_condition {
// Do not allow `a = b`, only `a = b.clone()`
c.error('use `array2 ${node.op.str()} array1.clone()` instead of `array2 ${node.op.str()} array1` (or use `unsafe`)',
node.pos)
}
// Do not allow `a = val.array_field`, only `a = val.array_field.clone()`
// TODO: turn this warning into an error after 2022/09/24
// TODO: and remove the less strict check from above.
if left_sym.kind == .array && !c.inside_unsafe && right_sym.kind == .array
&& left is ast.Ident && !left.is_blank_ident() && right in [ast.Ident, ast.SelectorExpr]
&& ((node.op == .decl_assign && left.is_mut) || node.op == .assign) {
// no point to show the notice, if the old error was already shown:
if !old_assign_error_condition {
mut_str := if node.op == .decl_assign { 'mut ' } else { '' }
c.warn('use `${mut_str}array2 ${node.op.str()} array1.clone()` instead of `${mut_str}array2 ${node.op.str()} array1` (or use `unsafe`)',
node.pos)
}
}
if left_sym.kind == .array && right_sym.kind == .array {
right_info := right_sym.info as ast.Array
right_elem_type := c.table.unaliased_type(right_info.elem_type)
if node.op in [.decl_assign, .assign] {
// Do not allow `mut arr := [&immutable_object]`
if mut left is ast.Ident && right_elem_type.is_ptr() {
if left.is_mut() || (left.obj is ast.Var && left.obj.is_mut) {
if mut right is ast.ArrayInit && right.exprs.len > 0 {
elem_expr := right.exprs[0]
if elem_expr is ast.PrefixExpr && elem_expr.op == .amp {
r := elem_expr.right
if r is ast.Ident {
obj := r.obj
if obj is ast.Var && !obj.is_mut {
c.warn('cannot add a referenece to an immutable object to a mutable array',
elem_expr.pos)
}
}
}
}
}
} else if mut left is ast.Ident && left.kind != .blank_ident
&& right is ast.IndexExpr {
if (right as ast.IndexExpr).left is ast.Ident
&& (right as ast.IndexExpr).index is ast.RangeExpr
&& ((right as ast.IndexExpr).left.is_mut() || left.is_mut())
&& !c.inside_unsafe {
// `mut a := arr[..]` auto add clone() -> `mut a := arr[..].clone()`
c.add_error_detail_with_pos('To silence this notice, use either an explicit `a[..].clone()`,
or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
right.pos())
c.note('an implicit clone of the slice was done here', right.pos())
right = ast.CallExpr{
name: 'clone'
left: right
left_type: left_type
is_method: true
receiver_type: left_type
return_type: left_type
scope: c.fn_scope
}
right_type = c.expr(mut right)
node.right[i] = right
}
}
}
if node.op == .assign {
// `mut arr := [u8(1),2,3]`
// `arr = [byte(4),5,6]`
left_info := left_sym.info as ast.Array
left_elem_type := c.table.unaliased_type(left_info.elem_type)
if left_type_unwrapped.nr_muls() == right_type_unwrapped.nr_muls()
&& left_info.nr_dims == right_info.nr_dims && left_elem_type == right_elem_type {
continue
}
}
}
if left_sym.kind == .array_fixed && !c.inside_unsafe && node.op in [.assign, .decl_assign]
&& right_sym.kind == .array_fixed && left is ast.Ident && !left.is_blank_ident()
&& right is ast.Ident {
if right_sym.info is ast.ArrayFixed {
if right_sym.info.elem_type.is_ptr() {
c.error('assignment from one fixed array to another with a pointer element type is prohibited outside of `unsafe`',
node.pos)
}
}
}
if left_sym.kind == .map && node.op in [.assign, .decl_assign] && right_sym.kind == .map
&& !left.is_blank_ident() && right.is_lvalue() && right !is ast.ComptimeSelector
&& (!right_type.is_ptr() || (right is ast.Ident && right.is_auto_deref_var())) {
// Do not allow `a = b`
c.error('cannot copy map: call `move` or `clone` method (or use a reference)',
right.pos())
}
if left_sym.kind == .function && right_sym.info is ast.FnType {
return_sym := c.table.sym(right_sym.info.func.return_type)
if return_sym.kind == .placeholder {
c.error('unkown return type: cannot assign `${right}` as a function variable',
right.pos())
} else if (!right_sym.info.is_anon && return_sym.kind == .any)
|| (return_sym.info is ast.Struct && return_sym.info.is_generic) {
c.error('cannot assign `${right}` as a generic function variable', right.pos())
}
}
if left_type.is_any_kind_of_pointer() && !left.is_auto_deref_var() {
if !c.inside_unsafe && node.op !in [.assign, .decl_assign] {
// ptr op=
c.warn('pointer arithmetic is only allowed in `unsafe` blocks', node.pos)
}
right_is_ptr := right_type.is_any_kind_of_pointer()
if !right_is_ptr && node.op == .assign && right_type_unwrapped.is_number() {
c.error('cannot assign to `${left}`: ' +
c.expected_msg(right_type_unwrapped, left_type_unwrapped), right.pos())
}
if !right_sym.is_number() && !left_type.has_flag(.shared_f)
&& (right is ast.StructInit || !right_is_ptr) {
left_name := c.table.type_to_str(left_type_unwrapped)
mut rtype := right_type_unwrapped
if rtype.is_ptr() {
rtype = rtype.deref()
}
right_name := c.table.type_to_str(rtype)
if !(left_type.has_flag(.option) && right_type == ast.none_type) {
c.error('mismatched types `${left_name}` and `${right_name}`', node.pos)
}
}
}
// Single side check
match node.op {
.assign {} // No need to do single side check for =. But here put it first for speed.
.plus_assign, .minus_assign {
if left_type == ast.string_type {
if node.op != .plus_assign {
c.error('operator `${node.op}` not defined on left operand type `${left_sym.name}`',
left.pos())
}
if right_type != ast.string_type {
c.error('invalid right operand: ${left_sym.name} ${node.op} ${right_sym.name}',
right.pos())
}
} else if !left_sym.is_number()
&& left_sym.kind !in [.byteptr, .charptr, .struct_, .alias] {
c.error('operator `${node.op}` not defined on left operand type `${left_sym.name}`',
left.pos())
} else if !right_sym.is_number()
&& left_sym.kind !in [.byteptr, .charptr, .struct_, .alias] {
c.error('invalid right operand: ${left_sym.name} ${node.op} ${right_sym.name}',
right.pos())
}
}
.mult_assign, .div_assign {
if !left_sym.is_number() && !c.table.final_sym(left_type_unwrapped).is_int()
&& left_sym.kind !in [.struct_, .alias] {
c.error('operator ${node.op.str()} not defined on left operand type `${left_sym.name}`',
left.pos())
} else if !right_sym.is_number() && !c.table.final_sym(left_type_unwrapped).is_int()
&& left_sym.kind !in [.struct_, .alias] {
c.error('operator ${node.op.str()} not defined on right operand type `${right_sym.name}`',
right.pos())
}
}
.and_assign, .or_assign, .xor_assign, .mod_assign, .left_shift_assign,
.right_shift_assign {
if !left_sym.is_int() && !c.table.final_sym(left_type_unwrapped).is_int() {
c.error('operator ${node.op.str()} not defined on left operand type `${left_sym.name}`',
left.pos())
} else if !right_sym.is_int() && !c.table.final_sym(right_type_unwrapped).is_int() {
c.error('operator ${node.op.str()} not defined on right operand type `${right_sym.name}`',
right.pos())
}
}
.unsigned_right_shift_assign {
if node.left.len != 1 || node.right.len != 1 {
c.error('unsupported operation: unable to lower expression for unsigned shift assignment.',
node.pos)
}
modified_left_type := if !left_type.is_int() {
c.error('invalid operation: shift on type `${c.table.sym(left_type).name}`',
node.pos)
ast.void_type_idx
} else if left_type.is_int_literal() {
// int literal => i64
ast.u32_type_idx
} else if left_type.is_unsigned() {
left_type
} else {
// signed types' idx adds with 5 will get correct relative unsigned type
// i8 => byte
// i16 => u16
// int => u32
// i64 => u64
// isize => usize
// i128 => u128 NOT IMPLEMENTED YET
left_type.idx() + ast.u32_type_idx - ast.int_type_idx
}
node = ast.AssignStmt{
op: .assign
pos: node.pos
end_comments: node.end_comments
left: node.left
right: [
ast.Expr(ast.InfixExpr{
left: ast.CastExpr{
expr: node.left[0]
typ: modified_left_type
typname: c.table.type_str(modified_left_type)
expr_type: left_type
pos: node.pos
}
op: .right_shift
right: node.right[0]
left_type: modified_left_type
right_type: right_type
pos: node.pos
}),
]
left_types: node.left_types
right_types: node.right_types
is_static: node.is_static
is_simple: node.is_simple
has_cross_var: node.has_cross_var
}
}
else {}
}
if node.op in [.plus_assign, .minus_assign, .mod_assign, .mult_assign, .div_assign]
&& (left_sym.kind == .alias || (left_sym.kind == .struct_
&& right_sym.kind == .struct_)) {
left_name := c.table.type_to_str(left_type_unwrapped)
right_name := c.table.type_to_str(right_type_unwrapped)
parent_sym := c.table.final_sym(left_type_unwrapped)
if left_sym.kind == .alias && right_sym.kind != .alias {
if !parent_sym.is_primitive() {
c.error('mismatched types `${left_name}` and `${right_name}`', node.pos)
}
}
extracted_op := match node.op {
.plus_assign { '+' }
.minus_assign { '-' }
.div_assign { '/' }
.mod_assign { '%' }
.mult_assign { '*' }
else { 'unknown op' }
}
if left_sym.kind == .struct_ && (left_sym.info as ast.Struct).generic_types.len > 0 {
continue
}
if method := left_sym.find_method(extracted_op) {
if method.return_type != left_type_unwrapped {
c.error('operator `${extracted_op}` must return `${left_name}` to be used as an assignment operator',
node.pos)
}
} else {
if !parent_sym.is_primitive() {
if left_name == right_name {
c.error('undefined operation `${left_name}` ${extracted_op} `${right_name}`',
node.pos)
} else {
c.error('mismatched types `${left_name}` and `${right_name}`',
node.pos)
}
}
}
}
if !is_blank_ident && right_sym.kind != .placeholder && left_sym.kind != .interface_
&& !right_type.has_flag(.generic) && !left_type.has_flag(.generic) {
// Dual sides check (compatibility check)
c.check_expected(right_type_unwrapped, left_type_unwrapped) or {
// allow literal values to auto deref var (e.g.`for mut v in values { v = 1.0 }`)
if left.is_auto_deref_var() || right.is_auto_deref_var() {
left_deref := if left.is_auto_deref_var() {
left_type.deref()
} else {
left_type
}
right_deref := if right.is_pure_literal() {
right.get_pure_type()
} else if right.is_auto_deref_var() {
right_type.deref()
} else {
right_type
}
if c.check_types(left_deref, right_deref) {
continue
}
}
// allow for ptr += 2
if left_type_unwrapped.is_ptr() && right_type_unwrapped.is_int()
&& node.op in [.plus_assign, .minus_assign] {
if !c.inside_unsafe {
c.warn('pointer arithmetic is only allowed in `unsafe` blocks',
node.pos)
}
} else {
// allow `t.$(field.name) = 0` where `t.$(field.name)` is a enum
if c.inside_comptime_for_field && left is ast.ComptimeSelector {
field_sym := c.table.sym(c.unwrap_generic(c.comptime_fields_default_type))
if field_sym.kind == .enum_ && !right_type.is_int() {
c.error('enums can only be assigned `int` values', right.pos())
}
} else {
if !var_option || (var_option && right_type_unwrapped != ast.none_type) {
c.error('cannot assign to `${left}`: ${err.msg()}', right.pos())
}
}
}
}
}
if left_sym.kind == .interface_ {
if c.type_implements(right_type, left_type, right.pos()) {
if !right_type.is_any_kind_of_pointer() && right_sym.kind != .interface_
&& !c.inside_unsafe {
c.mark_as_referenced(mut &node.right[i], true)
}
}
}
if left_sym.info is ast.Struct && !left_sym.info.is_anon && right is ast.StructInit
&& right.is_anon {
c.error('cannot assign anonymous `struct` to a typed `struct`', right.pos())
}
if right_sym.kind == .alias && right_sym.name == 'byte' {
c.warn('byte is deprecated, use u8 instead', right.pos())
}
}
// this needs to run after the assign stmt left exprs have been run through checker
// so that ident.obj is set
// Check `x := &y` and `mut x := <-ch`
if mut right_first is ast.PrefixExpr {
mut right_node := right_first
left_first := node.left[0]
if left_first is ast.Ident {
assigned_var := left_first
mut is_shared := false
if left_first.info is ast.IdentVar {
is_shared = left_first.info.share == .shared_t
}
old_inside_ref_lit := c.inside_ref_lit
c.inside_ref_lit = c.inside_ref_lit || right_node.op == .amp || is_shared
c.expr(mut right_node.right)
c.inside_ref_lit = old_inside_ref_lit
if right_node.op == .amp {
mut expr := right_node.right
for mut expr is ast.ParExpr {
expr = expr.expr
}
if mut expr is ast.Ident {
if mut expr.obj is ast.Var {
v := expr.obj
right_first_type = v.typ
}
if !c.inside_unsafe && assigned_var.is_mut() && !expr.is_mut() {
c.error('`${expr.name}` is immutable, cannot have a mutable reference to it',
right_node.pos)
}
}
}
if right_node.op == .arrow {
if assigned_var.is_mut {
right_sym := c.table.sym(right_first_type)
if right_sym.kind == .chan {
chan_info := right_sym.chan_info()
if chan_info.elem_type.is_ptr() && !chan_info.is_mut {
c.error('cannot have a mutable reference to object from `${right_sym.name}`',
right_node.pos)
}
}
}
}
}
}
if node.left_types.len != node.left.len {
c.error('assign statement left type number mismatch', node.pos)
}
}