authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-12-15 13:35:49+11:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2024-01-09 14:42:12+11:00
log6a18cee3af8021bcebbca40413056b18f33af8c7
treee7675a562337ed5f1fed4d86c62872c7b5893413
parentb7eb59fc140f3263b608a80fbe4e1ab56e04b318

astgen/sema: use switch_block_err_union for if-else-switch


3 files changed, 186 insertions(+), 30 deletions(-)

src/AstGen.zig+145-20
......@@ -839,7 +839,18 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
839839
840840 .if_simple,
841841 .@"if",
842 => return ifExpr(gz, scope, ri.br(), node, tree.fullIf(node).?),
842 => {
843 const if_full = tree.fullIf(node).?;
844 if (if_full.error_token) |error_token| {
845 const tag = node_tags[if_full.ast.else_expr];
846 if ((tag == .@"switch" or tag == .switch_comma) and
847 std.mem.eql(u8, tree.tokenSlice(error_token), tree.tokenSlice(error_token + 4)))
848 {
849 return switchExprErrUnion(gz, scope, ri.br(), node, .@"if");
850 }
851 }
852 return ifExpr(gz, scope, ri.br(), node, if_full);
853 },
843854
844855 .while_simple,
845856 .while_cont,
......@@ -1020,7 +1031,7 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
10201031 token_tags[catch_token + 4] == .keyword_switch)
10211032 {
10221033 if (std.mem.eql(u8, tree.tokenSlice(catch_token + 2), tree.tokenSlice(catch_token + 6))) {
1023 return switchExprErrUnion(gz, scope, ri.br(), node);
1034 return switchExprErrUnion(gz, scope, ri.br(), node, .@"catch");
10241035 }
10251036 }
10261037 break :blk catch_token + 2;
......@@ -6869,7 +6880,8 @@ fn switchExprErrUnion(
68696880 parent_gz: *GenZir,
68706881 scope: *Scope,
68716882 ri: ResultInfo,
6872 catch_node: Ast.Node.Index,
6883 catch_or_if_node: Ast.Node.Index,
6884 node_ty: enum { @"catch", @"if" },
68736885) InnerError!Zir.Inst.Ref {
68746886 const astgen = parent_gz.astgen;
68756887 const gpa = astgen.gpa;
......@@ -6878,21 +6890,42 @@ fn switchExprErrUnion(
68786890 const node_tags = tree.nodes.items(.tag);
68796891 const main_tokens = tree.nodes.items(.main_token);
68806892 const token_tags = tree.tokens.items(.tag);
6881 const operand_node = node_datas[catch_node].lhs;
6882 const switch_node = node_datas[catch_node].rhs;
6893
6894 const if_full = switch (node_ty) {
6895 .@"catch" => undefined,
6896 .@"if" => tree.fullIf(catch_or_if_node).?,
6897 };
6898
6899 const switch_node, const operand_node, const error_payload = switch (node_ty) {
6900 .@"catch" => .{
6901 node_datas[catch_or_if_node].rhs,
6902 node_datas[catch_or_if_node].lhs,
6903 main_tokens[catch_or_if_node] + 2,
6904 },
6905 .@"if" => .{
6906 if_full.ast.else_expr,
6907 if_full.ast.cond_expr,
6908 if_full.error_token.?,
6909 },
6910 };
6911 assert(node_tags[switch_node] == .@"switch" or node_tags[switch_node] == .switch_comma);
6912
68836913 const extra = tree.extraData(node_datas[switch_node].rhs, Ast.Node.SubRange);
68846914 const case_nodes = tree.extra_data[extra.start..extra.end];
68856915
6886 const need_rl = astgen.nodes_need_rl.contains(catch_node);
6916 const need_rl = astgen.nodes_need_rl.contains(catch_or_if_node);
68876917 const block_ri: ResultInfo = if (need_rl) ri else .{
68886918 .rl = switch (ri.rl) {
6889 .ptr => .{ .ty = (try ri.rl.resultType(parent_gz, catch_node)).? },
6919 .ptr => .{ .ty = (try ri.rl.resultType(parent_gz, catch_or_if_node)).? },
68906920 .inferred_ptr => .none,
68916921 else => ri.rl,
68926922 },
68936923 .ctx = ri.ctx,
68946924 };
68956925
6926 const payload_is_ref = node_ty == .@"if" and
6927 if_full.payload_token != null and token_tags[if_full.payload_token.?] == .asterisk;
6928
68966929 // We need to call `rvalue` to write through to the pointer only if we had a
68976930 // result pointer and aren't forwarding it.
68986931 const LocTag = @typeInfo(ResultInfo.Loc).Union.tag_type.?;
......@@ -6960,12 +6993,15 @@ fn switchExprErrUnion(
69606993 }
69616994 }
69626995
6963 const operand_ri: ResultInfo = .{ .rl = .none, .ctx = .error_handling_expr };
6996 const operand_ri: ResultInfo = .{
6997 .rl = if (payload_is_ref) .ref else .none,
6998 .ctx = .error_handling_expr,
6999 };
69647000
69657001 astgen.advanceSourceCursorToNode(operand_node);
69667002 const operand_lc = LineColumn{ astgen.source_line - parent_gz.decl_line, astgen.source_column };
69677003
6968 const raw_operand = try reachableExpr(parent_gz, scope, operand_ri, operand_node, node_datas[catch_node].rhs);
7004 const raw_operand = try reachableExpr(parent_gz, scope, operand_ri, operand_node, switch_node);
69697005 const item_ri: ResultInfo = .{ .rl = .none };
69707006
69717007 // This contains the data that goes into the `extra` array for the SwitchBlockErrUnion, except
......@@ -7006,13 +7042,93 @@ fn switchExprErrUnion(
70067042
70077043 try case_scope.addDbgBlockBegin();
70087044
7009 const unwrapped_payload = try case_scope.addUnNode(.err_union_payload_unsafe, raw_operand, catch_node);
7010 const case_result = switch (ri.rl) {
7011 .ref, .ref_coerced_ty => unwrapped_payload,
7012 else => try rvalue(&case_scope, block_scope.break_result_info, unwrapped_payload, catch_node),
7013 };
7014 try case_scope.addDbgBlockEnd();
7015 _ = try case_scope.addBreakWithSrcNode(.@"break", switch_block, case_result, catch_node);
7045 const unwrap_payload_tag: Zir.Inst.Tag = if (payload_is_ref)
7046 .err_union_payload_unsafe_ptr
7047 else
7048 .err_union_payload_unsafe;
7049
7050 const unwrapped_payload = try case_scope.addUnNode(
7051 unwrap_payload_tag,
7052 raw_operand,
7053 catch_or_if_node,
7054 );
7055
7056 switch (node_ty) {
7057 .@"catch" => {
7058 const case_result = switch (ri.rl) {
7059 .ref, .ref_coerced_ty => unwrapped_payload,
7060 else => try rvalue(
7061 &case_scope,
7062 block_scope.break_result_info,
7063 unwrapped_payload,
7064 catch_or_if_node,
7065 ),
7066 };
7067 try case_scope.addDbgBlockEnd();
7068 _ = try case_scope.addBreakWithSrcNode(
7069 .@"break",
7070 switch_block,
7071 case_result,
7072 catch_or_if_node,
7073 );
7074 },
7075 .@"if" => {
7076 var payload_val_scope: Scope.LocalVal = undefined;
7077
7078 try case_scope.addDbgBlockBegin();
7079 const then_node = if_full.ast.then_expr;
7080 const then_sub_scope = s: {
7081 assert(if_full.error_token != null);
7082 if (if_full.payload_token) |payload_token| {
7083 const token_name_index = payload_token + @intFromBool(payload_is_ref);
7084 const ident_name = try astgen.identAsString(token_name_index);
7085 const token_name_str = tree.tokenSlice(token_name_index);
7086 if (mem.eql(u8, "_", token_name_str))
7087 break :s &case_scope.base;
7088 try astgen.detectLocalShadowing(
7089 &case_scope.base,
7090 ident_name,
7091 token_name_index,
7092 token_name_str,
7093 .capture,
7094 );
7095 payload_val_scope = .{
7096 .parent = &case_scope.base,
7097 .gen_zir = &case_scope,
7098 .name = ident_name,
7099 .inst = unwrapped_payload,
7100 .token_src = payload_token,
7101 .id_cat = .capture,
7102 };
7103 try case_scope.addDbgVar(.dbg_var_val, ident_name, unwrapped_payload);
7104 break :s &payload_val_scope.base;
7105 } else {
7106 _ = try case_scope.addUnNode(
7107 .ensure_err_union_payload_void,
7108 raw_operand,
7109 catch_or_if_node,
7110 );
7111 break :s &case_scope.base;
7112 }
7113 };
7114 const then_result = try expr(
7115 &case_scope,
7116 then_sub_scope,
7117 block_scope.break_result_info,
7118 then_node,
7119 );
7120 try checkUsed(parent_gz, &case_scope.base, then_sub_scope);
7121 if (!case_scope.endsWithNoReturn()) {
7122 try case_scope.addDbgBlockEnd();
7123 _ = try case_scope.addBreakWithSrcNode(
7124 .@"break",
7125 switch_block,
7126 then_result,
7127 then_node,
7128 );
7129 }
7130 },
7131 }
70167132
70177133 const case_slice = case_scope.instructionsSlice();
70187134 // Since we use the switch_block_err_union instruction itself to refer
......@@ -7029,9 +7145,18 @@ fn switchExprErrUnion(
70297145 };
70307146 const body_len = refs_len + astgen.countBodyLenAfterFixups(case_slice);
70317147 try payloads.ensureUnusedCapacity(gpa, body_len);
7148 const capture: Zir.Inst.SwitchBlock.ProngInfo.Capture = switch (node_ty) {
7149 .@"catch" => .none,
7150 .@"if" => if (if_full.payload_token == null)
7151 .none
7152 else if (payload_is_ref)
7153 .by_ref
7154 else
7155 .by_val,
7156 };
70327157 payloads.items[body_len_index] = @bitCast(Zir.Inst.SwitchBlock.ProngInfo{
70337158 .body_len = @intCast(body_len),
7034 .capture = .none,
7159 .capture = capture,
70357160 .is_inline = false,
70367161 .has_tag_capture = false,
70377162 });
......@@ -7041,8 +7166,7 @@ fn switchExprErrUnion(
70417166 appendBodyWithFixupsArrayList(astgen, payloads, case_slice);
70427167 }
70437168
7044 const err_name, const error_payload = blk: {
7045 const error_payload = main_tokens[catch_node] + 2;
7169 const err_name = blk: {
70467170 const err_str = tree.tokenSlice(error_payload);
70477171 if (mem.eql(u8, err_str, "_")) {
70487172 return astgen.failTok(error_payload, "discard of error capture; omit it instead", .{});
......@@ -7050,7 +7174,7 @@ fn switchExprErrUnion(
70507174 const err_name = try astgen.identAsString(error_payload);
70517175 try astgen.detectLocalShadowing(scope, err_name, error_payload, err_str, .capture);
70527176
7053 break :blk .{ err_name, error_payload };
7177 break :blk err_name;
70547178 };
70557179
70567180 // allocate a shared dummy instruction for the error capture
......@@ -7241,6 +7365,7 @@ fn switchExprErrUnion(
72417365 .has_else = has_else,
72427366 .scalar_cases_len = @intCast(scalar_cases_len),
72437367 .any_uses_err_capture = any_uses_err_capture,
7368 .payload_is_ref = payload_is_ref,
72447369 },
72457370 });
72467371
src/Sema.zig+39-9
......@@ -8939,10 +8939,14 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
89398939 const tracy = trace(@src());
89408940 defer tracy.end();
89418941
8942 const mod = sema.mod;
89438942 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
89448943 const src = inst_data.src();
89458944 const operand = try sema.resolveInst(inst_data.operand);
8945 return sema.analyzeErrUnionCodePtr(block, src, operand);
8946}
8947
8948fn analyzeErrUnionCodePtr(sema: *Sema, block: *Block, src: LazySrcLoc, operand: Air.Inst.Ref) CompileError!Air.Inst.Ref {
8949 const mod = sema.mod;
89468950 const operand_ty = sema.typeOf(operand);
89478951 assert(operand_ty.zigTypeTag(mod) == .Pointer);
89488952
......@@ -8957,7 +8961,10 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
89578961 if (try sema.resolveDefinedValue(block, src, operand)) |pointer_val| {
89588962 if (try sema.pointerDeref(block, src, pointer_val, operand_ty)) |val| {
89598963 assert(val.getErrorName(mod) != .none);
8960 return Air.internedToRef(val.toIntern());
8964 return Air.internedToRef((try mod.intern(.{ .err = .{
8965 .ty = result_ty.toIntern(),
8966 .name = mod.intern_pool.indexToKey(val.toIntern()).error_union.val.err_name,
8967 } })));
89618968 }
89628969 }
89638970
......@@ -11174,7 +11181,6 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1117411181 const extra = sema.code.extraData(Zir.Inst.SwitchBlockErrUnion, inst_data.payload_index);
1117511182
1117611183 const raw_operand_val = try sema.resolveInst(extra.data.operand);
11177 assert(sema.typeOf(raw_operand_val).zigTypeTag(mod) == .ErrorUnion);
1117811184
1117911185 // AstGen guarantees that the instruction immediately preceding
1118011186 // switch_block_err_union is a dbg_stmt
......@@ -11205,6 +11211,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1120511211 const NonError = struct {
1120611212 body: []const Zir.Inst.Index,
1120711213 end: usize,
11214 capture: Zir.Inst.SwitchBlock.ProngInfo.Capture,
1120811215 };
1120911216
1121011217 const non_error_case: NonError = non_error: {
......@@ -11213,6 +11220,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1121311220 break :non_error .{
1121411221 .body = sema.code.bodySlice(extra_body_start, info.body_len),
1121511222 .end = extra_body_start + info.body_len,
11223 .capture = info.capture,
1121611224 };
1121711225 };
1121811226
......@@ -11237,7 +11245,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1123711245 .body = sema.code.bodySlice(extra_body_start, info.body_len),
1123811246 .end = extra_body_start + info.body_len,
1123911247 .is_inline = info.is_inline,
11240 .has_capture = info.capture == .by_val,
11248 .has_capture = info.capture != .none,
1124111249 };
1124211250 };
1124311251
......@@ -11245,7 +11253,10 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1124511253 defer seen_errors.deinit();
1124611254
1124711255 const operand_ty = sema.typeOf(raw_operand_val);
11248 const operand_err_set_ty = operand_ty.errorUnionSet(mod);
11256 const operand_err_set_ty = if (extra.data.bits.payload_is_ref)
11257 operand_ty.childType(mod).errorUnionSet(mod)
11258 else
11259 operand_ty.errorUnionSet(mod);
1124911260
1125011261 const block_inst: Air.Inst.Index = @enumFromInt(sema.air_instructions.len);
1125111262 try sema.air_instructions.append(gpa, .{
......@@ -11313,7 +11324,12 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1131311324 .tag_capture_inst = undefined,
1131411325 };
1131511326
11316 if (try sema.resolveDefinedValue(&child_block, src, raw_operand_val)) |operand_val| {
11327 if (try sema.resolveDefinedValue(&child_block, src, raw_operand_val)) |ov| {
11328 const operand_val = if (extra.data.bits.payload_is_ref)
11329 (try sema.pointerDeref(&child_block, src, ov, operand_ty)).?
11330 else
11331 ov;
11332
1131711333 if (operand_val.errorUnionIsPayload(mod)) {
1131811334 return sema.resolveBlockBody(block, operand_src, &child_block, non_error_case.body, inst, merges);
1131911335 } else {
......@@ -11323,7 +11339,10 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1132311339 .name = operand_val.getErrorName(mod).unwrap().?,
1132411340 },
1132511341 }));
11326 spa.operand = try sema.analyzeErrUnionCode(block, operand_src, raw_operand_val);
11342 spa.operand = if (extra.data.bits.payload_is_ref)
11343 try sema.analyzeErrUnionCodePtr(block, operand_src, raw_operand_val)
11344 else
11345 try sema.analyzeErrUnionCode(block, operand_src, raw_operand_val);
1132711346
1132811347 if (extra.data.bits.any_uses_err_capture) {
1132911348 sema.inst_map.putAssumeCapacity(err_capture_inst, spa.operand);
......@@ -11367,7 +11386,14 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1136711386 unreachable;
1136811387 }
1136911388
11370 const cond = try sema.analyzeIsNonErr(block, src, raw_operand_val);
11389 const cond = if (extra.data.bits.payload_is_ref) blk: {
11390 try sema.checkErrorType(block, src, sema.typeOf(raw_operand_val).elemType2(mod));
11391 const loaded = try sema.analyzeLoad(block, src, raw_operand_val, src);
11392 break :blk try sema.analyzeIsNonErr(block, src, loaded);
11393 } else blk: {
11394 try sema.checkErrorType(block, src, sema.typeOf(raw_operand_val));
11395 break :blk try sema.analyzeIsNonErr(block, src, raw_operand_val);
11396 };
1137111397
1137211398 var sub_block = child_block.makeSubBlock();
1137311399 sub_block.runtime_loop = null;
......@@ -11379,7 +11405,11 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1137911405 const true_instructions = try sub_block.instructions.toOwnedSlice(gpa);
1138011406 defer gpa.free(true_instructions);
1138111407
11382 spa.operand = try sema.analyzeErrUnionCode(&sub_block, operand_src, raw_operand_val);
11408 spa.operand = if (extra.data.bits.payload_is_ref)
11409 try sema.analyzeErrUnionCodePtr(&sub_block, operand_src, raw_operand_val)
11410 else
11411 try sema.analyzeErrUnionCode(&sub_block, operand_src, raw_operand_val);
11412
1138311413 if (extra.data.bits.any_uses_err_capture) {
1138411414 sema.inst_map.putAssumeCapacity(err_capture_inst, spa.operand);
1138511415 }
src/Zir.zig+2-1
......@@ -2793,9 +2793,10 @@ pub const Inst = struct {
27932793 /// If true, there is an else prong. This is mutually exclusive with `has_under`.
27942794 has_else: bool,
27952795 any_uses_err_capture: bool,
2796 payload_is_ref: bool,
27962797 scalar_cases_len: ScalarCasesLen,
27972798
2798 pub const ScalarCasesLen = u29;
2799 pub const ScalarCasesLen = u28;
27992800 };
28002801
28012802 pub const MultiProng = struct {