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...@@ -11253,7 +11253,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
11253 var spa: SwitchProngAnalysis = .{11253 var spa: SwitchProngAnalysis = .{
11254 .sema = sema,11254 .sema = sema,
11255 .parent_block = block,11255 .parent_block = block,
11256 .operand = raw_operand_val,11256 .operand = undefined, // must be set to the unwrapped error code before use
11257 .operand_ptr = .none,11257 .operand_ptr = .none,
11258 .cond = raw_operand_val,11258 .cond = raw_operand_val,
11259 .else_error_ty = else_error_ty,11259 .else_error_ty = else_error_ty,
...@@ -11387,6 +11387,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp...@@ -11387,6 +11387,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
11387 undefined,11387 undefined,
11388 undefined,11388 undefined,
11389 cond_dbg_node_index,11389 cond_dbg_node_index,
11390 true,
11390 );11391 );
1139111392
11392 try sema.air_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.CondBr).Struct.fields.len +11393 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...@@ -11970,7 +11971,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
11970 if (special_prong == .none) {11971 if (special_prong == .none) {
11971 return sema.fail(block, src, "switch must handle all possibilities", .{});11972 return sema.fail(block, src, "switch must handle all possibilities", .{});
11972 }11973 }
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)) {
11974 return .unreachable_value;11975 return .unreachable_value;
11975 }11976 }
11976 if (mod.backendSupportsFeature(.is_named_enum_value) and block.wantSafety() and operand_ty.zigTypeTag(mod) == .Enum and11977 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...@@ -12024,6 +12025,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
12024 true_count,12025 true_count,
12025 false_count,12026 false_count,
12026 cond_dbg_node_index,12027 cond_dbg_node_index,
12028 false,
12027 );12029 );
1202812030
12029 return sema.analyzeBlockBody(block, src, &child_block, merges);12031 return sema.analyzeBlockBody(block, src, &child_block, merges);
...@@ -12060,6 +12062,7 @@ fn analyzeSwitchRuntimeBlock(...@@ -12060,6 +12062,7 @@ fn analyzeSwitchRuntimeBlock(
12060 true_count: u8,12062 true_count: u8,
12061 false_count: u8,12063 false_count: u8,
12062 cond_dbg_node_index: Zir.Inst.Index,12064 cond_dbg_node_index: Zir.Inst.Index,
12065 allow_err_code_unwrap: bool,
12063) CompileError!Air.Inst.Ref {12066) CompileError!Air.Inst.Ref {
12064 const mod = sema.mod;12067 const mod = sema.mod;
12065 const gpa = sema.gpa;12068 const gpa = sema.gpa;
...@@ -12101,7 +12104,7 @@ fn analyzeSwitchRuntimeBlock(...@@ -12101,7 +12104,7 @@ fn analyzeSwitchRuntimeBlock(
12101 break :blk field_ty.zigTypeTag(mod) != .NoReturn;12104 break :blk field_ty.zigTypeTag(mod) != .NoReturn;
12102 } else true;12105 } 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)) {
12105 // nothing to do here12108 // nothing to do here
12106 } else if (analyze_body) {12109 } else if (analyze_body) {
12107 try spa.analyzeProngRuntime(12110 try spa.analyzeProngRuntime(
...@@ -12285,7 +12288,7 @@ fn analyzeSwitchRuntimeBlock(...@@ -12285,7 +12288,7 @@ fn analyzeSwitchRuntimeBlock(
1228512288
12286 const body = sema.code.bodySlice(extra_index, info.body_len);12289 const body = sema.code.bodySlice(extra_index, info.body_len);
12287 extra_index += info.body_len;12290 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)) {
12289 // nothing to do here12292 // nothing to do here
12290 } else if (analyze_body) {12293 } else if (analyze_body) {
12291 try spa.analyzeProngRuntime(12294 try spa.analyzeProngRuntime(
...@@ -12369,7 +12372,7 @@ fn analyzeSwitchRuntimeBlock(...@@ -12369,7 +12372,7 @@ fn analyzeSwitchRuntimeBlock(
1236912372
12370 const body = sema.code.bodySlice(extra_index, info.body_len);12373 const body = sema.code.bodySlice(extra_index, info.body_len);
12371 extra_index += info.body_len;12374 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)) {
12373 // nothing to do here12376 // nothing to do here
12374 } else {12377 } else {
12375 try spa.analyzeProngRuntime(12378 try spa.analyzeProngRuntime(
...@@ -12612,7 +12615,7 @@ fn analyzeSwitchRuntimeBlock(...@@ -12612,7 +12615,7 @@ fn analyzeSwitchRuntimeBlock(
12612 else12615 else
12613 true;12616 true;
12614 if (special.body.len != 0 and err_set and12617 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))
12616 {12619 {
12617 // nothing to do here12620 // nothing to do here
12618 } else if (special.body.len != 0 and analyze_body and !special.is_inline) {12621 } else if (special.body.len != 0 and analyze_body and !special.is_inline) {
...@@ -13266,7 +13269,14 @@ fn validateSwitchNoRange(...@@ -13266,7 +13269,14 @@ fn validateSwitchNoRange(
13266 return sema.failWithOwnedErrorMsg(block, msg);13269 return sema.failWithOwnedErrorMsg(block, msg);
13267}13270}
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 {
13270 const mod = sema.mod;13280 const mod = sema.mod;
13271 if (!mod.backendSupportsFeature(.panic_unwrap_error)) return false;13281 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...@@ -13274,6 +13284,7 @@ fn maybeErrorUnwrap(sema: *Sema, block: *Block, body: []const Zir.Inst.Index, op
13274 for (body) |inst| {13284 for (body) |inst| {
13275 switch (tags[@intFromEnum(inst)]) {13285 switch (tags[@intFromEnum(inst)]) {
13276 .@"unreachable" => if (!block.wantSafety()) return false,13286 .@"unreachable" => if (!block.wantSafety()) return false,
13287 .err_union_code => if (!allow_err_code_inst) return false,
13277 .save_err_ret_index,13288 .save_err_ret_index,
13278 .dbg_block_begin,13289 .dbg_block_begin,
13279 .dbg_block_end,13290 .dbg_block_end,
...@@ -13291,6 +13302,7 @@ fn maybeErrorUnwrap(sema: *Sema, block: *Block, body: []const Zir.Inst.Index, op...@@ -13291,6 +13302,7 @@ fn maybeErrorUnwrap(sema: *Sema, block: *Block, body: []const Zir.Inst.Index, op
13291 const air_inst = switch (tags[@intFromEnum(inst)]) {13302 const air_inst = switch (tags[@intFromEnum(inst)]) {
13292 .dbg_block_begin,13303 .dbg_block_begin,
13293 .dbg_block_end,13304 .dbg_block_end,
13305 .err_union_code,
13294 => continue,13306 => continue,
13295 .dbg_stmt => {13307 .dbg_stmt => {
13296 try sema.zirDbgStmt(block, inst);13308 try sema.zirDbgStmt(block, inst);
...@@ -18754,7 +18766,7 @@ fn zirCondbr(...@@ -18754,7 +18766,7 @@ fn zirCondbr(
18754 break :blk try sub_block.addTyOp(.unwrap_errunion_err, result_ty, err_operand);18766 break :blk try sub_block.addTyOp(.unwrap_errunion_err, result_ty, err_operand);
18755 };18767 };
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)) {
18758 // nothing to do18770 // nothing to do
18759 } else {18771 } else {
18760 try sema.analyzeBodyRuntimeBreak(&sub_block, else_body);18772 try sema.analyzeBodyRuntimeBreak(&sub_block, else_body);