Skip to content

Commit b44fb15

Browse files
committed
checker: add a more helpful suggestion for fixed_array = [1,2,3]
1 parent 5abeb41 commit b44fb15

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

vlib/v/checker/assign.v

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,15 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
775775
} else {
776776
if right_type_unwrapped != ast.void_type {
777777
if !var_option || (var_option && right_type_unwrapped != ast.none_type) {
778-
c.error('cannot assign to `${left}`: ${err.msg()}', right.pos())
778+
if left_sym.kind == .array_fixed && right_sym.kind == .array
779+
&& right is ast.ArrayInit {
780+
c.add_error_detail('try `${left} = ${right}!` instead (with `!` after the array literal)')
781+
c.error('cannot assign to `${left}`: ${err.msg()}',
782+
right.pos())
783+
} else {
784+
c.error('cannot assign to `${left}`: ${err.msg()}',
785+
right.pos())
786+
}
779787
}
780788
}
781789
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vlib/v/checker/tests/assign_array_init_to_fixed_array_var.vv:2:5: error: cannot assign to `a`: expected `[3]int`, not `[]int`
2+
1 | mut a := [1, 2, 10]!
3+
2 | a = [1, 2, 3]
4+
| ~~~~~~~~~
5+
3 | dump(a)
6+
Details: try `a = [1, 2, 3]!` instead (with `!` after the array literal)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mut a := [1, 2, 10]!
2+
a = [1, 2, 3]
3+
dump(a)

0 commit comments

Comments
 (0)