zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit e584558bd8533ce91d1683cdbc6b77d7bb652acf (tree)
parent e42f83825f1473661700f89ffd3060013261d605
Author: Veikka Tuominen <git@vexu.eu>
Date:   Mon, 19 Sep 2022 12:38:31 +0300

Sema: do not use coerceCompatiblePtr for alignCast

Closes  #12902

Diffstat:
Msrc/Sema.zig | 25++++++++-----------------
Mtest/behavior/align.zig | 8++++++++
2 files changed, 16 insertions(+), 17 deletions(-)

diff --git a/src/Sema.zig b/src/Sema.zig @@ -17931,23 +17931,14 @@ fn zirAlignCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A const ptr = try sema.resolveInst(extra.rhs); const ptr_ty = sema.typeOf(ptr); - // TODO in addition to pointers, this instruction is supposed to work for - // pointer-like optionals and slices. try sema.checkPtrOperand(block, ptr_src, ptr_ty); - // TODO compile error if the result pointer is comptime known and would have an - // alignment that disagrees with the Decl's alignment. - - const ptr_info = ptr_ty.ptrInfo().data; - const dest_ty = try Type.ptr(sema.arena, sema.mod, .{ - .pointee_type = ptr_info.pointee_type, - .@"align" = dest_align, - .@"addrspace" = ptr_info.@"addrspace", - .mutable = ptr_info.mutable, - .@"allowzero" = ptr_info.@"allowzero", - .@"volatile" = ptr_info.@"volatile", - .size = ptr_info.size, - }); + var ptr_info = ptr_ty.ptrInfo().data; + ptr_info.@"align" = dest_align; + var dest_ty = try Type.ptr(sema.arena, sema.mod, ptr_info); + if (ptr_ty.zigTypeTag() == .Optional) { + dest_ty = try Type.Tag.optional.create(sema.arena, dest_ty); + } if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |val| { 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 try sema.requireRuntimeBlock(block, inst_data.src(), ptr_src); if (block.wantSafety() and dest_align > 1 and - try sema.typeHasRuntimeBits(block, sema.src, dest_ty.elemType2())) + try sema.typeHasRuntimeBits(block, sema.src, ptr_info.pointee_type)) { const val_payload = try sema.arena.create(Value.Payload.U64); val_payload.* = .{ @@ -17985,7 +17976,7 @@ fn zirAlignCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A } else is_aligned; try sema.addSafetyCheck(block, ok, .incorrect_alignment); } - return sema.coerceCompatiblePtrs(block, dest_ty, ptr, ptr_src); + return sema.bitCast(block, dest_ty, ptr, ptr_src); } fn zirBitCount( diff --git a/test/behavior/align.zig b/test/behavior/align.zig @@ -556,3 +556,11 @@ test "comptime alloc alignment" { var bytes2_addr = @ptrToInt(&bytes2); try expect(bytes2_addr & 0xff == 0); } + +test "@alignCast null" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + + var ptr: ?*anyopaque = null; + const aligned: ?*anyopaque = @alignCast(@alignOf(?*anyopaque), ptr); + try expect(aligned == null); +}