Skip to content

Commit

Permalink
checker: fix voidptr type checking (#21923)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Jul 27, 2024
1 parent ac136c0 commit 521c815
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions vlib/v/checker/check_types.v
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/voidptr_arg_err.out
Original file line number Diff line number Diff line change
@@ -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 |
8 changes: 8 additions & 0 deletions vlib/v/checker/tests/voidptr_arg_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn test_main() {
c := voidptr(10)
println(test(c))
}

fn test(a []i32) i32 {
return a[0]
}

0 comments on commit 521c815

Please sign in to comment.