Commit 49a4b1b930
Changed files (5)
src/all_types.hpp
@@ -383,6 +383,7 @@ enum CastOp {
CastOpFloatToInt,
CastOpBoolToInt,
CastOpResizeSlice,
+ CastOpIntToEnum,
};
struct AstNodeFnCallExpr {
src/analyze.cpp
@@ -4428,6 +4428,14 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B
}
}
+ // explicit cast from integer to enum type with no payload
+ if (actual_type->id == TypeTableEntryIdInt &&
+ wanted_type->id == TypeTableEntryIdEnum &&
+ wanted_type->data.enumeration.gen_field_count == 0)
+ {
+ return resolve_cast(g, context, node, expr_node, wanted_type, CastOpIntToEnum, false);
+ }
+
add_node_error(g, node,
buf_sprintf("invalid cast from type '%s' to '%s'",
buf_ptr(&actual_type->name),
src/codegen.cpp
@@ -1032,6 +1032,8 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {
set_debug_source_node(g, node);
return LLVMBuildZExt(g->builder, expr_val, wanted_type->type_ref, "");
+ case CastOpIntToEnum:
+ return gen_widen_or_shorten(g, node, actual_type, wanted_type->data.enumeration.tag_type, expr_val);
}
zig_unreachable();
}
@@ -3913,7 +3915,7 @@ static void do_code_gen(CodeGen *g) {
LLVMZigAddNonNullAttr(fn_table_entry->fn_value, 1);
is_sret = true;
}
- if (fn_table_entry->is_pure && !is_sret) {
+ if (fn_table_entry->is_pure && !is_sret && g->is_release_build) {
LLVMAddFunctionAttr(fn_table_entry->fn_value, LLVMReadOnlyAttribute);
}
src/eval.cpp
@@ -657,6 +657,16 @@ void eval_const_expr_implicit_cast(CastOp cast_op,
bignum_init_unsigned(&const_val->data.x_bignum, other_val->data.x_bool ? 1 : 0);
const_val->ok = true;
break;
+ case CastOpIntToEnum:
+ {
+ uint64_t value = other_val->data.x_bignum.data.x_uint;
+ assert(new_type->id == TypeTableEntryIdEnum);
+ assert(value < new_type->data.enumeration.field_count);
+ const_val->data.x_enum.tag = value;
+ const_val->data.x_enum.payload = NULL;
+ const_val->ok = true;
+ break;
+ }
}
}
test/self_hosted.zig
@@ -1740,3 +1740,23 @@ fn int_type_builtin() {
assert(!usize.is_signed);
}
+
+#attribute("test")
+fn int_to_enum() {
+ test_int_to_enum_eval(3);
+ test_int_to_enum_noeval(3);
+}
+fn test_int_to_enum_eval(x: i32) {
+ assert(IntToEnumNumber(x) == IntToEnumNumber.Three);
+}
+#static_eval_enable(false)
+fn test_int_to_enum_noeval(x: i32) {
+ assert(IntToEnumNumber(x) == IntToEnumNumber.Three);
+}
+enum IntToEnumNumber {
+ Zero,
+ One,
+ Two,
+ Three,
+ Four,
+}