mirror of
https://github.com/ziglang/zig.git
synced 2025-12-24 15:13:08 +00:00
parent
e42f83825f
commit
e584558bd8
25
src/Sema.zig
25
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(
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user