authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-19 12:38:31+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-19 18:34:52+03:00
loge584558bd8533ce91d1683cdbc6b77d7bb652acf
tree640be3ada9fe338c396adbae29e6dccc54987d71
parente42f83825f1473661700f89ffd3060013261d605

Sema: do not use coerceCompatiblePtr for alignCast

Closes #12902

2 files changed, 16 insertions(+), 17 deletions(-)

src/Sema.zig+8-17
...@@ -17931,23 +17931,14 @@ fn zirAlignCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -17931,23 +17931,14 @@ fn zirAlignCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
17931 const ptr = try sema.resolveInst(extra.rhs);17931 const ptr = try sema.resolveInst(extra.rhs);
17932 const ptr_ty = sema.typeOf(ptr);17932 const ptr_ty = sema.typeOf(ptr);
1793317933
17934 // TODO in addition to pointers, this instruction is supposed to work for
17935 // pointer-like optionals and slices.
17936 try sema.checkPtrOperand(block, ptr_src, ptr_ty);17934 try sema.checkPtrOperand(block, ptr_src, ptr_ty);
1793717935
17938 // TODO compile error if the result pointer is comptime known and would have an17936 var ptr_info = ptr_ty.ptrInfo().data;
17939 // alignment that disagrees with the Decl's alignment.17937 ptr_info.@"align" = dest_align;
1794017938 var dest_ty = try Type.ptr(sema.arena, sema.mod, ptr_info);
17941 const ptr_info = ptr_ty.ptrInfo().data;17939 if (ptr_ty.zigTypeTag() == .Optional) {
17942 const dest_ty = try Type.ptr(sema.arena, sema.mod, .{17940 dest_ty = try Type.Tag.optional.create(sema.arena, dest_ty);
17943 .pointee_type = ptr_info.pointee_type,17941 }
17944 .@"align" = dest_align,
17945 .@"addrspace" = ptr_info.@"addrspace",
17946 .mutable = ptr_info.mutable,
17947 .@"allowzero" = ptr_info.@"allowzero",
17948 .@"volatile" = ptr_info.@"volatile",
17949 .size = ptr_info.size,
17950 });
1795117942
17952 if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |val| {17943 if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |val| {
17953 if (try val.getUnsignedIntAdvanced(sema.mod.getTarget(), null)) |addr| {17944 if (try val.getUnsignedIntAdvanced(sema.mod.getTarget(), null)) |addr| {
...@@ -17960,7 +17951,7 @@ fn zirAlignCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -17960,7 +17951,7 @@ fn zirAlignCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1796017951
17961 try sema.requireRuntimeBlock(block, inst_data.src(), ptr_src);17952 try sema.requireRuntimeBlock(block, inst_data.src(), ptr_src);
17962 if (block.wantSafety() and dest_align > 1 and17953 if (block.wantSafety() and dest_align > 1 and
17963 try sema.typeHasRuntimeBits(block, sema.src, dest_ty.elemType2()))17954 try sema.typeHasRuntimeBits(block, sema.src, ptr_info.pointee_type))
17964 {17955 {
17965 const val_payload = try sema.arena.create(Value.Payload.U64);17956 const val_payload = try sema.arena.create(Value.Payload.U64);
17966 val_payload.* = .{17957 val_payload.* = .{
...@@ -17985,7 +17976,7 @@ fn zirAlignCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -17985,7 +17976,7 @@ fn zirAlignCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
17985 } else is_aligned;17976 } else is_aligned;
17986 try sema.addSafetyCheck(block, ok, .incorrect_alignment);17977 try sema.addSafetyCheck(block, ok, .incorrect_alignment);
17987 }17978 }
17988 return sema.coerceCompatiblePtrs(block, dest_ty, ptr, ptr_src);17979 return sema.bitCast(block, dest_ty, ptr, ptr_src);
17989}17980}
1799017981
17991fn zirBitCount(17982fn zirBitCount(
test/behavior/align.zig+8
...@@ -556,3 +556,11 @@ test "comptime alloc alignment" {...@@ -556,3 +556,11 @@ test "comptime alloc alignment" {
556 var bytes2_addr = @ptrToInt(&bytes2);556 var bytes2_addr = @ptrToInt(&bytes2);
557 try expect(bytes2_addr & 0xff == 0);557 try expect(bytes2_addr & 0xff == 0);
558}558}
559
560test "@alignCast null" {
561 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
562
563 var ptr: ?*anyopaque = null;
564 const aligned: ?*anyopaque = @alignCast(@alignOf(?*anyopaque), ptr);
565 try expect(aligned == null);
566}