authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-10-25 03:41:23+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-10-25 20:41:15+02:00
log4eb7b28700b23d8465a36e364e60394b2a1da41b
treecb46cfbbd0a3efc2dc2862cee7a871c593427e9b
parent7062c8a8865bbd2fb8181b579da552295cd68e6a

stage2: generate correct constants for zero-sized arrays


3 files changed, 12 insertions(+), 12 deletions(-)

src/codegen/llvm.zig+3-3
...@@ -762,7 +762,7 @@ pub const DeclGen = struct {...@@ -762,7 +762,7 @@ pub const DeclGen = struct {
762 }762 }
763 const llvm_addrspace = dg.llvmAddressSpace(t.ptrAddressSpace());763 const llvm_addrspace = dg.llvmAddressSpace(t.ptrAddressSpace());
764 const elem_ty = t.childType();764 const elem_ty = t.childType();
765 const llvm_elem_ty = if (elem_ty.hasCodeGenBits())765 const llvm_elem_ty = if (elem_ty.hasCodeGenBits() or elem_ty.zigTypeTag() == .Array)
766 try dg.llvmType(elem_ty)766 try dg.llvmType(elem_ty)
767 else767 else
768 dg.context.intType(8);768 dg.context.intType(8);
...@@ -1475,7 +1475,7 @@ pub const DeclGen = struct {...@@ -1475,7 +1475,7 @@ pub const DeclGen = struct {
1475 }1475 }
14761476
1477 const llvm_type = try self.llvmType(tv.ty);1477 const llvm_type = try self.llvmType(tv.ty);
1478 if (!tv.ty.childType().hasCodeGenBits()) {1478 if (!tv.ty.childType().hasCodeGenBits() or !decl.ty.hasCodeGenBits()) {
1479 return self.lowerPtrToVoid(tv.ty);1479 return self.lowerPtrToVoid(tv.ty);
1480 }1480 }
14811481
...@@ -1497,7 +1497,7 @@ pub const DeclGen = struct {...@@ -1497,7 +1497,7 @@ pub const DeclGen = struct {
1497 // for non-optional pointers. We also need to respect the alignment, even though1497 // for non-optional pointers. We also need to respect the alignment, even though
1498 // the address will never be dereferenced.1498 // the address will never be dereferenced.
1499 const llvm_usize = try dg.llvmType(Type.usize);1499 const llvm_usize = try dg.llvmType(Type.usize);
1500 const llvm_ptr_ty = dg.context.intType(8).pointerType(0);1500 const llvm_ptr_ty = try dg.llvmType(ptr_ty);
1501 if (alignment != 0) {1501 if (alignment != 0) {
1502 return llvm_usize.constInt(alignment, .False).constIntToPtr(llvm_ptr_ty);1502 return llvm_usize.constInt(alignment, .False).constIntToPtr(llvm_ptr_ty);
1503 }1503 }
test/behavior/slice.zig+9
...@@ -157,3 +157,12 @@ test "comptime pointer cast array and then slice" {...@@ -157,3 +157,12 @@ test "comptime pointer cast array and then slice" {
157 try expect(sliceA[1] == 2);157 try expect(sliceA[1] == 2);
158 try expect(sliceB[1] == 2);158 try expect(sliceB[1] == 2);
159}159}
160
161test "slicing zero length array" {
162 const s1 = ""[0..];
163 const s2 = ([_]u32{})[0..];
164 try expect(s1.len == 0);
165 try expect(s2.len == 0);
166 try expect(mem.eql(u8, s1, ""));
167 try expect(mem.eql(u32, s2, &[_]u32{}));
168}
test/behavior/slice_stage1.zig-9
...@@ -4,15 +4,6 @@ const expectEqualSlices = std.testing.expectEqualSlices;...@@ -4,15 +4,6 @@ const expectEqualSlices = std.testing.expectEqualSlices;
4const expectEqual = std.testing.expectEqual;4const expectEqual = std.testing.expectEqual;
5const mem = std.mem;5const mem = std.mem;
66
7test "slicing zero length array" {
8 const s1 = ""[0..];
9 const s2 = ([_]u32{})[0..];
10 try expect(s1.len == 0);
11 try expect(s2.len == 0);
12 try expect(mem.eql(u8, s1, ""));
13 try expect(mem.eql(u32, s2, &[_]u32{}));
14}
15
16test "slice string literal has correct type" {7test "slice string literal has correct type" {
17 comptime {8 comptime {
18 try expect(@TypeOf("aoeu"[0..]) == *const [4:0]u8);9 try expect(@TypeOf("aoeu"[0..]) == *const [4:0]u8);