authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-01-19 11:25:05-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-01-19 21:25:05+02:00
log100efcf8d3e7fb7562d51b011dfaf639bf93c9ba
tree08eaaf37a83154cb0624ca82f70784a0f79feeb8
parentb80cad24840700ead20b59734916bf3d4d4ba87c
signaturebadge-check Signed by PGP key B5690EEEBB952194

return optional state to `zirPtrCastNoDest`


2 files changed, 15 insertions(+), 1 deletions(-)

src/Sema.zig+8-1
...@@ -22741,7 +22741,14 @@ fn zirPtrCastNoDest(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Inst...@@ -22741,7 +22741,14 @@ fn zirPtrCastNoDest(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Inst
22741 var ptr_info = operand_ty.ptrInfo(mod);22741 var ptr_info = operand_ty.ptrInfo(mod);
22742 if (flags.const_cast) ptr_info.flags.is_const = false;22742 if (flags.const_cast) ptr_info.flags.is_const = false;
22743 if (flags.volatile_cast) ptr_info.flags.is_volatile = false;22743 if (flags.volatile_cast) ptr_info.flags.is_volatile = false;
22744 const dest_ty = try sema.ptrType(ptr_info);22744
22745 const dest_ty = blk: {
22746 const dest_ty = try sema.ptrType(ptr_info);
22747 if (operand_ty.zigTypeTag(mod) == .Optional) {
22748 break :blk try mod.optionalType(dest_ty.toIntern());
22749 }
22750 break :blk dest_ty;
22751 };
2274522752
22746 if (try sema.resolveValue(operand)) |operand_val| {22753 if (try sema.resolveValue(operand)) |operand_val| {
22747 return Air.internedToRef((try mod.getCoerced(operand_val, dest_ty)).toIntern());22754 return Air.internedToRef((try mod.getCoerced(operand_val, dest_ty)).toIntern());
test/behavior/cast.zig+7
...@@ -1585,6 +1585,13 @@ test "@constCast without a result location" {...@@ -1585,6 +1585,13 @@ test "@constCast without a result location" {
1585 try expect(y.* == 1234);1585 try expect(y.* == 1234);
1586}1586}
15871587
1588test "@constCast optional" {
1589 const x: u8 = 10;
1590 const m: ?*const u8 = &x;
1591 const p = @constCast(m);
1592 try expect(@TypeOf(p) == ?*u8);
1593}
1594
1588test "@volatileCast without a result location" {1595test "@volatileCast without a result location" {
1589 var x: i32 = 1234;1596 var x: i32 = 1234;
1590 const y: *volatile i32 = &x;1597 const y: *volatile i32 = &x;