From 100efcf8d3e7fb7562d51b011dfaf639bf93c9ba Mon Sep 17 00:00:00 2001 From: David Rubin <87927264+Rexicon226@users.noreply.github.com> Date: Fri, 19 Jan 2024 11:25:05 -0800 Subject: [PATCH] return optional state to `zirPtrCastNoDest` --- src/Sema.zig | 9 ++++++++- test/behavior/cast.zig | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Sema.zig b/src/Sema.zig index dfb71e08b1..d86a8ef2f0 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -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()); diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index ed390d96a3..64f3c1d376 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -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;