authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-12-14 14:36:16+11:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2024-01-09 14:42:12+11:00
logfc6dc797ceff35c5bbaa51d914a62256bab3c409
treec8358cace14e9ed28054a5899b42c781ee529cd7
parent8695bc7ed3583bb6b896079dd0b7bfd678e82722

astgen/sema: fix source locations for switch_block_err_union


3 files changed, 31 insertions(+), 27 deletions(-)

src/AstGen.zig+1
......@@ -7367,6 +7367,7 @@ fn switchExprErrUnion(
73677367 .any_uses_err_capture = any_uses_err_capture,
73687368 .payload_is_ref = payload_is_ref,
73697369 },
7370 .main_src_node_offset = parent_gz.nodeIndexToRelative(catch_or_if_node),
73707371 });
73717372
73727373 if (multi_cases_len != 0) {
src/Sema.zig+29-27
......@@ -11174,11 +11174,13 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1117411174 const mod = sema.mod;
1117511175 const gpa = sema.gpa;
1117611176 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
11177 const src = inst_data.src();
11178 const src_node_offset = inst_data.src_node;
11179 const operand_src: LazySrcLoc = .{ .node_offset_switch_operand = src_node_offset };
11180 const else_prong_src: LazySrcLoc = .{ .node_offset_switch_special_prong = src_node_offset };
11177 const switch_src = inst_data.src();
11178 const switch_src_node_offset = inst_data.src_node;
11179 const switch_operand_src: LazySrcLoc = .{ .node_offset_switch_operand = switch_src_node_offset };
11180 const else_prong_src: LazySrcLoc = .{ .node_offset_switch_special_prong = switch_src_node_offset };
1118111181 const extra = sema.code.extraData(Zir.Inst.SwitchBlockErrUnion, inst_data.payload_index);
11182 const main_operand_src: LazySrcLoc = .{ .node_offset_if_cond = extra.data.main_src_node_offset };
11183 const main_src: LazySrcLoc = .{ .node_offset_main_token = extra.data.main_src_node_offset };
1118211184
1118311185 const raw_operand_val = try sema.resolveInst(extra.data.operand);
1118411186
......@@ -11295,9 +11297,9 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1129511297 defer child_block.instructions.deinit(gpa);
1129611298 defer merges.deinit(gpa);
1129711299
11298 const resolved_err_set = try sema.resolveInferredErrorSetTy(block, src, operand_err_set_ty.toIntern());
11300 const resolved_err_set = try sema.resolveInferredErrorSetTy(block, main_src, operand_err_set_ty.toIntern());
1129911301 if (Type.fromInterned(resolved_err_set).errorSetIsEmpty(mod)) {
11300 return sema.resolveBlockBody(block, operand_src, &child_block, non_error_case.body, inst, merges);
11302 return sema.resolveBlockBody(block, main_operand_src, &child_block, non_error_case.body, inst, merges);
1130111303 }
1130211304
1130311305 const else_error_ty: ?Type = try validateErrSetSwitch(
......@@ -11324,14 +11326,14 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1132411326 .tag_capture_inst = undefined,
1132511327 };
1132611328
11327 if (try sema.resolveDefinedValue(&child_block, src, raw_operand_val)) |ov| {
11329 if (try sema.resolveDefinedValue(&child_block, main_src, raw_operand_val)) |ov| {
1132811330 const operand_val = if (extra.data.bits.payload_is_ref)
11329 (try sema.pointerDeref(&child_block, src, ov, operand_ty)).?
11331 (try sema.pointerDeref(&child_block, main_src, ov, operand_ty)).?
1133011332 else
1133111333 ov;
1133211334
1133311335 if (operand_val.errorUnionIsPayload(mod)) {
11334 return sema.resolveBlockBody(block, operand_src, &child_block, non_error_case.body, inst, merges);
11336 return sema.resolveBlockBody(block, main_operand_src, &child_block, non_error_case.body, inst, merges);
1133511337 } else {
1133611338 const err_val = Value.fromInterned(try mod.intern(.{
1133711339 .err = .{
......@@ -11340,9 +11342,9 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1134011342 },
1134111343 }));
1134211344 spa.operand = if (extra.data.bits.payload_is_ref)
11343 try sema.analyzeErrUnionCodePtr(block, operand_src, raw_operand_val)
11345 try sema.analyzeErrUnionCodePtr(block, switch_operand_src, raw_operand_val)
1134411346 else
11345 try sema.analyzeErrUnionCode(block, operand_src, raw_operand_val);
11347 try sema.analyzeErrUnionCode(block, switch_operand_src, raw_operand_val);
1134611348
1134711349 if (extra.data.bits.any_uses_err_capture) {
1134811350 sema.inst_map.putAssumeCapacity(err_capture_inst, spa.operand);
......@@ -11353,7 +11355,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1135311355 sema,
1135411356 spa,
1135511357 &child_block,
11356 try sema.switchCond(block, operand_src, spa.operand),
11358 try sema.switchCond(block, switch_operand_src, spa.operand),
1135711359 err_val,
1135811360 operand_err_set_ty,
1135911361 .{
......@@ -11374,12 +11376,12 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1137411376
1137511377 if (scalar_cases_len + multi_cases_len == 0) {
1137611378 if (else_error_ty) |ty| if (ty.errorSetIsEmpty(mod)) {
11377 return sema.resolveBlockBody(block, operand_src, &child_block, non_error_case.body, inst, merges);
11379 return sema.resolveBlockBody(block, main_operand_src, &child_block, non_error_case.body, inst, merges);
1137811380 };
1137911381 }
1138011382
1138111383 if (child_block.is_comptime) {
11382 _ = try sema.resolveConstDefinedValue(&child_block, operand_src, raw_operand_val, .{
11384 _ = try sema.resolveConstDefinedValue(&child_block, main_operand_src, raw_operand_val, .{
1138311385 .needed_comptime_reason = "condition in comptime switch must be comptime-known",
1138411386 .block_comptime_reason = child_block.comptime_reason,
1138511387 });
......@@ -11387,17 +11389,17 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1138711389 }
1138811390
1138911391 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);
11392 try sema.checkErrorType(block, main_src, sema.typeOf(raw_operand_val).elemType2(mod));
11393 const loaded = try sema.analyzeLoad(block, main_src, raw_operand_val, main_src);
11394 break :blk try sema.analyzeIsNonErr(block, main_src, loaded);
1139311395 } 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 try sema.checkErrorType(block, main_src, sema.typeOf(raw_operand_val));
11397 break :blk try sema.analyzeIsNonErr(block, main_src, raw_operand_val);
1139611398 };
1139711399
1139811400 var sub_block = child_block.makeSubBlock();
1139911401 sub_block.runtime_loop = null;
11400 sub_block.runtime_cond = operand_src;
11402 sub_block.runtime_cond = main_operand_src;
1140111403 sub_block.runtime_index.increment();
1140211404 defer sub_block.instructions.deinit(gpa);
1140311405
......@@ -11406,9 +11408,9 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1140611408 defer gpa.free(true_instructions);
1140711409
1140811410 spa.operand = if (extra.data.bits.payload_is_ref)
11409 try sema.analyzeErrUnionCodePtr(&sub_block, operand_src, raw_operand_val)
11411 try sema.analyzeErrUnionCodePtr(&sub_block, switch_operand_src, raw_operand_val)
1141011412 else
11411 try sema.analyzeErrUnionCode(&sub_block, operand_src, raw_operand_val);
11413 try sema.analyzeErrUnionCode(&sub_block, switch_operand_src, raw_operand_val);
1141211414
1141311415 if (extra.data.bits.any_uses_err_capture) {
1141411416 sema.inst_map.putAssumeCapacity(err_capture_inst, spa.operand);
......@@ -11417,10 +11419,10 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1141711419 _ = try sema.analyzeSwitchRuntimeBlock(
1141811420 spa,
1141911421 &sub_block,
11420 src,
11421 try sema.switchCond(block, operand_src, spa.operand),
11422 switch_src,
11423 try sema.switchCond(block, switch_operand_src, spa.operand),
1142211424 operand_err_set_ty,
11423 operand_src,
11425 switch_operand_src,
1142411426 case_vals,
1142511427 .{
1142611428 .body = else_case.body,
......@@ -11434,7 +11436,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1143411436 false,
1143511437 undefined,
1143611438 true,
11437 src_node_offset,
11439 switch_src_node_offset,
1143811440 else_prong_src,
1143911441 undefined,
1144011442 seen_errors,
......@@ -11461,7 +11463,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1146111463 sema.air_extra.appendSliceAssumeCapacity(@ptrCast(true_instructions));
1146211464 sema.air_extra.appendSliceAssumeCapacity(@ptrCast(sub_block.instructions.items));
1146311465
11464 return sema.analyzeBlockBody(block, src, &child_block, merges);
11466 return sema.analyzeBlockBody(block, main_src, &child_block, merges);
1146511467}
1146611468
1146711469fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_ref: bool) CompileError!Air.Inst.Ref {
src/Zir.zig+1
......@@ -2786,6 +2786,7 @@ pub const Inst = struct {
27862786 pub const SwitchBlockErrUnion = struct {
27872787 operand: Ref,
27882788 bits: Bits,
2789 main_src_node_offset: i32,
27892790
27902791 pub const Bits = packed struct(u32) {
27912792 /// If true, one or more prongs have multiple items.