diff --git a/compiler/parser.v b/compiler/parser.v index 6778669298..ffb93b92f1 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -1963,11 +1963,15 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string { } // expression inside [ ] if is_arr { + index_pos := p.cgen.cur_line.len T := p.table.find_type(p.expression()) // Allows only i8-64 and u8-64 to be used when accessing an array if T.parent != 'int' && T.parent != 'u32' { p.check_types(T.name, 'int') } + if p.cgen.cur_line.right(index_pos).replace(' ', '').int() < 0 { + p.error('cannot access negative array index') + } } else { T := p.table.find_type(p.expression())