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:
parent
a7f84e79f8
commit
ee7d34e650
@ -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
|
signed, enum_umin, enum_umax = false, 0, 0xFFFF_FFFF_FFFF_FFFF
|
||||||
}
|
}
|
||||||
else {
|
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)
|
node.typ_pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if enum_imin > 0 {
|
if enum_imin > 0 {
|
||||||
// ensure that the minimum value is negative, even with msvc, which has a bug that makes -2147483648 positive ...
|
// ensure that the minimum value is negative, even with msvc, which has a bug that makes -2147483648 positive ...
|
||||||
enum_imin *= -1
|
enum_imin *= -1
|
||||||
|
@ -5,7 +5,7 @@ vlib/v/checker/tests/enum_field_value_overflow.vv:3:10: error: enum value `21474
|
|||||||
| ~~~~~~~~~~
|
| ~~~~~~~~~~
|
||||||
4 | blue
|
4 | blue
|
||||||
5 | }
|
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 | }
|
5 | }
|
||||||
6 |
|
6 |
|
||||||
7 | enum ColorString as string {
|
7 | enum ColorString as string {
|
||||||
|
@ -88,6 +88,20 @@ fn test_nums() {
|
|||||||
assert d == unsafe { Foo(-10) }
|
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 {
|
enum Expr {
|
||||||
BoolExpr(bool)
|
BoolExpr(bool)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user