Skip to content

Commit 521c815

Browse files
authored
checker: fix voidptr type checking (#21923)
1 parent ac136c0 commit 521c815

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

vlib/v/checker/check_types.v

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,17 @@ fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type, lan
304304
}
305305

306306
if c.check_types(got, expected) {
307+
if language == .v && idx_got == ast.voidptr_type_idx {
308+
if expected.is_int_valptr() || expected.is_int() || expected.is_ptr() {
309+
return
310+
}
311+
exp_sym := c.table.final_sym(expected)
312+
if exp_sym.language == .v
313+
&& exp_sym.kind !in [.voidptr, .charptr, .byteptr, .function, .placeholder, .array_fixed, .struct_] {
314+
got_typ_str, expected_typ_str := c.get_string_names_of(got, expected)
315+
return error('cannot use `${got_typ_str}` as `${expected_typ_str}`')
316+
}
317+
}
307318
if language != .v || expected.is_ptr() == got.is_ptr() || arg.is_mut
308319
|| arg.expr.is_auto_deref_var() || got.has_flag(.shared_f)
309320
|| c.table.sym(expected_).kind !in [.array, .map] {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/voidptr_arg_err.vv:3:15: error: cannot use `voidptr` as `[]i32` in argument 1 to `test`
2+
1 | fn test_main() {
3+
2 | c := voidptr(10)
4+
3 | println(test(c))
5+
| ^
6+
4 | }
7+
5 |
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn test_main() {
2+
c := voidptr(10)
3+
println(test(c))
4+
}
5+
6+
fn test(a []i32) i32 {
7+
return a[0]
8+
}

0 commit comments

Comments
 (0)