commit 100efcf8d3e7fb7562d51b011dfaf639bf93c9ba (tree)
parent b80cad24840700ead20b59734916bf3d4d4ba87c
Author: David Rubin <87927264+Rexicon226@users.noreply.github.com>
Date: Fri, 19 Jan 2024 11:25:05 -0800
return optional state to `zirPtrCastNoDest`
Diffstat:
2 files changed, 15 insertions(+), 1 deletion(-)
diff --git 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
@@ -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;