diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 08725970f0..aeeacbdd6a 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 { diff --git a/vlib/v/checker/tests/pointer_ops.out b/vlib/v/checker/tests/pointer_ops.out index b896053605..022111696c 100644 --- a/vlib/v/checker/tests/pointer_ops.out +++ b/vlib/v/checker/tests/pointer_ops.out @@ -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 | } diff --git a/vlib/v/checker/tests/pointer_ops.vv b/vlib/v/checker/tests/pointer_ops.vv index 238e3e32c3..e93161b675 100644 --- a/vlib/v/checker/tests/pointer_ops.vv +++ b/vlib/v/checker/tests/pointer_ops.vv @@ -8,5 +8,6 @@ fn test_voidptr() { _ = p - 1 p-- p -= 3 + _ = p[3] } }