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

checker: allow i32 in enum MyEnum as i32 { (#18172)

This commit is contained in:
Swastik Baranwal 2023-05-16 10:39:13 +05:30 committed by GitHub
parent a7f84e79f8
commit ee7d34e650
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 3 deletions

View File

@ -1627,10 +1627,14 @@ fn (mut c Checker) enum_decl(mut node ast.EnumDecl) {
signed, enum_umin, enum_umax = false, 0, 0xFFFF_FFFF_FFFF_FFFF
}
else {
c.error('`${senum_type}` is not one of `i8`,`i16`,`int`,`i64`,`u8`,`u16`,`u32`,`u64`',
if senum_type == 'i32' {
signed, enum_imin, enum_imax = true, -2_147_483_648, 0x7FFF_FFFF
} else {
c.error('`${senum_type}` is not one of `i8`,`i16`,`i32`,`int`,`i64`,`u8`,`u16`,`u32`,`u64`',
node.typ_pos)
}
}
}
if enum_imin > 0 {
// ensure that the minimum value is negative, even with msvc, which has a bug that makes -2147483648 positive ...
enum_imin *= -1

View File

@ -5,7 +5,7 @@ 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:21: 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`,`i32`,`int`,`i64`,`u8`,`u16`,`u32`,`u64`
5 | }
6 |
7 | enum ColorString as string {

View File

@ -88,6 +88,20 @@ fn test_nums() {
assert d == unsafe { Foo(-10) }
}
enum Number as i32 {
a = 100
b = 200
c = 300
d = 400
}
fn test_enum_as_i32() {
assert int(Number.a) == 100
assert int(Number.b) == 200
assert int(Number.c) == 300
assert int(Number.d) == 400
}
/*
enum Expr {
BoolExpr(bool)