harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 28541ae45185f623f7ff4693081ec3144f477858
parent 0a21c166ae1417767d649dfa3ddb8e9856806c1e
Author: Eyal Sawady <ecs@d2evs.net>
Date:   Thu, 13 Jan 2022 13:11:15 +0000

eval: handle char in itrunc

Fixes casts to char at compile-time

Signed-off-by: Eyal Sawady <ecs@d2evs.net>

Diffstat:
Msrc/eval.c | 2+-
Mtests/15-enums.ha | 6++++++
2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/eval.c b/src/eval.c @@ -44,6 +44,7 @@ static uintmax_t itrunc(const struct type *type, uintmax_t val) { switch (type->storage) { + case STORAGE_CHAR: case STORAGE_U8: return (uint8_t)val; case STORAGE_U16: @@ -80,7 +81,6 @@ itrunc(const struct type *type, uintmax_t val) return itrunc(type_dealias(type), val); case STORAGE_ENUM: return itrunc(builtin_type_for_storage(type->_enum.storage, false), val); - case STORAGE_CHAR: case STORAGE_F32: case STORAGE_F64: case STORAGE_FCONST: diff --git a/tests/15-enums.ha b/tests/15-enums.ha @@ -33,6 +33,11 @@ type with_storage = enum u16 { BEEF = 0xBEEF, }; +type char_storage = enum char { + FOO = 0, + BAR = 1, +}; + fn storage() void = { assert(size(explicit_values) == size(int)); assert(size(with_storage) == 2); @@ -42,6 +47,7 @@ fn storage() void = { assert(with_storage::BABE: u8 == (if (is_little) 0xBEu8 else 0xBAu8)); assert(with_storage::DEAD: u8 == (if (is_little) 0xADu8 else 0xDEu8)); assert(with_storage::BEEF: u8 == (if (is_little) 0xEFu8 else 0xBEu8)); + assert(char_storage::FOO == 0 && char_storage::BAR == 1); }; export fn main() void = {