Skip to content

Commit

Permalink
checker: add a more helpful suggestion for fixed_array = [1,2,3]
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Dec 17, 2023
1 parent 5abeb41 commit b44fb15
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
10 changes: 9 additions & 1 deletion vlib/v/checker/assign.v
Expand Up @@ -775,7 +775,15 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
} else {
if right_type_unwrapped != ast.void_type {
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 == .array_fixed && right_sym.kind == .array
&& right is ast.ArrayInit {
c.add_error_detail('try `${left} = ${right}!` instead (with `!` after the array literal)')
c.error('cannot assign to `${left}`: ${err.msg()}',
right.pos())
} else {
c.error('cannot assign to `${left}`: ${err.msg()}',
right.pos())
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/checker/tests/assign_array_init_to_fixed_array_var.out
@@ -0,0 +1,6 @@
vlib/v/checker/tests/assign_array_init_to_fixed_array_var.vv:2:5: error: cannot assign to `a`: expected `[3]int`, not `[]int`
1 | mut a := [1, 2, 10]!
2 | a = [1, 2, 3]
| ~~~~~~~~~
3 | dump(a)
Details: try `a = [1, 2, 3]!` instead (with `!` after the array literal)
3 changes: 3 additions & 0 deletions vlib/v/checker/tests/assign_array_init_to_fixed_array_var.vv
@@ -0,0 +1,3 @@
mut a := [1, 2, 10]!
a = [1, 2, 3]
dump(a)

0 comments on commit b44fb15

Please sign in to comment.