diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 70bbc5951a..4af16c82c0 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -1176,6 +1176,7 @@ pub: fields []EnumField // all the enum fields attrs []Attr // attributes of enum declaration typ Type // the default is `int`; can be changed by `enum Big as u64 { a = 5 }` + typ_pos token.Pos pos token.Pos } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 3f3f2878d7..6de7adcdd0 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1579,7 +1579,7 @@ fn (mut c Checker) enum_decl(mut node ast.EnumDecl) { } else { c.error('`${senum_type}` is not one of `i8`,`i16`,`int`,`i64`,`u8`,`u16`,`u32`,`u64`', - node.pos) + node.typ_pos) } } if enum_imin > 0 { diff --git a/vlib/v/checker/tests/enum_field_value_overflow.out b/vlib/v/checker/tests/enum_field_value_overflow.out index a13678b4ec..5375f40e87 100644 --- a/vlib/v/checker/tests/enum_field_value_overflow.out +++ b/vlib/v/checker/tests/enum_field_value_overflow.out @@ -5,11 +5,11 @@ vlib/v/checker/tests/enum_field_value_overflow.vv:3:10: error: enum value `21474 | ~~~~~~~~~~ 4 | blue 5 | } -vlib/v/checker/tests/enum_field_value_overflow.vv:7:1: error: `string` is not one of `i8`,`i16`,`int`,`i64`,`u8`,`u16`,`u32`,`u64` +vlib/v/checker/tests/enum_field_value_overflow.vv:7:21: error: `string` is not one of `i8`,`i16`,`int`,`i64`,`u8`,`u16`,`u32`,`u64` 5 | } - 6 | + 6 | 7 | enum ColorString as string { - | ~~~~~~~~~~~~~~~~ + | ~~~~~~ 8 | red 9 | green = 'abc' vlib/v/checker/tests/enum_field_value_overflow.vv:9:10: error: the default value for an enum has to be an integer @@ -69,21 +69,21 @@ vlib/v/checker/tests/enum_field_value_overflow.vv:56:10: error: enum value `256` 57 | } 58 | vlib/v/checker/tests/enum_field_value_overflow.vv:60:10: error: enum value `65536` overflows the enum type `u16`, values of which have to be in [0, 65535] - 58 | + 58 | 59 | enum ColorU16ShouldFail as u16 { 60 | green = 65536 | ~~~~~ 61 | } 62 | vlib/v/checker/tests/enum_field_value_overflow.vv:64:10: error: enum value `4294967296` overflows the enum type `u32`, values of which have to be in [0, 4294967295] - 62 | + 62 | 63 | enum ColorU32ShouldFail as u32 { 64 | green = 4294967296 | ~~~~~~~~~~ 65 | } 66 | vlib/v/checker/tests/enum_field_value_overflow.vv:68:10: error: enum value `18446744073709551616` overflows the enum type `u64`, values of which have to be in [0, 18446744073709551615] - 66 | + 66 | 67 | enum ColorU64ShouldFail as u64 { 68 | green = 18446744073709551616 | ~~~~~~~~~~~~~~~~~~~~ diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 559d6bfbcf..a2e9c14979 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -3774,8 +3774,10 @@ fn (mut p Parser) enum_decl() ast.EnumDecl { } name := p.prepend_mod(enum_name) mut enum_type := ast.int_type + mut typ_pos := token.Pos{} if p.tok.kind == .key_as { p.next() + typ_pos = p.tok.pos() enum_type = p.parse_type() } p.check(.lcbr) @@ -3856,6 +3858,7 @@ fn (mut p Parser) enum_decl() ast.EnumDecl { enum_decl := ast.EnumDecl{ name: name typ: enum_type + typ_pos: typ_pos is_pub: is_pub is_flag: is_flag is_multi_allowed: is_multi_allowed