return optional state to zirPtrCastNoDest

This commit is contained in:
David Rubin 2024-01-19 11:25:05 -08:00 committed by GitHub
parent b80cad2484
commit 100efcf8d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View File

@ -22741,7 +22741,14 @@ fn zirPtrCastNoDest(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Inst
var ptr_info = operand_ty.ptrInfo(mod);
if (flags.const_cast) ptr_info.flags.is_const = false;
if (flags.volatile_cast) ptr_info.flags.is_volatile = false;
const dest_ty = try sema.ptrType(ptr_info);
const dest_ty = blk: {
const dest_ty = try sema.ptrType(ptr_info);
if (operand_ty.zigTypeTag(mod) == .Optional) {
break :blk try mod.optionalType(dest_ty.toIntern());
}
break :blk dest_ty;
};
if (try sema.resolveValue(operand)) |operand_val| {
return Air.internedToRef((try mod.getCoerced(operand_val, dest_ty)).toIntern());

View File

@ -1585,6 +1585,13 @@ test "@constCast without a result location" {
try expect(y.* == 1234);
}
test "@constCast optional" {
const x: u8 = 10;
const m: ?*const u8 = &x;
const p = @constCast(m);
try expect(@TypeOf(p) == ?*u8);
}
test "@volatileCast without a result location" {
var x: i32 = 1234;
const y: *volatile i32 = &x;