aboutsummaryrefslogtreecommitdiff
path: root/src/ir.cpp
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2020-01-06 19:54:14 +0100
committerAndrew Kelley <andrew@ziglang.org>2020-01-06 19:36:17 -0500
commit8c640b3e604e597ba49abbda39bd97c379072a3a (patch)
treeab0aedc4572504248d61a074faa31637444b0ae9 /src/ir.cpp
parent7e7d0e1ffaaee4f3deb49d3b98ffd5fcefaf85b1 (diff)
downloadzig-8c640b3e604e597ba49abbda39bd97c379072a3a.tar.gz
zig-8c640b3e604e597ba49abbda39bd97c379072a3a.zip
Prevent bitCast to enum types
Stop the user from creating invalid enum values.
Diffstat (limited to 'src/ir.cpp')
-rw-r--r--src/ir.cpp9
1 files changed, 8 insertions, 1 deletions
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) {