mirror of
https://github.com/ziglang/zig.git
synced 2026-02-12 20:37:54 +00:00
make addrSpaceCast work with optionals; forbid ptrCast'ing address spaces
This commit is contained in:
parent
aa20295d24
commit
ad74773959
24
src/Sema.zig
24
src/Sema.zig
@ -18181,11 +18181,10 @@ fn zirAddrSpaceCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Inst
|
||||
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);
|
||||
|
||||
const src_addrspace = ptr_ty.ptrAddressSpace();
|
||||
var ptr_info = ptr_ty.ptrInfo().data;
|
||||
const src_addrspace = ptr_info.@"addrspace";
|
||||
if (!target_util.addrSpaceCastIsValid(sema.mod.getTarget(), src_addrspace, dest_addrspace)) {
|
||||
const msg = msg: {
|
||||
const msg = try sema.errMsg(block, src, "invalid address space cast", .{});
|
||||
@ -18196,16 +18195,12 @@ fn zirAddrSpaceCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Inst
|
||||
return sema.failWithOwnedErrorMsg(msg);
|
||||
}
|
||||
|
||||
const ptr_info = ptr_ty.ptrInfo().data;
|
||||
const dest_ty = try Type.ptr(sema.arena, sema.mod, .{
|
||||
.pointee_type = ptr_info.pointee_type,
|
||||
.@"align" = ptr_info.@"align",
|
||||
.@"addrspace" = dest_addrspace,
|
||||
.mutable = ptr_info.mutable,
|
||||
.@"allowzero" = ptr_info.@"allowzero",
|
||||
.@"volatile" = ptr_info.@"volatile",
|
||||
.size = ptr_info.size,
|
||||
});
|
||||
ptr_info.@"addrspace" = dest_addrspace;
|
||||
const dest_ptr_ty = try Type.ptr(sema.arena, sema.mod, ptr_info);
|
||||
const dest_ty = if (ptr_ty.zigTypeTag() == .Optional)
|
||||
try Type.optional(sema.arena, dest_ptr_ty)
|
||||
else
|
||||
dest_ptr_ty;
|
||||
|
||||
if (try sema.resolveMaybeUndefVal(block, ptr_src, ptr)) |val| {
|
||||
// Pointer value should compatible with both address spaces.
|
||||
@ -18472,6 +18467,9 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
|
||||
if (operand_info.@"volatile" and !dest_info.@"volatile") {
|
||||
return sema.fail(block, src, "cast discards volatile qualifier", .{});
|
||||
}
|
||||
if (operand_info.@"addrspace" != dest_info.@"addrspace") {
|
||||
return sema.fail(block, src, "cast changes pointer address space", .{});
|
||||
}
|
||||
|
||||
const dest_is_slice = dest_ty.isSlice();
|
||||
const operand_is_slice = operand_ty.isSlice();
|
||||
|
||||
@ -2786,6 +2786,12 @@ pub const Type = extern union {
|
||||
|
||||
.pointer => self.castTag(.pointer).?.data.@"addrspace",
|
||||
|
||||
.optional => {
|
||||
var buf: Payload.ElemType = undefined;
|
||||
const child_type = self.optionalChild(&buf);
|
||||
return child_type.ptrAddressSpace();
|
||||
},
|
||||
|
||||
else => unreachable,
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user