Commit 100efcf8d3

David Rubin <87927264+Rexicon226@users.noreply.github.com>
2024-01-19 20:25:05
return optional state to `zirPtrCastNoDest`
1 parent b80cad2
Changed files (2)
src
test
behavior
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());
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;