checker: add check for decomposing to interface (fix #24441) (#24453)

This commit is contained in:
Felipe Pena 2025-05-10 15:38:45 -03:00 committed by GitHub
parent 6a98e38f0d
commit 3f76b69165
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 0 deletions

View File

@ -1612,6 +1612,12 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
c.error('cannot use `${got_typ_str}` as `${expected_typ_str}` in argument ${i + 1} to `${fn_name}`',
call_arg.pos)
}
if call_arg.expr is ast.ArrayDecompose && arg_typ.idx() != final_param_typ.idx() {
expected_type_str := c.table.type_to_str(param.typ)
got_type_str := c.table.type_to_str(arg_typ)
c.error('cannot use `${got_type_str}` as `${expected_type_str}` in argument ${i + 1} to `${fn_name}`',
call_arg.pos)
}
continue
}
if param.typ.is_ptr() && !param.is_mut && !call_arg.typ.is_any_kind_of_pointer()

View File

@ -0,0 +1,20 @@
vlib/v/checker/tests/decompose_interface_err.vv:3:8: warning: module 'arrays' is imported but never used
1 | module main
2 |
3 | import arrays
| ~~~~~~
4 |
5 | interface Value {}
vlib/v/checker/tests/decompose_interface_err.vv:17:2: warning: unused variable: `region_id`
15 | 'AF',
16 | ]
17 | region_id := 'something'
| ~~~~~~~~~
18 | some_function(...country_ids)
19 | }
vlib/v/checker/tests/decompose_interface_err.vv:18:16: error: cannot use `string` as `Value` in argument 1 to `some_function`
16 | ]
17 | region_id := 'something'
18 | some_function(...country_ids)
| ~~~~~~~~~~~~~~
19 | }

View File

@ -0,0 +1,19 @@
module main
import arrays
interface Value {}
fn some_function(values ...Value) {
println(values)
}
fn main() {
country_ids := [
'AD',
'AE',
'AF',
]
region_id := 'something'
some_function(...country_ids)
}