diff --git a/src/Sema.zig b/src/Sema.zig index 55d33d47e112501c215b1a9f3d2d16611bfa6e00..2ee64ca73de2c1995a78229624f21dae42c9f7bf 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -3203,11 +3203,6 @@ fn zirValidateArrayInit( // Determine whether the value stored to this pointer is comptime-known. - if (opt_opv) |opv| { - element_vals[i] = opv; - continue; - } - const elem_ptr_air_ref = sema.inst_map.get(elem_ptr).?; const elem_ptr_air_inst = Air.refToIndex(elem_ptr_air_ref).?; // Find the block index of the elem_ptr so that we can look at the next @@ -3223,6 +3218,12 @@ fn zirValidateArrayInit( break :inst block.instructions.items[block_index + 1]; }; + // Array has one possible value, so value is always comptime-known + if (opt_opv) |opv| { + element_vals[i] = opv; + continue; + } + // If the next instructon is a store with a comptime operand, this element // is comptime. switch (air_tags[next_air_inst]) { diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 692edd48c15d771a9f0e7f8df8140ec43e819495..343e503aea837ad287d505785ec441d16a371793 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -5422,21 +5422,22 @@ pub const FuncGen = struct { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const dest_ptr = try self.resolveInst(bin_op.lhs); const ptr_ty = self.air.typeOf(bin_op.lhs); + const operand_ty = ptr_ty.childType(); + if (!operand_ty.isFnOrHasRuntimeBitsIgnoreComptime()) return null; // TODO Sema should emit a different instruction when the store should // possibly do the safety 0xaa bytes for undefined. const val_is_undef = if (self.air.value(bin_op.rhs)) |val| val.isUndefDeep() else false; if (val_is_undef) { - const elem_ty = ptr_ty.childType(); const target = self.dg.module.getTarget(); - const elem_size = elem_ty.abiSize(target); + const operand_size = operand_ty.abiSize(target); const u8_llvm_ty = self.context.intType(8); const ptr_u8_llvm_ty = u8_llvm_ty.pointerType(0); const dest_ptr_u8 = self.builder.buildBitCast(dest_ptr, ptr_u8_llvm_ty, ""); const fill_char = u8_llvm_ty.constInt(0xaa, .False); const dest_ptr_align = ptr_ty.ptrAlignment(target); const usize_llvm_ty = try self.dg.llvmType(Type.usize); - const len = usize_llvm_ty.constInt(elem_size, .False); + const len = usize_llvm_ty.constInt(operand_size, .False); _ = self.builder.buildMemSet(dest_ptr_u8, fill_char, len, dest_ptr_align, ptr_ty.isVolatilePtr()); if (self.dg.module.comp.bin_file.options.valgrind) { // TODO generate valgrind client request to mark byte range as undefined diff --git a/test/behavior.zig b/test/behavior.zig index edc9953e59b08f074f0b3bf04769a3c51b5a8df1..f142464dc3ff3cf49f5f09a1c0641ddcf8a30331 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -65,8 +65,11 @@ test { _ = @import("behavior/bugs/10970.zig"); _ = @import("behavior/bugs/11046.zig"); _ = @import("behavior/bugs/11139.zig"); + _ = @import("behavior/bugs/11159.zig"); + _ = @import("behavior/bugs/11162.zig"); _ = @import("behavior/bugs/11165.zig"); _ = @import("behavior/bugs/11181.zig"); + _ = @import("behavior/bugs/11182.zig"); _ = @import("behavior/call.zig"); _ = @import("behavior/cast.zig"); _ = @import("behavior/comptime_memory.zig"); diff --git a/test/behavior/bugs/11159.zig b/test/behavior/bugs/11159.zig new file mode 100644 index 0000000000000000000000000000000000000000..fdd380705923821eb4ae54311d77dd166b648129 --- /dev/null +++ b/test/behavior/bugs/11159.zig @@ -0,0 +1,23 @@ +const std = @import("std"); +const builtin = @import("builtin"); + +test { + const T = @TypeOf(.{ @as(i32, 0), @as(u32, 0) }); + var a: T = .{ 0, 0 }; + _ = a; +} + +test { + if (builtin.zig_backend == .stage1) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO + + const S = struct { + comptime x: i32 = 0, + comptime y: u32 = 0, + }; + var a: S = .{}; + _ = a; + var b = S{}; + _ = b; +} diff --git a/test/behavior/bugs/11162.zig b/test/behavior/bugs/11162.zig new file mode 100644 index 0000000000000000000000000000000000000000..ced435a30573ee6a996dcff7a758f6ee4c84efee --- /dev/null +++ b/test/behavior/bugs/11162.zig @@ -0,0 +1,16 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const expect = std.testing.expect; + +test { + if (builtin.zig_backend == .stage1) return error.SkipZigTest; // TODO + if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO + + var x: u32 = 15; + const T = @TypeOf(.{ @as(i32, -1234), @as(u32, 5678), x }); + var a: T = .{ -1234, 5678, x + 1 }; + + try expect(a[0] == -1234); + try expect(a[1] == 5678); + try expect(a[2] == 16); +} diff --git a/test/behavior/bugs/11182.zig b/test/behavior/bugs/11182.zig new file mode 100644 index 0000000000000000000000000000000000000000..eb4ee8ac9d9d9fa6fd6716d91d41ebc9ea263e98 --- /dev/null +++ b/test/behavior/bugs/11182.zig @@ -0,0 +1,10 @@ +const std = @import("std"); +const builtin = @import("builtin"); + +test { + if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO + + const T = @TypeOf(.{ @as(i32, 0), @as(u32, 0) }); + var a = T{ 0, 0 }; + _ = a; +}