From b44fb1503266356a3cd92d036e24a593b81665c2 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 17 Dec 2023 17:28:11 +0200 Subject: [PATCH] checker: add a more helpful suggestion for `fixed_array = [1,2,3]` --- vlib/v/checker/assign.v | 10 +++++++++- .../tests/assign_array_init_to_fixed_array_var.out | 6 ++++++ .../tests/assign_array_init_to_fixed_array_var.vv | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/assign_array_init_to_fixed_array_var.out create mode 100644 vlib/v/checker/tests/assign_array_init_to_fixed_array_var.vv diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index e7628f08a..cb05a36f5 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -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()) + } } } } diff --git a/vlib/v/checker/tests/assign_array_init_to_fixed_array_var.out b/vlib/v/checker/tests/assign_array_init_to_fixed_array_var.out new file mode 100644 index 000000000..888ce4097 --- /dev/null +++ b/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) diff --git a/vlib/v/checker/tests/assign_array_init_to_fixed_array_var.vv b/vlib/v/checker/tests/assign_array_init_to_fixed_array_var.vv new file mode 100644 index 000000000..e769611a0 --- /dev/null +++ b/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) -- 2.39.5