From 8c640b3e604e597ba49abbda39bd97c379072a3a Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 6 Jan 2020 19:54:14 +0100 Subject: Prevent bitCast to enum types Stop the user from creating invalid enum values. --- src/ir.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index 8332cbd2d6..9b6265e1e4 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -27078,13 +27078,20 @@ static IrInstruction *ir_analyze_bit_cast(IrAnalyze *ira, IrInstruction *source_ ir_assert(get_codegen_ptr_type(dest_type) == nullptr, source_instr); ir_assert(type_can_bit_cast(dest_type), source_instr); + if (dest_type->id == ZigTypeIdEnum) { + ErrorMsg *msg = ir_add_error_node(ira, source_instr->source_node, + buf_sprintf("cannot cast a value of type '%s'", buf_ptr(&dest_type->name))); + add_error_note(ira->codegen, msg, source_instr->source_node, + buf_sprintf("use @intToEnum for type coercion")); + return ira->codegen->invalid_instruction; + } + if ((err = type_resolve(ira->codegen, dest_type, ResolveStatusSizeKnown))) return ira->codegen->invalid_instruction; if ((err = type_resolve(ira->codegen, src_type, ResolveStatusSizeKnown))) return ira->codegen->invalid_instruction; - uint64_t dest_size_bytes = type_size(ira->codegen, dest_type); uint64_t src_size_bytes = type_size(ira->codegen, src_type); if (dest_size_bytes != src_size_bytes) { -- cgit v1.2.3