Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: fix array method error message with generic type #23304

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]()
}
Loading