authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-12-03 22:07:45+11:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2024-01-09 14:42:12+11:00
logb7eb59fc140f3263b608a80fbe4e1ab56e04b318
treebb68023b3273d2c1343fe79fedf1d9b87a8ed943
parentadcaad6d91c309fab20ea208231c3e7d33fcc21f

fix x86_64 crashes for switch_block_err_union

This change only emits the unwrap_errunion_err instruction if the error capture is actually used in a branch.

4 files changed, 99 insertions(+), 31 deletions(-)

src/AstGen.zig+66-30
......@@ -7017,8 +7017,7 @@ fn switchExprErrUnion(
70177017 const case_slice = case_scope.instructionsSlice();
70187018 // Since we use the switch_block_err_union instruction itself to refer
70197019 // to the capture, which will not be added to the child block, we need
7020 // to handle ref_table manually, and the same for the inline tag
7021 // capture instruction.
7020 // to handle ref_table manually.
70227021 const refs_len = refs: {
70237022 var n: usize = 0;
70247023 var check_inst = switch_block;
......@@ -7054,9 +7053,24 @@ fn switchExprErrUnion(
70547053 break :blk .{ err_name, error_payload };
70557054 };
70567055
7056 // allocate a shared dummy instruction for the error capture
7057 const err_inst = err_inst: {
7058 const inst: Zir.Inst.Index = @enumFromInt(astgen.instructions.len);
7059 try astgen.instructions.append(astgen.gpa, .{
7060 .tag = .extended,
7061 .data = .{ .extended = .{
7062 .opcode = .value_placeholder,
7063 .small = undefined,
7064 .operand = undefined,
7065 } },
7066 });
7067 break :err_inst inst;
7068 };
7069
70577070 // In this pass we generate all the item and prong expressions for error cases.
70587071 var multi_case_index: u32 = 0;
70597072 var scalar_case_index: u32 = 0;
7073 var any_uses_err_capture = false;
70607074 for (case_nodes) |case_node| {
70617075 const case = tree.fullSwitchCase(case_node).?;
70627076
......@@ -7066,31 +7080,42 @@ fn switchExprErrUnion(
70667080 var dbg_var_name: ?u32 = null;
70677081 var dbg_var_inst: Zir.Inst.Ref = undefined;
70687082 var err_scope: Scope.LocalVal = undefined;
7069 var tag_scope: Scope.LocalVal = undefined;
7083 var capture_scope: Scope.LocalVal = undefined;
70707084
70717085 const sub_scope = blk: {
7072 const tag_token = case.payload_token orelse break :blk &case_scope.base;
7073 assert(token_tags[tag_token] == .identifier);
7086 err_scope = .{
7087 .parent = &case_scope.base,
7088 .gen_zir = &case_scope,
7089 .name = err_name,
7090 .inst = err_inst.toRef(),
7091 .token_src = error_payload,
7092 .id_cat = .capture,
7093 };
70747094
7075 const tag_slice = tree.tokenSlice(tag_token);
7076 if (mem.eql(u8, tag_slice, "_")) {
7077 return astgen.failTok(tag_token, "discard of error capture; omit it instead", .{});
7095 const capture_token = case.payload_token orelse break :blk &err_scope.base;
7096 assert(token_tags[capture_token] == .identifier);
7097
7098 const capture_slice = tree.tokenSlice(capture_token);
7099 if (mem.eql(u8, capture_slice, "_")) {
7100 return astgen.failTok(capture_token, "discard of error capture; omit it instead", .{});
70787101 }
7079 const tag_name = try astgen.identAsString(tag_token);
7080 try astgen.detectLocalShadowing(&case_scope.base, tag_name, tag_token, tag_slice, .capture);
7102 const tag_name = try astgen.identAsString(capture_token);
7103 try astgen.detectLocalShadowing(&case_scope.base, tag_name, capture_token, capture_slice, .capture);
70817104
7082 tag_scope = .{
7105 capture_scope = .{
70837106 .parent = &case_scope.base,
70847107 .gen_zir = &case_scope,
70857108 .name = tag_name,
70867109 .inst = switch_block.toRef(),
7087 .token_src = tag_token,
7110 .token_src = capture_token,
70887111 .id_cat = .capture,
70897112 };
70907113 dbg_var_name = tag_name;
70917114 dbg_var_inst = switch_block.toRef();
70927115
7093 break :blk &tag_scope.base;
7116 err_scope.parent = &capture_scope.base;
7117
7118 break :blk &err_scope.base;
70947119 };
70957120
70967121 const header_index: u32 = @intCast(payloads.items.len);
......@@ -7144,34 +7169,28 @@ fn switchExprErrUnion(
71447169 case_scope.instructions_top = parent_gz.instructions.items.len;
71457170 defer case_scope.unstack();
71467171
7147 const err_code_inst = try case_scope.addUnNode(.err_union_code, raw_operand, operand_node);
7148 err_scope = .{
7149 .parent = sub_scope,
7150 .gen_zir = &case_scope,
7151 .name = err_name,
7152 .inst = err_code_inst,
7153 .token_src = error_payload,
7154 .id_cat = .capture,
7155 };
7156
71577172 try case_scope.addDbgBlockBegin();
71587173 if (dbg_var_name) |some| {
71597174 try case_scope.addDbgVar(.dbg_var_val, some, dbg_var_inst);
71607175 }
71617176 const target_expr_node = case.ast.target_expr;
7162 const case_result = try expr(&case_scope, &err_scope.base, block_scope.break_result_info, target_expr_node);
7163 // check sub_scope, not err_scope to avoid false positive unused error capture
7164 try checkUsed(parent_gz, &case_scope.base, sub_scope);
7177 const case_result = try expr(&case_scope, sub_scope, block_scope.break_result_info, target_expr_node);
7178 // check capture_scope, not err_scope to avoid false positive unused error capture
7179 try checkUsed(parent_gz, &case_scope.base, err_scope.parent);
7180 const uses_err = err_scope.used != 0 or err_scope.discarded != 0;
7181 if (uses_err) {
7182 try case_scope.addDbgVar(.dbg_var_val, err_name, err_inst.toRef());
7183 any_uses_err_capture = true;
7184 }
71657185 try case_scope.addDbgBlockEnd();
71667186 if (!parent_gz.refIsNoReturn(case_result)) {
71677187 _ = try case_scope.addBreakWithSrcNode(.@"break", switch_block, case_result, target_expr_node);
71687188 }
71697189
71707190 const case_slice = case_scope.instructionsSlice();
7171 // Since we use the switch_block instruction itself to refer to the
7172 // capture, which will not be added to the child block, we need to
7173 // handle ref_table manually, and the same for the inline tag
7174 // capture instruction.
7191 // Since we use the switch_block_err_union instruction itself to refer
7192 // to the capture, which will not be added to the child block, we need
7193 // to handle ref_table manually.
71757194 const refs_len = refs: {
71767195 var n: usize = 0;
71777196 var check_inst = switch_block;
......@@ -7179,6 +7198,13 @@ fn switchExprErrUnion(
71797198 n += 1;
71807199 check_inst = ref_inst;
71817200 }
7201 if (uses_err) {
7202 check_inst = err_inst;
7203 while (astgen.ref_table.get(check_inst)) |ref_inst| {
7204 n += 1;
7205 check_inst = ref_inst;
7206 }
7207 }
71827208 break :refs n;
71837209 };
71847210 const body_len = refs_len + astgen.countBodyLenAfterFixups(case_slice);
......@@ -7192,6 +7218,11 @@ fn switchExprErrUnion(
71927218 if (astgen.ref_table.fetchRemove(switch_block)) |kv| {
71937219 appendPossiblyRefdBodyInst(astgen, payloads, kv.value);
71947220 }
7221 if (uses_err) {
7222 if (astgen.ref_table.fetchRemove(err_inst)) |kv| {
7223 appendPossiblyRefdBodyInst(astgen, payloads, kv.value);
7224 }
7225 }
71957226 appendBodyWithFixupsArrayList(astgen, payloads, case_slice);
71967227 }
71977228 }
......@@ -7209,6 +7240,7 @@ fn switchExprErrUnion(
72097240 .has_multi_cases = multi_cases_len != 0,
72107241 .has_else = has_else,
72117242 .scalar_cases_len = @intCast(scalar_cases_len),
7243 .any_uses_err_capture = any_uses_err_capture,
72127244 },
72137245 });
72147246
......@@ -7216,6 +7248,10 @@ fn switchExprErrUnion(
72167248 astgen.extra.appendAssumeCapacity(multi_cases_len);
72177249 }
72187250
7251 if (any_uses_err_capture) {
7252 astgen.extra.appendAssumeCapacity(@intFromEnum(err_inst));
7253 }
7254
72197255 const zir_datas = astgen.instructions.items(.data);
72207256 zir_datas[@intFromEnum(switch_block)].pl_node.payload_index = payload_index;
72217257
src/Sema.zig+20
......@@ -11189,6 +11189,16 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1118911189 break :blk multi_cases_len;
1119011190 } else 0;
1119111191
11192 const err_capture_inst: Zir.Inst.Index = if (extra.data.bits.any_uses_err_capture) blk: {
11193 const err_capture_inst: Zir.Inst.Index = @enumFromInt(sema.code.extra[header_extra_index]);
11194 header_extra_index += 1;
11195 // SwitchProngAnalysis wants inst_map to have space for the tag capture.
11196 // Note that the normal capture is referred to via the switch block
11197 // index, which there is already necessarily space for.
11198 try sema.inst_map.ensureSpaceForInstructions(gpa, &.{err_capture_inst});
11199 break :blk err_capture_inst;
11200 } else undefined;
11201
1119211202 var case_vals = try std.ArrayListUnmanaged(Air.Inst.Ref).initCapacity(gpa, scalar_cases_len + 2 * multi_cases_len);
1119311203 defer case_vals.deinit(gpa);
1119411204
......@@ -11314,6 +11324,12 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1131411324 },
1131511325 }));
1131611326 spa.operand = try sema.analyzeErrUnionCode(block, operand_src, raw_operand_val);
11327
11328 if (extra.data.bits.any_uses_err_capture) {
11329 sema.inst_map.putAssumeCapacity(err_capture_inst, spa.operand);
11330 }
11331 defer if (extra.data.bits.any_uses_err_capture) assert(sema.inst_map.remove(err_capture_inst));
11332
1131711333 return resolveSwitchComptime(
1131811334 sema,
1131911335 spa,
......@@ -11364,6 +11380,10 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1136411380 defer gpa.free(true_instructions);
1136511381
1136611382 spa.operand = try sema.analyzeErrUnionCode(&sub_block, operand_src, raw_operand_val);
11383 if (extra.data.bits.any_uses_err_capture) {
11384 sema.inst_map.putAssumeCapacity(err_capture_inst, spa.operand);
11385 }
11386 defer if (extra.data.bits.any_uses_err_capture) assert(sema.inst_map.remove(err_capture_inst));
1136711387 _ = try sema.analyzeSwitchRuntimeBlock(
1136811388 spa,
1136911389 &sub_block,
src/Zir.zig+2-1
......@@ -2792,9 +2792,10 @@ pub const Inst = struct {
27922792 has_multi_cases: bool,
27932793 /// If true, there is an else prong. This is mutually exclusive with `has_under`.
27942794 has_else: bool,
2795 any_uses_err_capture: bool,
27952796 scalar_cases_len: ScalarCasesLen,
27962797
2797 pub const ScalarCasesLen = u30;
2798 pub const ScalarCasesLen = u29;
27982799 };
27992800
28002801 pub const MultiProng = struct {
src/print_zir.zig+11
......@@ -2040,8 +2040,19 @@ const Writer = struct {
20402040 break :blk multi_cases_len;
20412041 } else 0;
20422042
2043 const err_capture_inst: Zir.Inst.Index = if (extra.data.bits.any_uses_err_capture) blk: {
2044 const tag_capture_inst = self.code.extra[extra_index];
2045 extra_index += 1;
2046 break :blk @enumFromInt(tag_capture_inst);
2047 } else undefined;
2048
20432049 try self.writeInstRef(stream, extra.data.operand);
20442050
2051 if (extra.data.bits.any_uses_err_capture) {
2052 try stream.writeAll(", err_capture=");
2053 try self.writeInstIndex(stream, err_capture_inst);
2054 }
2055
20452056 self.indent += 2;
20462057
20472058 {