diff options
| author | Veikka Tuominen <git@vexu.eu> | 2022-07-19 13:19:23 +0300 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-07-21 12:21:30 -0700 |
| commit | 83b2d2cd3eb701b43d17404e73933bc00f7be828 (patch) | |
| tree | fae2a1c6e8e32ed4056b59ae639133e207a43916 /src | |
| parent | 9fb8d21a019deab308a63f7959dab2ee05385969 (diff) | |
| download | zig-83b2d2cd3eb701b43d17404e73933bc00f7be828.tar.gz zig-83b2d2cd3eb701b43d17404e73933bc00f7be828.zip | |
Sema: better source location for incompatible capture group
Diffstat (limited to 'src')
| -rw-r--r-- | src/Module.zig | 28 | ||||
| -rw-r--r-- | src/Sema.zig | 23 |
2 files changed, 42 insertions, 9 deletions
diff --git a/src/Module.zig b/src/Module.zig index fd8c217bd1..8389dcec19 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -2348,6 +2348,26 @@ pub const SrcLoc = struct { } } else unreachable; }, + .node_offset_switch_prong_capture => |node_off| { + const tree = try src_loc.file_scope.getTree(gpa); + const case_node = src_loc.declRelativeToNodeIndex(node_off); + const node_tags = tree.nodes.items(.tag); + const case = switch (node_tags[case_node]) { + .switch_case_one => tree.switchCaseOne(case_node), + .switch_case => tree.switchCase(case_node), + else => unreachable, + }; + const start_tok = case.payload_token.?; + const token_tags = tree.tokens.items(.tag); + const end_tok = switch (token_tags[start_tok]) { + .asterisk => start_tok + 1, + else => start_tok, + }; + const start = tree.tokens.items(.start)[start_tok]; + const end_start = tree.tokens.items(.start)[end_tok]; + const end = end_start + @intCast(u32, tree.tokenSlice(end_tok).len); + return Span{ .start = start, .end = end, .main = start }; + }, .node_offset_fn_type_align => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); const node_datas = tree.nodes.items(.data); @@ -2877,6 +2897,9 @@ pub const LazySrcLoc = union(enum) { /// range nodes. The error applies to all of them. /// The Decl is determined contextually. node_offset_switch_range: i32, + /// The source location points to the capture of a switch_prong. + /// The Decl is determined contextually. + node_offset_switch_prong_capture: i32, /// The source location points to the align expr of a function type /// expression, found by taking this AST node index offset from the containing /// Decl AST node, which points to a function type AST node. Next, navigate to @@ -3017,6 +3040,7 @@ pub const LazySrcLoc = union(enum) { .node_offset_switch_operand, .node_offset_switch_special_prong, .node_offset_switch_range, + .node_offset_switch_prong_capture, .node_offset_fn_type_align, .node_offset_fn_type_addrspace, .node_offset_fn_type_section, @@ -5602,6 +5626,7 @@ pub const SwitchProngSrc = union(enum) { scalar: u32, multi: Multi, range: Multi, + multi_capture: u32, pub const Multi = struct { prong: u32, @@ -5657,6 +5682,9 @@ pub const SwitchProngSrc = union(enum) { .scalar => |i| if (!is_multi and i == scalar_i) return LazySrcLoc.nodeOffset( decl.nodeIndexToRelative(case.ast.values[0]), ), + .multi_capture => |i| if (is_multi and i == multi_i) { + return LazySrcLoc{ .node_offset_switch_prong_capture = decl.nodeIndexToRelative(case_node) }; + }, .multi => |s| if (is_multi and s.prong == multi_i) { var item_i: u32 = 0; for (case.ast.values) |item_node| { diff --git a/src/Sema.zig b/src/Sema.zig index af44ffcedd..d8a6eddbb6 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -8197,7 +8197,6 @@ fn zirSwitchCapture( const switch_info = zir_datas[capture_info.switch_inst].pl_node; const switch_extra = sema.code.extraData(Zir.Inst.SwitchBlock, switch_info.payload_index); const operand_src: LazySrcLoc = .{ .node_offset_switch_operand = switch_info.src_node }; - const switch_src = switch_info.src(); const operand_is_ref = switch_extra.data.bits.is_ref; const cond_inst = Zir.refToIndex(switch_extra.data.operand).?; const cond_info = sema.code.instructions.items(.data)[cond_inst].un_node; @@ -8247,7 +8246,7 @@ fn zirSwitchCapture( const first_field_index = @intCast(u32, enum_ty.enumTagFieldIndex(first_item_val, sema.mod).?); const first_field = union_obj.fields.values()[first_field_index]; - for (items[1..]) |item| { + for (items[1..]) |item, i| { const item_ref = try sema.resolveInst(item); // Previous switch validation ensured this will succeed const item_val = sema.resolveConstValue(block, .unneeded, item_ref, undefined) catch unreachable; @@ -8255,11 +8254,17 @@ fn zirSwitchCapture( const field_index = enum_ty.enumTagFieldIndex(item_val, sema.mod).?; const field = union_obj.fields.values()[field_index]; if (!field.ty.eql(first_field.ty, sema.mod)) { - const first_item_src = switch_src; // TODO better source location - const item_src = switch_src; const msg = msg: { - const msg = try sema.errMsg(block, switch_src, "capture group with incompatible types", .{}); + const raw_capture_src = Module.SwitchProngSrc{ .multi_capture = capture_info.prong_index }; + const capture_src = raw_capture_src.resolve(sema.gpa, sema.mod.declPtr(block.src_decl), switch_info.src_node, .first); + + const msg = try sema.errMsg(block, capture_src, "capture group with incompatible types", .{}); errdefer msg.destroy(sema.gpa); + + const raw_first_item_src = Module.SwitchProngSrc{ .multi = .{ .prong = capture_info.prong_index, .item = 0 } }; + const first_item_src = raw_first_item_src.resolve(sema.gpa, sema.mod.declPtr(block.src_decl), switch_info.src_node, .first); + const raw_item_src = Module.SwitchProngSrc{ .multi = .{ .prong = capture_info.prong_index, .item = 1 + @intCast(u32, i) } }; + const item_src = raw_item_src.resolve(sema.gpa, sema.mod.declPtr(block.src_decl), switch_info.src_node, .first); try sema.errNote(block, first_item_src, msg, "type '{}' here", .{first_field.ty.fmt(sema.mod)}); try sema.errNote(block, item_src, msg, "type '{}' here", .{field.ty.fmt(sema.mod)}); break :msg msg; @@ -21094,17 +21099,17 @@ const InMemoryCoercionResult = union(enum) { } } if (!actual_noalias) { - try sema.errNote(block, src, msg, "regular paramter {d} cannot cast into a noalias paramter", .{index}); + try sema.errNote(block, src, msg, "regular parameter {d} cannot cast into a noalias parameter", .{index}); } else { - try sema.errNote(block, src, msg, "noalias paramter {d} cannot cast into a regular paramter", .{index}); + try sema.errNote(block, src, msg, "noalias parameter {d} cannot cast into a regular parameter", .{index}); } break; }, .fn_param_comptime => |param| { if (param.wanted) { - try sema.errNote(block, src, msg, "non-comptime paramter {d} cannot cast into a comptime paramter", .{param.index}); + try sema.errNote(block, src, msg, "non-comptime parameter {d} cannot cast into a comptime parameter", .{param.index}); } else { - try sema.errNote(block, src, msg, "comptime paramter {d} cannot cast into a non-comptime paramter", .{param.index}); + try sema.errNote(block, src, msg, "comptime parameter {d} cannot cast into a non-comptime parameter", .{param.index}); } break; }, |
