authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-11-28 15:05:43+11:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2024-01-09 14:42:12+11:00
log2fa69cce7261d223cbbc4b824a4da3ef41805214
treec58f28efbfd70b67b1f523c14c657341542baddb
parenta175a6438400d3e8842b4a4ac6a8cb76f53976a0

sema: allow maybeErrorUnwrap to handle err_union_code


1 files changed, 20 insertions(+), 8 deletions(-)

src/Sema.zig+20-8
......@@ -11253,7 +11253,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1125311253 var spa: SwitchProngAnalysis = .{
1125411254 .sema = sema,
1125511255 .parent_block = block,
11256 .operand = raw_operand_val,
11256 .operand = undefined, // must be set to the unwrapped error code before use
1125711257 .operand_ptr = .none,
1125811258 .cond = raw_operand_val,
1125911259 .else_error_ty = else_error_ty,
......@@ -11387,6 +11387,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1138711387 undefined,
1138811388 undefined,
1138911389 cond_dbg_node_index,
11390 true,
1139011391 );
1139111392
1139211393 try sema.air_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.CondBr).Struct.fields.len +
......@@ -11970,7 +11971,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1197011971 if (special_prong == .none) {
1197111972 return sema.fail(block, src, "switch must handle all possibilities", .{});
1197211973 }
11973 if (err_set and try sema.maybeErrorUnwrap(block, special.body, operand, operand_src)) {
11974 if (err_set and try sema.maybeErrorUnwrap(block, special.body, operand, operand_src, false)) {
1197411975 return .unreachable_value;
1197511976 }
1197611977 if (mod.backendSupportsFeature(.is_named_enum_value) and block.wantSafety() and operand_ty.zigTypeTag(mod) == .Enum and
......@@ -12024,6 +12025,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1202412025 true_count,
1202512026 false_count,
1202612027 cond_dbg_node_index,
12028 false,
1202712029 );
1202812030
1202912031 return sema.analyzeBlockBody(block, src, &child_block, merges);
......@@ -12060,6 +12062,7 @@ fn analyzeSwitchRuntimeBlock(
1206012062 true_count: u8,
1206112063 false_count: u8,
1206212064 cond_dbg_node_index: Zir.Inst.Index,
12065 allow_err_code_unwrap: bool,
1206312066) CompileError!Air.Inst.Ref {
1206412067 const mod = sema.mod;
1206512068 const gpa = sema.gpa;
......@@ -12101,7 +12104,7 @@ fn analyzeSwitchRuntimeBlock(
1210112104 break :blk field_ty.zigTypeTag(mod) != .NoReturn;
1210212105 } else true;
1210312106
12104 if (err_set and try sema.maybeErrorUnwrap(&case_block, body, operand, operand_src)) {
12107 if (err_set and try sema.maybeErrorUnwrap(&case_block, body, operand, operand_src, allow_err_code_unwrap)) {
1210512108 // nothing to do here
1210612109 } else if (analyze_body) {
1210712110 try spa.analyzeProngRuntime(
......@@ -12285,7 +12288,7 @@ fn analyzeSwitchRuntimeBlock(
1228512288
1228612289 const body = sema.code.bodySlice(extra_index, info.body_len);
1228712290 extra_index += info.body_len;
12288 if (err_set and try sema.maybeErrorUnwrap(&case_block, body, operand, operand_src)) {
12291 if (err_set and try sema.maybeErrorUnwrap(&case_block, body, operand, operand_src, allow_err_code_unwrap)) {
1228912292 // nothing to do here
1229012293 } else if (analyze_body) {
1229112294 try spa.analyzeProngRuntime(
......@@ -12369,7 +12372,7 @@ fn analyzeSwitchRuntimeBlock(
1236912372
1237012373 const body = sema.code.bodySlice(extra_index, info.body_len);
1237112374 extra_index += info.body_len;
12372 if (err_set and try sema.maybeErrorUnwrap(&case_block, body, operand, operand_src)) {
12375 if (err_set and try sema.maybeErrorUnwrap(&case_block, body, operand, operand_src, allow_err_code_unwrap)) {
1237312376 // nothing to do here
1237412377 } else {
1237512378 try spa.analyzeProngRuntime(
......@@ -12612,7 +12615,7 @@ fn analyzeSwitchRuntimeBlock(
1261212615 else
1261312616 true;
1261412617 if (special.body.len != 0 and err_set and
12615 try sema.maybeErrorUnwrap(&case_block, special.body, operand, operand_src))
12618 try sema.maybeErrorUnwrap(&case_block, special.body, operand, operand_src, allow_err_code_unwrap))
1261612619 {
1261712620 // nothing to do here
1261812621 } else if (special.body.len != 0 and analyze_body and !special.is_inline) {
......@@ -13266,7 +13269,14 @@ fn validateSwitchNoRange(
1326613269 return sema.failWithOwnedErrorMsg(block, msg);
1326713270}
1326813271
13269fn maybeErrorUnwrap(sema: *Sema, block: *Block, body: []const Zir.Inst.Index, operand: Air.Inst.Ref, operand_src: LazySrcLoc) !bool {
13272fn maybeErrorUnwrap(
13273 sema: *Sema,
13274 block: *Block,
13275 body: []const Zir.Inst.Index,
13276 operand: Air.Inst.Ref,
13277 operand_src: LazySrcLoc,
13278 allow_err_code_inst: bool,
13279) !bool {
1327013280 const mod = sema.mod;
1327113281 if (!mod.backendSupportsFeature(.panic_unwrap_error)) return false;
1327213282
......@@ -13274,6 +13284,7 @@ fn maybeErrorUnwrap(sema: *Sema, block: *Block, body: []const Zir.Inst.Index, op
1327413284 for (body) |inst| {
1327513285 switch (tags[@intFromEnum(inst)]) {
1327613286 .@"unreachable" => if (!block.wantSafety()) return false,
13287 .err_union_code => if (!allow_err_code_inst) return false,
1327713288 .save_err_ret_index,
1327813289 .dbg_block_begin,
1327913290 .dbg_block_end,
......@@ -13291,6 +13302,7 @@ fn maybeErrorUnwrap(sema: *Sema, block: *Block, body: []const Zir.Inst.Index, op
1329113302 const air_inst = switch (tags[@intFromEnum(inst)]) {
1329213303 .dbg_block_begin,
1329313304 .dbg_block_end,
13305 .err_union_code,
1329413306 => continue,
1329513307 .dbg_stmt => {
1329613308 try sema.zirDbgStmt(block, inst);
......@@ -18754,7 +18766,7 @@ fn zirCondbr(
1875418766 break :blk try sub_block.addTyOp(.unwrap_errunion_err, result_ty, err_operand);
1875518767 };
1875618768
18757 if (err_cond != null and try sema.maybeErrorUnwrap(&sub_block, else_body, err_cond.?, cond_src)) {
18769 if (err_cond != null and try sema.maybeErrorUnwrap(&sub_block, else_body, err_cond.?, cond_src, false)) {
1875818770 // nothing to do
1875918771 } else {
1876018772 try sema.analyzeBodyRuntimeBreak(&sub_block, else_body);