aboutsummaryrefslogtreecommitdiff
path: root/src/Sema.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2025-06-29 16:39:26 -0700
committerAndrew Kelley <andrew@ziglang.org>2025-06-29 17:20:23 -0700
commit7999374b21795a7b8fac7af0b2e326d39bd1e839 (patch)
tree14a3344acff87876a43897211912115f592b771d /src/Sema.zig
parente8377659560497beb36ec1f981b024f74292c1b9 (diff)
downloadzig-7999374b21795a7b8fac7af0b2e326d39bd1e839.tar.gz
zig-7999374b21795a7b8fac7af0b2e326d39bd1e839.zip
Sema: correct OPV for optional empty error set
prevents crashes in backends; improves codegen; provides more comptime-ness.
Diffstat (limited to 'src/Sema.zig')
-rw-r--r--src/Sema.zig22
1 files changed, 17 insertions, 5 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index 20be9e7052..68890c1f6f 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -9065,10 +9065,14 @@ fn zirOptionalPayload(
};
if (try sema.resolveDefinedValue(block, src, operand)) |val| {
- return if (val.optionalValue(zcu)) |payload|
- Air.internedToRef(payload.toIntern())
- else
- sema.fail(block, src, "unable to unwrap null", .{});
+ if (val.optionalValue(zcu)) |payload| return Air.internedToRef(payload.toIntern());
+ if (block.isComptime()) return sema.fail(block, src, "unable to unwrap null", .{});
+ if (safety_check and block.wantSafety()) {
+ try sema.safetyPanic(block, src, .unwrap_null);
+ } else {
+ _ = try block.addNoOp(.unreach);
+ }
+ return .unreachable_value;
}
try sema.requireRuntimeBlock(block, src, null);
@@ -36443,7 +36447,6 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
.type_int_unsigned, // u0 handled above
.type_pointer,
.type_slice,
- .type_optional, // ?noreturn handled above
.type_anyframe,
.type_error_union,
.type_anyerror_union,
@@ -36655,6 +36658,15 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
else => unreachable,
},
+
+ .type_optional => {
+ const payload_ip = ip.indexToKey(ty.toIntern()).opt_type;
+ // Although ?noreturn is handled above, the element type
+ // can be effectively noreturn for example via an empty
+ // enum or error set.
+ if (ip.isNoReturn(payload_ip)) return try pt.nullValue(ty);
+ return null;
+ },
},
};
}