checker: disallow indexing a voidptr (#8109)

pull/8113/head
Nick Treleaven 2021-01-14 18:51:43 +00:00 committed by GitHub
parent 890fb2d09d
commit 127503c77d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 5 deletions

View File

@ -4718,8 +4718,8 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) table.Type {
typ := c.expr(node.left)
node.left_type = typ
typ_sym := c.table.get_final_type_symbol(typ)
if typ_sym.kind !in [.array, .array_fixed, .string, .map] && !typ.is_ptr() && !(!typ_sym.name[0].is_capital() &&
typ_sym.name.ends_with('ptr')) && !typ.has_flag(.variadic) { // byteptr, charptr etc
if typ_sym.kind !in [.array, .array_fixed, .string, .map] && !typ.is_ptr() && typ !in [table.byteptr_type, table.charptr_type] &&
!typ.has_flag(.variadic) {
c.error('type `$typ_sym.name` does not support indexing', node.pos)
}
if typ_sym.kind == .string && !typ.is_ptr() && node.is_setter {

View File

@ -32,11 +32,18 @@ vlib/v/checker/tests/pointer_ops.vv:9:4: error: invalid operation: -- (non-numer
9 | p--
| ~~
10 | p -= 3
11 | }
11 | _ = p[3]
vlib/v/checker/tests/pointer_ops.vv:10:3: error: operator `-=` not defined on left operand type `voidptr`
8 | _ = p - 1
9 | p--
10 | p -= 3
| ^
11 | }
12 | }
11 | _ = p[3]
12 | }
vlib/v/checker/tests/pointer_ops.vv:11:8: error: type `voidptr` does not support indexing
9 | p--
10 | p -= 3
11 | _ = p[3]
| ~~~
12 | }
13 | }

View File

@ -8,5 +8,6 @@ fn test_voidptr() {
_ = p - 1
p--
p -= 3
_ = p[3]
}
}