array: [].map(fn...) return type can be different than original type (#7300)

pull/7322/head
LilEnvy 2020-12-12 18:29:48 -08:00 committed by GitHub
parent e6f651978e
commit 0aacc9a80a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 48 additions and 9 deletions

View File

@ -676,6 +676,23 @@ fn test_anon_fn_arg_map() {
assert a == [2,3,4]
}
fn test_anon_fn_arg_different_type_map() {
i_to_str := fn (i int) string {
return i.str()
}
a := [1, 2, 3].map(i_to_str)
assert a == ['1', '2', '3']
}
fn test_anon_fn_inline_different_type_map() {
a := [1, 2, 3].map(fn (i int) string {
return i.str()
})
assert a == ['1', '2', '3']
}
fn test_array_str() {
numbers := [1, 2, 3]
assert numbers == [1,2,3]

View File

@ -1086,9 +1086,8 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ table.Type, call_e
if arg_expr.decl.params.len > 1 {
c.error('function needs exactly 1 argument', arg_expr.decl.pos)
} else if is_map &&
(arg_expr.decl.return_type != elem_typ || arg_expr.decl.params[0].typ != elem_typ) {
c.error('type mismatch, should use `fn(a $elem_sym.name) $elem_sym.name {...}`',
arg_expr.decl.pos)
(arg_expr.decl.return_type == table.void_type || arg_expr.decl.params[0].typ != elem_typ) {
c.error('type mismatch, should use `fn(a $elem_sym.name) T {...}`', arg_expr.decl.pos)
} else if !is_map &&
(arg_expr.decl.return_type != table.bool_type || arg_expr.decl.params[0].typ != elem_typ) {
c.error('type mismatch, should use `fn(a $elem_sym.name) bool {...}`',
@ -1103,8 +1102,9 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ table.Type, call_e
}
if func.params.len > 1 {
c.error('function needs exactly 1 argument', call_expr.pos)
} else if is_map && (func.return_type != elem_typ || func.params[0].typ != elem_typ) {
c.error('type mismatch, should use `fn(a $elem_sym.name) $elem_sym.name {...}`',
} else if is_map &&
(func.return_type == table.void_type || func.params[0].typ != elem_typ) {
c.error('type mismatch, should use `fn(a $elem_sym.name) T {...}`',
arg_expr.pos)
} else if !is_map &&
(func.return_type != table.bool_type || func.params[0].typ != elem_typ) {

View File

@ -1,6 +1,6 @@
vlib/v/checker/tests/array_map_anon_fn_err_b.vv:2:21: error: type mismatch, should use `fn(a int) int {...}`
vlib/v/checker/tests/array_map_anon_fn_err_b.vv:2:21: error: type mismatch, should use `fn(a int) T {...}`
1 | fn main() {
2 | a := [1,2,3,4].map(fn(a string) string { return a })
| ~~
3 | println(a)
4 | }
4 | }

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/array_map_anon_fn_err_c.vv:2:21: error: type mismatch, should use `fn(a int) T {...}`
1 | fn main() {
2 | a := [1,2,3,4].map(fn(a string) {})
| ~~
3 | println(a)
4 | }

View File

@ -0,0 +1,4 @@
fn main() {
a := [1,2,3,4].map(fn(a string) {})
println(a)
}

View File

@ -1,7 +1,7 @@
vlib/v/checker/tests/array_map_fn_err_b.vv:5:21: error: type mismatch, should use `fn(a int) int {...}`
vlib/v/checker/tests/array_map_fn_err_b.vv:5:21: error: type mismatch, should use `fn(a int) T {...}`
3 | }
4 | fn main() {
5 | a := [1,2,3,4].map(add)
| ~~~
6 | println(a)
7 | }
7 | }

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/array_map_fn_err_c.vv:3:21: error: type mismatch, should use `fn(a int) T {...}`
1 | fn do_nothing(a string) {}
2 | fn main() {
3 | a := [1,2,3,4].map(do_nothing)
| ~~~~~~~~~~
4 | println(a)
5 | }

View File

@ -0,0 +1,5 @@
fn do_nothing(a string) {}
fn main() {
a := [1,2,3,4].map(do_nothing)
println(a)
}