diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 50dafb4f67..3282f0d1f8 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1674,6 +1674,7 @@ fn (mut c Checker) enum_decl(mut node ast.EnumDecl) { mut overflows := false mut uval := u64(0) mut ival := i64(0) + if signed { val := field.expr.val.i64() ival = val @@ -1686,9 +1687,22 @@ fn (mut c Checker) enum_decl(mut node ast.EnumDecl) { val := field.expr.val.u64() uval = val if val >= enum_umax { - c.error('enum value `${field.expr.val}` overflows the enum type `${senum_type}`, values of which have to be in [${enum_umin}, ${enum_umax}]', - field.expr.pos) overflows = true + if val == enum_umax { + is_bin := field.expr.val.starts_with('0b') + is_oct := field.expr.val.starts_with('0o') + is_hex := field.expr.val.starts_with('0x') + + if is_hex { + overflows = val.hex() != enum_umax.hex() + } else if !is_bin && !is_oct && !is_hex { + overflows = field.expr.val.str() != enum_umax.str() + } + } + if overflows { + c.error('enum value `${field.expr.val}` overflows the enum type `${senum_type}`, values of which have to be in [${enum_umin}, ${enum_umax}]', + field.expr.pos) + } } } if !overflows && !c.pref.translated && !c.file.is_translated diff --git a/vlib/v/tests/enum_max_test.v b/vlib/v/tests/enum_max_test.v new file mode 100644 index 0000000000..e8e14e0b7c --- /dev/null +++ b/vlib/v/tests/enum_max_test.v @@ -0,0 +1,27 @@ +enum Nums as u8 { + one + two + three = 0xff +} + +enum Nums2 as u32 { + one + two + three = 0xFFFFFFFF +} + +enum Nums3 as u64 { + one + two + three = 0xFFFFFFFFFFFFFFFF +} + +fn test_main() { + mut a := Nums.one + assert a == Nums.one + assert int(Nums.three) == 0xff + assert Nums.three == unsafe { Nums(255) } + + assert u64(Nums2.three) == 0xFFFFFFFF + assert u64(Nums3.three) == 0xFFFFFFFFFFFFFFFF +}