Commit 7571db05de

Andrew Kelley <andrew@ziglang.org>
2019-02-26 02:28:09
fix incorrectly trying to memset at comptime
closes #718
1 parent 4b7e285
Changed files (3)
src
test
stage1
behavior
bugs
src/ir.cpp
@@ -19451,10 +19451,12 @@ static IrInstruction *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructio
     if (type_is_invalid(casted_count->value.type))
         return ira->codegen->invalid_instruction;
 
+    // TODO test this at comptime with u8 and non-u8 types
     if (casted_dest_ptr->value.special == ConstValSpecialStatic &&
         casted_byte->value.special == ConstValSpecialStatic &&
         casted_count->value.special == ConstValSpecialStatic &&
-        casted_dest_ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr)
+        casted_dest_ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr &&
+        casted_dest_ptr->value.data.x_ptr.mut != ConstPtrMutRuntimeVar)
     {
         ConstExprValue *dest_ptr_val = &casted_dest_ptr->value;
 
@@ -19573,6 +19575,8 @@ static IrInstruction *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructio
     if (type_is_invalid(casted_count->value.type))
         return ira->codegen->invalid_instruction;
 
+    // TODO test this at comptime with u8 and non-u8 types
+    // TODO test with dest ptr being a global runtime variable 
     if (casted_dest_ptr->value.special == ConstValSpecialStatic &&
         casted_src_ptr->value.special == ConstValSpecialStatic &&
         casted_count->value.special == ConstValSpecialStatic &&
test/stage1/behavior/bugs/718.zig
@@ -0,0 +1,17 @@
+const std = @import("std");
+const mem = std.mem;
+const expect = std.testing.expect;
+const Keys = struct {
+    up: bool,
+    down: bool,
+    left: bool,
+    right: bool,
+};
+var keys: Keys = undefined;
+test "zero keys with @memset" {
+    @memset(@ptrCast([*]u8, &keys), 0, @sizeOf(@typeOf(keys)));
+    expect(!keys.up);
+    expect(!keys.down);
+    expect(!keys.left);
+    expect(!keys.right);
+}
test/stage1/behavior.zig
@@ -25,6 +25,7 @@ comptime {
     _ = @import("behavior/bugs/655.zig");
     _ = @import("behavior/bugs/656.zig");
     _ = @import("behavior/bugs/704.zig");
+    _ = @import("behavior/bugs/718.zig");
     _ = @import("behavior/bugs/726.zig");
     _ = @import("behavior/bugs/828.zig");
     _ = @import("behavior/bugs/920.zig");