checker: fix voidptr type checking (#21923)

This commit is contained in:
Felipe Pena 2024-07-27 20:09:26 -03:00 committed by GitHub
parent ac136c08e6
commit 521c8156bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 0 deletions

View File

@ -304,6 +304,17 @@ fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type, lan
}
if c.check_types(got, expected) {
if language == .v && idx_got == ast.voidptr_type_idx {
if expected.is_int_valptr() || expected.is_int() || expected.is_ptr() {
return
}
exp_sym := c.table.final_sym(expected)
if exp_sym.language == .v
&& exp_sym.kind !in [.voidptr, .charptr, .byteptr, .function, .placeholder, .array_fixed, .struct_] {
got_typ_str, expected_typ_str := c.get_string_names_of(got, expected)
return error('cannot use `${got_typ_str}` as `${expected_typ_str}`')
}
}
if language != .v || expected.is_ptr() == got.is_ptr() || arg.is_mut
|| arg.expr.is_auto_deref_var() || got.has_flag(.shared_f)
|| c.table.sym(expected_).kind !in [.array, .map] {

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/voidptr_arg_err.vv:3:15: error: cannot use `voidptr` as `[]i32` in argument 1 to `test`
1 | fn test_main() {
2 | c := voidptr(10)
3 | println(test(c))
| ^
4 | }
5 |

View File

@ -0,0 +1,8 @@
fn test_main() {
c := voidptr(10)
println(test(c))
}
fn test(a []i32) i32 {
return a[0]
}