diff --git a/releasenotes.md b/releasenotes.md index fecbd98ec..0f094cd85 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -18,6 +18,7 @@ - Casting a slice address to its pointer type should not compile #1193. - Union is not properly zero-initialized with designated initializer #1194. - Compile time fmod evaluates to 0 #1195. +- Assertion failed when casting (unsigned) argument to enum #1196 ### Stdlib changes - Add 'zstr' variants for `string::new_format` / `string::tformat`. diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 59c5835b7..4a07dd496 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -1576,6 +1576,7 @@ void llvm_emit_cast(GenContext *c, CastKind cast_kind, Expr *expr, BEValue *valu llvm_value_set(value, llvm_zext_trunc(c, value->value, llvm_get_type(c, to_type)), to_type); return; } + value->type = type_lowering(to_type); return; case CAST_SABOOL: llvm_value_fold_optional(c, value); diff --git a/test/test_suite/enumerations/enum_signed_cast_swap.c3t b/test/test_suite/enumerations/enum_signed_cast_swap.c3t new file mode 100644 index 000000000..8b59417ca --- /dev/null +++ b/test/test_suite/enumerations/enum_signed_cast_swap.c3t @@ -0,0 +1,27 @@ +// #target: macos-x64 +module test; +enum Abc : char { ABC } + +enum Mouse_Button { + LEFT, + RIGHT, + MIDDLE, +} + +fn void foo(Mouse_Button button) +{ +} + +fn int main() { + uint x = 1; + foo((Mouse_Button)x); + return 0; +} + +/* #expect: test.ll + + %x = alloca i32, align 4 + store i32 1, ptr %x, align 4 + %0 = load i32, ptr %x, align 4 + call void @test.foo(i32 %0) + ret i32 0