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

checker: fix error msg for index error for int ptr (#18896)

This commit is contained in:
Swastik Baranwal 2023-07-18 20:00:54 +05:30 committed by GitHub
parent 4f629cd883
commit e5a727c3e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 2 deletions

View File

@ -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()) && (index_type_sym.info as ast.Alias).parent_type.is_int())
|| (c.pref.translated && index_type.is_any_kind_of_pointer())) { || (c.pref.translated && index_type.is_any_kind_of_pointer())) {
type_str := if typ_sym.kind == .string { 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 { } 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) c.error('${type_str}', pos)
} }

View File

@ -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 | }

View File

@ -0,0 +1,6 @@
pub fn read_int(bytes []u8, mut offset &int) int {
defer {
offset += 4
}
return bytes[offset]
}