diff --git a/src/Sema.zig b/src/Sema.zig index dfb71e08b..d86a8ef2f 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 ed390d96a..64f3c1d37 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;