1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix dereferencing voidptr. (#15033)

This commit is contained in:
Dmitriy Smirnov 2022-07-12 01:12:29 +03:00 committed by GitHub
parent dc68469818
commit f35a3a89f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 0 deletions

View File

@ -331,6 +331,11 @@ pub fn (typ Type) is_pointer() bool {
return typ.idx() in ast.pointer_type_idxs
}
[inline]
pub fn (typ Type) is_voidptr() bool {
return typ.idx() == ast.voidptr_type_idx
}
[inline]
pub fn (typ Type) is_real_pointer() bool {
return typ.is_ptr() || typ.is_pointer()

View File

@ -3305,6 +3305,9 @@ pub fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
s := c.table.type_to_str(right_type)
c.error('invalid indirect of `$s`', node.pos)
}
if right_type.is_voidptr() {
c.error('cannot dereference to void', node.pos)
}
}
if node.op == .bit_not && !right_type.is_int() && !c.pref.translated && !c.file.is_translated {
c.error('operator ~ only defined on int types', node.pos)

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/voidptr_dereference_err.vv:5:10: error: cannot dereference to void
3 | mut b := 123
4 | a = &b
5 | println(*a)
| ^
6 | }

View File

@ -0,0 +1,6 @@
fn main() {
mut a := voidptr(0)
mut b := 123
a = &b
println(*a)
}