Skip to content

Commit

Permalink
checker: fix array method error message with generic type (fix #23283) (
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Dec 30, 2024
1 parent fb5bac1 commit d9f5112
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
9 changes: 5 additions & 4 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -3568,7 +3568,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
if node.args.len != 1 {
c.error('`.contains()` expected 1 argument, but got ${node.args.len}', node.pos)
} else if !left_sym.has_method('contains') {
arg_typ := c.expr(mut node.args[0].expr)
arg_typ := c.unwrap_generic(c.expr(mut node.args[0].expr))
c.check_expected_call_arg(arg_typ, c.unwrap_generic(elem_typ), node.language,
node.args[0]) or {
c.error('${err.msg()} in argument 1 to `.contains()`', node.args[0].pos)
Expand All @@ -3582,8 +3582,9 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
if node.args.len != 1 {
c.error('`.index()` expected 1 argument, but got ${node.args.len}', node.pos)
} else if !left_sym.has_method('index') {
arg_typ := c.expr(mut node.args[0].expr)
c.check_expected_call_arg(arg_typ, elem_typ, node.language, node.args[0]) or {
arg_typ := c.unwrap_generic(c.expr(mut node.args[0].expr))
c.check_expected_call_arg(arg_typ, c.unwrap_generic(elem_typ), node.language,
node.args[0]) or {
c.error('${err.msg()} in argument 1 to `.index()`', node.args[0].pos)
}
}
Expand Down Expand Up @@ -3625,7 +3626,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
if node.args.len != 1 {
c.error('`.delete()` expected 1 argument, but got ${node.args.len}', node.pos)
} else {
arg_typ := c.expr(mut node.args[0].expr)
arg_typ := c.unwrap_generic(c.expr(mut node.args[0].expr))
c.check_expected_call_arg(arg_typ, ast.int_type, node.language, node.args[0]) or {
c.error('${err.msg()} in argument 1 to `.delete()`', node.args[0].pos)
}
Expand Down
21 changes: 21 additions & 0 deletions vlib/v/checker/tests/array_generic_methods_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
vlib/v/checker/tests/array_generic_methods_err.vv:3:23: error: cannot use `[]string` as `string` in argument 1 to `.contains()`
1 | fn generic_fn[T]() {
2 | mut arr := []T{}
3 | println(arr.contains(arr))
| ~~~
4 | arr.delete(arr)
5 | arr.index(arr)
vlib/v/checker/tests/array_generic_methods_err.vv:4:13: error: cannot use `[]string` as `int` in argument 1 to `.delete()`
2 | mut arr := []T{}
3 | println(arr.contains(arr))
4 | arr.delete(arr)
| ~~~
5 | arr.index(arr)
6 | }
vlib/v/checker/tests/array_generic_methods_err.vv:5:12: error: cannot use `[]string` as `string` in argument 1 to `.index()`
3 | println(arr.contains(arr))
4 | arr.delete(arr)
5 | arr.index(arr)
| ~~~
6 | }
7 |
10 changes: 10 additions & 0 deletions vlib/v/checker/tests/array_generic_methods_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn generic_fn[T]() {
mut arr := []T{}
println(arr.contains(arr))
arr.delete(arr)
arr.index(arr)
}

fn main() {
generic_fn[string]()
}

0 comments on commit d9f5112

Please sign in to comment.