fix source location for not-indexable for loop errors

This commit is contained in:
Andrew Kelley
2023-02-18 13:22:29 -07:00
parent bcb72401d3
commit 601db3981c
2 changed files with 16 additions and 7 deletions

View File

@@ -3929,11 +3929,15 @@ fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
.Int, .ComptimeInt => true,
else => false,
};
const arg_src: LazySrcLoc = .{ .for_input = .{
.for_node_offset = inst_data.src_node,
.input_index = i,
} };
const arg_len = if (is_int) object else l: {
try checkIndexable(sema, block, src, object_ty);
try checkIndexable(sema, block, arg_src, object_ty);
if (!object_ty.indexableHasLen()) continue;
break :l try sema.fieldVal(block, src, object, "len", src);
break :l try sema.fieldVal(block, arg_src, object, "len", arg_src);
};
if (len == .none) {
len = arg_len;
@@ -3949,14 +3953,10 @@ fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
.for_node_offset = inst_data.src_node,
.input_index = len_idx,
} };
const b_src: LazySrcLoc = .{ .for_input = .{
.for_node_offset = inst_data.src_node,
.input_index = i,
} };
try sema.errNote(block, a_src, msg, "length {} here", .{
v.fmtValue(Type.usize, sema.mod),
});
try sema.errNote(block, b_src, msg, "length {} here", .{
try sema.errNote(block, arg_src, msg, "length {} here", .{
arg_val.fmtValue(Type.usize, sema.mod),
});
break :msg msg;

View File

@@ -3,6 +3,13 @@ export fn a() void {
_ = i; _ = j;
}
}
export fn b() void {
const s1 = "hello";
const s2 = true;
for (s1, s2) |i, j| {
_ = i; _ = j;
}
}
// error
// backend=stage2
@@ -11,3 +18,5 @@ export fn a() void {
// :2:5: error: non-matching for loop lengths
// :2:11: note: length 10 here
// :2:19: note: length 11 here
// :9:14: error: type 'bool' does not support indexing
// :9:14: note: for loop operand must be an array, slice, tuple, or vector