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(...@@ -7367,6 +7367,7 @@ fn switchExprErrUnion(
7367 .any_uses_err_capture = any_uses_err_capture,7367 .any_uses_err_capture = any_uses_err_capture,
7368 .payload_is_ref = payload_is_ref,7368 .payload_is_ref = payload_is_ref,
7369 },7369 },
7370 .main_src_node_offset = parent_gz.nodeIndexToRelative(catch_or_if_node),
7370 });7371 });
73717372
7372 if (multi_cases_len != 0) {7373 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...@@ -11174,11 +11174,13 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
11174 const mod = sema.mod;11174 const mod = sema.mod;
11175 const gpa = sema.gpa;11175 const gpa = sema.gpa;
11176 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;11176 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
11177 const src = inst_data.src();11177 const switch_src = inst_data.src();
11178 const src_node_offset = inst_data.src_node;11178 const switch_src_node_offset = inst_data.src_node;
11179 const operand_src: LazySrcLoc = .{ .node_offset_switch_operand = src_node_offset };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 = src_node_offset };11180 const else_prong_src: LazySrcLoc = .{ .node_offset_switch_special_prong = switch_src_node_offset };
11181 const extra = sema.code.extraData(Zir.Inst.SwitchBlockErrUnion, inst_data.payload_index);11181 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
11183 const raw_operand_val = try sema.resolveInst(extra.data.operand);11185 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...@@ -11295,9 +11297,9 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
11295 defer child_block.instructions.deinit(gpa);11297 defer child_block.instructions.deinit(gpa);
11296 defer merges.deinit(gpa);11298 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());
11299 if (Type.fromInterned(resolved_err_set).errorSetIsEmpty(mod)) {11301 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);
11301 }11303 }
1130211304
11303 const else_error_ty: ?Type = try validateErrSetSwitch(11305 const else_error_ty: ?Type = try validateErrSetSwitch(
...@@ -11324,14 +11326,14 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp...@@ -11324,14 +11326,14 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
11324 .tag_capture_inst = undefined,11326 .tag_capture_inst = undefined,
11325 };11327 };
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| {
11328 const operand_val = if (extra.data.bits.payload_is_ref)11330 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)).?
11330 else11332 else
11331 ov;11333 ov;
1133211334
11333 if (operand_val.errorUnionIsPayload(mod)) {11335 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);
11335 } else {11337 } else {
11336 const err_val = Value.fromInterned(try mod.intern(.{11338 const err_val = Value.fromInterned(try mod.intern(.{
11337 .err = .{11339 .err = .{
...@@ -11340,9 +11342,9 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp...@@ -11340,9 +11342,9 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
11340 },11342 },
11341 }));11343 }));
11342 spa.operand = if (extra.data.bits.payload_is_ref)11344 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)
11344 else11346 else
11345 try sema.analyzeErrUnionCode(block, operand_src, raw_operand_val);11347 try sema.analyzeErrUnionCode(block, switch_operand_src, raw_operand_val);
1134611348
11347 if (extra.data.bits.any_uses_err_capture) {11349 if (extra.data.bits.any_uses_err_capture) {
11348 sema.inst_map.putAssumeCapacity(err_capture_inst, spa.operand);11350 sema.inst_map.putAssumeCapacity(err_capture_inst, spa.operand);
...@@ -11353,7 +11355,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp...@@ -11353,7 +11355,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
11353 sema,11355 sema,
11354 spa,11356 spa,
11355 &child_block,11357 &child_block,
11356 try sema.switchCond(block, operand_src, spa.operand),11358 try sema.switchCond(block, switch_operand_src, spa.operand),
11357 err_val,11359 err_val,
11358 operand_err_set_ty,11360 operand_err_set_ty,
11359 .{11361 .{
...@@ -11374,12 +11376,12 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp...@@ -11374,12 +11376,12 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1137411376
11375 if (scalar_cases_len + multi_cases_len == 0) {11377 if (scalar_cases_len + multi_cases_len == 0) {
11376 if (else_error_ty) |ty| if (ty.errorSetIsEmpty(mod)) {11378 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);
11378 };11380 };
11379 }11381 }
1138011382
11381 if (child_block.is_comptime) {11383 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, .{
11383 .needed_comptime_reason = "condition in comptime switch must be comptime-known",11385 .needed_comptime_reason = "condition in comptime switch must be comptime-known",
11384 .block_comptime_reason = child_block.comptime_reason,11386 .block_comptime_reason = child_block.comptime_reason,
11385 });11387 });
...@@ -11387,17 +11389,17 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp...@@ -11387,17 +11389,17 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
11387 }11389 }
1138811390
11389 const cond = if (extra.data.bits.payload_is_ref) blk: {11391 const cond = if (extra.data.bits.payload_is_ref) blk: {
11390 try sema.checkErrorType(block, src, sema.typeOf(raw_operand_val).elemType2(mod));11392 try sema.checkErrorType(block, main_src, sema.typeOf(raw_operand_val).elemType2(mod));
11391 const loaded = try sema.analyzeLoad(block, src, raw_operand_val, src);11393 const loaded = try sema.analyzeLoad(block, main_src, raw_operand_val, main_src);
11392 break :blk try sema.analyzeIsNonErr(block, src, loaded);11394 break :blk try sema.analyzeIsNonErr(block, main_src, loaded);
11393 } else blk: {11395 } else blk: {
11394 try sema.checkErrorType(block, src, sema.typeOf(raw_operand_val));11396 try sema.checkErrorType(block, main_src, sema.typeOf(raw_operand_val));
11395 break :blk try sema.analyzeIsNonErr(block, src, raw_operand_val);11397 break :blk try sema.analyzeIsNonErr(block, main_src, raw_operand_val);
11396 };11398 };
1139711399
11398 var sub_block = child_block.makeSubBlock();11400 var sub_block = child_block.makeSubBlock();
11399 sub_block.runtime_loop = null;11401 sub_block.runtime_loop = null;
11400 sub_block.runtime_cond = operand_src;11402 sub_block.runtime_cond = main_operand_src;
11401 sub_block.runtime_index.increment();11403 sub_block.runtime_index.increment();
11402 defer sub_block.instructions.deinit(gpa);11404 defer sub_block.instructions.deinit(gpa);
1140311405
...@@ -11406,9 +11408,9 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp...@@ -11406,9 +11408,9 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
11406 defer gpa.free(true_instructions);11408 defer gpa.free(true_instructions);
1140711409
11408 spa.operand = if (extra.data.bits.payload_is_ref)11410 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)
11410 else11412 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
11413 if (extra.data.bits.any_uses_err_capture) {11415 if (extra.data.bits.any_uses_err_capture) {
11414 sema.inst_map.putAssumeCapacity(err_capture_inst, spa.operand);11416 sema.inst_map.putAssumeCapacity(err_capture_inst, spa.operand);
...@@ -11417,10 +11419,10 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp...@@ -11417,10 +11419,10 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
11417 _ = try sema.analyzeSwitchRuntimeBlock(11419 _ = try sema.analyzeSwitchRuntimeBlock(
11418 spa,11420 spa,
11419 &sub_block,11421 &sub_block,
11420 src,11422 switch_src,
11421 try sema.switchCond(block, operand_src, spa.operand),11423 try sema.switchCond(block, switch_operand_src, spa.operand),
11422 operand_err_set_ty,11424 operand_err_set_ty,
11423 operand_src,11425 switch_operand_src,
11424 case_vals,11426 case_vals,
11425 .{11427 .{
11426 .body = else_case.body,11428 .body = else_case.body,
...@@ -11434,7 +11436,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp...@@ -11434,7 +11436,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
11434 false,11436 false,
11435 undefined,11437 undefined,
11436 true,11438 true,
11437 src_node_offset,11439 switch_src_node_offset,
11438 else_prong_src,11440 else_prong_src,
11439 undefined,11441 undefined,
11440 seen_errors,11442 seen_errors,
...@@ -11461,7 +11463,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp...@@ -11461,7 +11463,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
11461 sema.air_extra.appendSliceAssumeCapacity(@ptrCast(true_instructions));11463 sema.air_extra.appendSliceAssumeCapacity(@ptrCast(true_instructions));
11462 sema.air_extra.appendSliceAssumeCapacity(@ptrCast(sub_block.instructions.items));11464 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);
11465}11467}
1146611468
11467fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_ref: bool) CompileError!Air.Inst.Ref {11469fn 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 {...@@ -2786,6 +2786,7 @@ pub const Inst = struct {
2786 pub const SwitchBlockErrUnion = struct {2786 pub const SwitchBlockErrUnion = struct {
2787 operand: Ref,2787 operand: Ref,
2788 bits: Bits,2788 bits: Bits,
2789 main_src_node_offset: i32,
27892790
2790 pub const Bits = packed struct(u32) {2791 pub const Bits = packed struct(u32) {
2791 /// If true, one or more prongs have multiple items.2792 /// If true, one or more prongs have multiple items.