| author | |
| committer | |
| log | f24c77fc48b7272618b48cac7bb15d6997e95c5a |
| tree | cc75a53ed2774ae1556f0f0d5a16c2b60b9eb32a |
| parent | 4809e0ea7f53d1b56cbc4c6e62e4b8ea40936d44 |
| parent | e0bc5f65b98d154b4318027d56f780b55605e33c |
| signature | Signed by PGP key 4AEE18F83AFDEB23 |
llvm: fix null pointer use when lowering pointer to final zero-width field of a comptime value4 files changed, 42 insertions(+), 24 deletions(-)
lib/std/rand/Xoroshiro128.zig+2-2| ... | @@ -20,7 +20,7 @@ pub fn random(self: *Xoroshiro128) Random { | ... | @@ -20,7 +20,7 @@ pub fn random(self: *Xoroshiro128) Random { |
| 20 | return Random.init(self, fill); | 20 | return Random.init(self, fill); |
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | fn next(self: *Xoroshiro128) u64 { | 23 | pub fn next(self: *Xoroshiro128) u64 { |
| 24 | const s0 = self.s[0]; | 24 | const s0 = self.s[0]; |
| 25 | var s1 = self.s[1]; | 25 | var s1 = self.s[1]; |
| 26 | const r = s0 +% s1; | 26 | const r = s0 +% s1; |
| ... | @@ -33,7 +33,7 @@ fn next(self: *Xoroshiro128) u64 { | ... | @@ -33,7 +33,7 @@ fn next(self: *Xoroshiro128) u64 { |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | // Skip 2^64 places ahead in the sequence | 35 | // Skip 2^64 places ahead in the sequence |
| 36 | fn jump(self: *Xoroshiro128) void { | 36 | pub fn jump(self: *Xoroshiro128) void { |
| 37 | var s0: u64 = 0; | 37 | var s0: u64 = 0; |
| 38 | var s1: u64 = 0; | 38 | var s1: u64 = 0; |
| 39 | 39 |
lib/std/rand/Xoshiro256.zig+2-2| ... | @@ -22,7 +22,7 @@ pub fn random(self: *Xoshiro256) Random { | ... | @@ -22,7 +22,7 @@ pub fn random(self: *Xoshiro256) Random { |
| 22 | return Random.init(self, fill); | 22 | return Random.init(self, fill); |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | fn next(self: *Xoshiro256) u64 { | 25 | pub fn next(self: *Xoshiro256) u64 { |
| 26 | const r = math.rotl(u64, self.s[0] +% self.s[3], 23) +% self.s[0]; | 26 | const r = math.rotl(u64, self.s[0] +% self.s[3], 23) +% self.s[0]; |
| 27 | 27 | ||
| 28 | const t = self.s[1] << 17; | 28 | const t = self.s[1] << 17; |
| ... | @@ -40,7 +40,7 @@ fn next(self: *Xoshiro256) u64 { | ... | @@ -40,7 +40,7 @@ fn next(self: *Xoshiro256) u64 { |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | // Skip 2^128 places ahead in the sequence | 42 | // Skip 2^128 places ahead in the sequence |
| 43 | fn jump(self: *Xoshiro256) void { | 43 | pub fn jump(self: *Xoshiro256) void { |
| 44 | var s: u256 = 0; | 44 | var s: u256 = 0; |
| 45 | 45 | ||
| 46 | var table: u256 = 0x39abdc4529b1661ca9582618e03fc9aad5a61266f0c9392c180ec6d33cfd0aba; | 46 | var table: u256 = 0x39abdc4529b1661ca9582618e03fc9aad5a61266f0c9392c180ec6d33cfd0aba; |
src/codegen/llvm.zig+15-7| ... | @@ -4033,16 +4033,24 @@ pub const DeclGen = struct { | ... | @@ -4033,16 +4033,24 @@ pub const DeclGen = struct { |
| 4033 | const final_llvm_ty = (try dg.lowerType(ptr_child_ty)).pointerType(0); | 4033 | const final_llvm_ty = (try dg.lowerType(ptr_child_ty)).pointerType(0); |
| 4034 | break :blk field_addr.constIntToPtr(final_llvm_ty); | 4034 | break :blk field_addr.constIntToPtr(final_llvm_ty); |
| 4035 | } | 4035 | } |
| 4036 | bitcast_needed = !field_ty.eql(ptr_child_ty, dg.module); | ||
| 4037 | 4036 | ||
| 4038 | var ty_buf: Type.Payload.Pointer = undefined; | 4037 | var ty_buf: Type.Payload.Pointer = undefined; |
| 4039 | const llvm_field_index = llvmFieldIndex(parent_ty, field_index, target, &ty_buf).?; | 4038 | |
| 4040 | const indices: [2]*llvm.Value = .{ | ||
| 4041 | llvm_u32.constInt(0, .False), | ||
| 4042 | llvm_u32.constInt(llvm_field_index, .False), | ||
| 4043 | }; | ||
| 4044 | const parent_llvm_ty = try dg.lowerType(parent_ty); | 4039 | const parent_llvm_ty = try dg.lowerType(parent_ty); |
| 4045 | break :blk parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); | 4040 | if (llvmFieldIndex(parent_ty, field_index, target, &ty_buf)) |llvm_field_index| { |
| 4041 | bitcast_needed = !field_ty.eql(ptr_child_ty, dg.module); | ||
| 4042 | const indices: [2]*llvm.Value = .{ | ||
| 4043 | llvm_u32.constInt(0, .False), | ||
| 4044 | llvm_u32.constInt(llvm_field_index, .False), | ||
| 4045 | }; | ||
| 4046 | break :blk parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); | ||
| 4047 | } else { | ||
| 4048 | bitcast_needed = !parent_ty.eql(ptr_child_ty, dg.module); | ||
| 4049 | const indices: [1]*llvm.Value = .{ | ||
| 4050 | llvm_u32.constInt(1, .False), | ||
| 4051 | }; | ||
| 4052 | break :blk parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); | ||
| 4053 | } | ||
| 4046 | }, | 4054 | }, |
| 4047 | .Pointer => { | 4055 | .Pointer => { |
| 4048 | assert(parent_ty.isSlice()); | 4056 | assert(parent_ty.isSlice()); |
test/behavior/struct.zig+23-13| ... | @@ -1359,23 +1359,33 @@ test "under-aligned struct field" { | ... | @@ -1359,23 +1359,33 @@ test "under-aligned struct field" { |
| 1359 | try expect(result == 1234); | 1359 | try expect(result == 1234); |
| 1360 | } | 1360 | } |
| 1361 | 1361 | ||
| 1362 | test "address of zero-bit field is equal to address of only field" { | 1362 | test "fieldParentPtr of a zero-bit field" { |
| 1363 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO | 1363 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO |
| 1364 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO | 1364 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO |
| 1365 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO | 1365 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 1366 | 1366 | ||
| 1367 | { | 1367 | const S = struct { |
| 1368 | const A = struct { b: void = {}, u: u8 }; | 1368 | fn testOneType(comptime A: type) !void { |
| 1369 | var a = A{ .u = 0 }; | 1369 | { |
| 1370 | const a_ptr = @fieldParentPtr(A, "b", &a.b); | 1370 | const a = A{ .u = 0 }; |
| 1371 | try std.testing.expectEqual(&a, a_ptr); | 1371 | const b_ptr = &a.b; |
| 1372 | } | 1372 | const a_ptr = @fieldParentPtr(A, "b", b_ptr); |
| 1373 | { | 1373 | try std.testing.expectEqual(&a, a_ptr); |
| 1374 | const A = struct { u: u8, b: void = {} }; | 1374 | } |
| 1375 | var a = A{ .u = 0 }; | 1375 | { |
| 1376 | const a_ptr = @fieldParentPtr(A, "b", &a.b); | 1376 | var a = A{ .u = 0 }; |
| 1377 | try std.testing.expectEqual(&a, a_ptr); | 1377 | const b_ptr = &a.b; |
| 1378 | } | 1378 | const a_ptr = @fieldParentPtr(A, "b", b_ptr); |
| 1379 | try std.testing.expectEqual(&a, a_ptr); | ||
| 1380 | } | ||
| 1381 | } | ||
| 1382 | fn doTheTest() !void { | ||
| 1383 | try testOneType(struct { b: void = {}, u: u8 }); | ||
| 1384 | try testOneType(struct { u: u8, b: void = {} }); | ||
| 1385 | } | ||
| 1386 | }; | ||
| 1387 | try S.doTheTest(); | ||
| 1388 | comptime try S.doTheTest(); | ||
| 1379 | } | 1389 | } |
| 1380 | 1390 | ||
| 1381 | test "struct field has a pointer to an aligned version of itself" { | 1391 | test "struct field has a pointer to an aligned version of itself" { |