Skip to content

Commit

Permalink
checker: fix error msg for index error for int ptr (#18896)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Jul 18, 2023
1 parent 4f629cd commit e5a727c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions vlib/v/checker/checker.v
Expand Up @@ -4076,9 +4076,9 @@ fn (mut c Checker) check_index(typ_sym &ast.TypeSymbol, index ast.Expr, index_ty
&& (index_type_sym.info as ast.Alias).parent_type.is_int())
|| (c.pref.translated && index_type.is_any_kind_of_pointer())) {
type_str := if typ_sym.kind == .string {
'non-integer string index `${index_type_sym.name}`'
'non-integer string index `${c.table.type_to_str(index_type)}`'
} else {
'non-integer index `${index_type_sym.name}` (array type `${typ_sym.name}`)'
'non-integer index `${c.table.type_to_str(index_type)}` (array type `${typ_sym.name}`)'
}
c.error('${type_str}', pos)
}
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/checker/tests/int_ptr_array_index_err.out
@@ -0,0 +1,6 @@
vlib/v/checker/tests/int_ptr_array_index_err.vv:5:14: error: non-integer index `&int` (array type `[]u8`)
3 | offset += 4
4 | }
5 | return bytes[offset]
| ~~~~~~~~
6 | }
6 changes: 6 additions & 0 deletions vlib/v/checker/tests/int_ptr_array_index_err.vv
@@ -0,0 +1,6 @@
pub fn read_int(bytes []u8, mut offset &int) int {
defer {
offset += 4
}
return bytes[offset]
}

0 comments on commit e5a727c

Please sign in to comment.