mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-03-08 02:44:43 +01:00
Fixes enums.fromInt failing at compile time on in range but invalid values
This commit is contained in:
parent
b346090ed2
commit
3af842f0e8
1 changed files with 6 additions and 1 deletions
|
|
@ -22,7 +22,7 @@ pub fn fromInt(comptime E: type, integer: anytype) ?E {
|
|||
// without requiring an inline loop.
|
||||
// This generates better machine code.
|
||||
for (values(E)) |value| {
|
||||
if (@intFromEnum(value) == integer) return @enumFromInt(integer);
|
||||
if (@intFromEnum(value) == integer) return value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
@ -213,6 +213,7 @@ test fromInt {
|
|||
B,
|
||||
};
|
||||
const E3 = enum(i8) { A, _ };
|
||||
const E4 = enum(u8) { A };
|
||||
|
||||
var zero: u8 = 0;
|
||||
var one: u16 = 1;
|
||||
|
|
@ -226,6 +227,10 @@ test fromInt {
|
|||
try testing.expectEqual(null, fromInt(E1, one));
|
||||
try testing.expectEqual(null, fromInt(E3, 128));
|
||||
try testing.expectEqual(null, fromInt(E3, -129));
|
||||
|
||||
// `fromInt` used to produce a compiler error instead of `null` if trying to convert an integer
|
||||
// that wasn't out of range, but also wasn't a valid value.
|
||||
try testing.expectEqual(null, fromInt(E4, 1));
|
||||
}
|
||||
|
||||
/// A set of enum elements, backed by a bitfield. If the enum
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue