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

v2: allow enum value to be used as int

This commit is contained in:
Joe Conigliaro 2020-03-02 21:32:28 +11:00
parent f57a651e3b
commit 16528b12fa
2 changed files with 8 additions and 1 deletions

View File

@ -764,8 +764,10 @@ pub fn (c mut Checker) index_expr(node ast.IndexExpr) table.Type {
if !is_range {
typ_sym := c.table.get_type_symbol(typ)
index_type := c.expr(node.index)
index_type_sym := c.table.get_type_symbol(index_type)
// println('index expr left=$typ_sym.name $node.pos.line_nr')
if typ_sym.kind == .array && !(table.type_idx(index_type) in table.number_idxs) {
if typ_sym.kind == .array &&
(!(table.type_idx(index_type) in table.number_idxs) && index_type_sym.kind != .enum_) {
c.error('non-integer index (type `$typ_sym.name`)', node.pos)
}
else if typ_sym.kind == .map && table.type_idx(index_type) != table.string_type_idx {

View File

@ -386,6 +386,11 @@ pub fn (t &Table) check(got, expected Type) bool {
if got_type_sym.is_int() && exp_type_sym.is_int() {
return true
}
// allow enum value to be used as int
if (got_type_sym.is_int() && exp_type_sym.kind == .enum_) ||
(exp_type_sym.is_int() && got_type_sym.kind == .enum_) {
return true
}
// TODO
if got_type_sym.is_number() && exp_type_sym.is_number() {
return true