Commit b83ce08a3b

Vexu <15308111+Vexu@users.noreply.github.com>
2019-11-13 11:05:15
add compile error for @atomicRmw on enum not being an .Xchg
1 parent c806de8
Changed files (2)
src/ir.cpp
@@ -25824,6 +25824,12 @@ static IrInstruction *ir_analyze_instruction_atomic_rmw(IrAnalyze *ira, IrInstru
         }
     }
 
+    if (operand_type->id == ZigTypeIdEnum && op != AtomicRmwOp_xchg) {
+        ir_add_error(ira, instruction->op,
+            buf_sprintf("@atomicRmw on enum only works with .Xchg"));
+        return ira->codegen->invalid_instruction;
+    }
+
     IrInstruction *operand = instruction->operand->child;
     if (type_is_invalid(operand->value.type))
         return ira->codegen->invalid_instruction;
test/compile_errors.zig
@@ -2,6 +2,22 @@ const tests = @import("tests.zig");
 const builtin = @import("builtin");
 
 pub fn addCases(cases: *tests.CompileErrorContext) void {
+    cases.add(
+        "atomicrmw with enum op not .Xchg",
+        \\export fn entry() void {
+        \\    const E = enum(u8) {
+        \\        a,
+        \\        b,
+        \\        c,
+        \\        d,
+        \\    };
+        \\    var x: E = .a;
+        \\    _ = @atomicRmw(E, &x, .Add, .b, .SeqCst);
+        \\}
+    ,
+        "tmp.zig:9:27: error: @atomicRmw on enum only works with .Xchg",
+    );
+
     cases.add(
         "atomic orderings of atomicStore Acquire or AcqRel",
         \\export fn entry() void {