checker: disallow indexing a voidptr (#8109)
parent
890fb2d09d
commit
127503c77d
|
@ -4718,8 +4718,8 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) table.Type {
|
||||||
typ := c.expr(node.left)
|
typ := c.expr(node.left)
|
||||||
node.left_type = typ
|
node.left_type = typ
|
||||||
typ_sym := c.table.get_final_type_symbol(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() &&
|
if typ_sym.kind !in [.array, .array_fixed, .string, .map] && !typ.is_ptr() && typ !in [table.byteptr_type, table.charptr_type] &&
|
||||||
typ_sym.name.ends_with('ptr')) && !typ.has_flag(.variadic) { // byteptr, charptr etc
|
!typ.has_flag(.variadic) {
|
||||||
c.error('type `$typ_sym.name` does not support indexing', node.pos)
|
c.error('type `$typ_sym.name` does not support indexing', node.pos)
|
||||||
}
|
}
|
||||||
if typ_sym.kind == .string && !typ.is_ptr() && node.is_setter {
|
if typ_sym.kind == .string && !typ.is_ptr() && node.is_setter {
|
||||||
|
|
|
@ -32,11 +32,18 @@ vlib/v/checker/tests/pointer_ops.vv:9:4: error: invalid operation: -- (non-numer
|
||||||
9 | p--
|
9 | p--
|
||||||
| ~~
|
| ~~
|
||||||
10 | p -= 3
|
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`
|
vlib/v/checker/tests/pointer_ops.vv:10:3: error: operator `-=` not defined on left operand type `voidptr`
|
||||||
8 | _ = p - 1
|
8 | _ = p - 1
|
||||||
9 | p--
|
9 | p--
|
||||||
10 | p -= 3
|
10 | p -= 3
|
||||||
| ^
|
| ^
|
||||||
11 | }
|
11 | _ = p[3]
|
||||||
12 | }
|
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 | }
|
||||||
|
|
|
@ -8,5 +8,6 @@ fn test_voidptr() {
|
||||||
_ = p - 1
|
_ = p - 1
|
||||||
p--
|
p--
|
||||||
p -= 3
|
p -= 3
|
||||||
|
_ = p[3]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue