mirror of
https://github.com/ziglang/zig.git
synced 2026-02-13 21:08:36 +00:00
Add error hint when looping over ErrorUnion
This commit is contained in:
parent
f2e249e920
commit
eb4024036d
@ -4221,6 +4221,11 @@ fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
|
||||
const msg = try sema.errMsg(block, arg_src, "type '{}' is not indexable and not a range", .{object_ty.fmt(sema.mod)});
|
||||
errdefer msg.destroy(sema.gpa);
|
||||
try sema.errNote(block, arg_src, msg, "for loop operand must be a range, array, slice, tuple, or vector", .{});
|
||||
|
||||
if (object_ty.zigTypeTag(mod) == .ErrorUnion) {
|
||||
try sema.errNote(block, arg_src, msg, "consider using 'try', 'catch', or 'if'", .{});
|
||||
}
|
||||
|
||||
break :msg msg;
|
||||
};
|
||||
return sema.failWithOwnedErrorMsg(block, msg);
|
||||
|
||||
15
test/cases/compile_errors/for_loop_error_union.zig
Normal file
15
test/cases/compile_errors/for_loop_error_union.zig
Normal file
@ -0,0 +1,15 @@
|
||||
fn b() !u32 {
|
||||
return 2;
|
||||
}
|
||||
|
||||
export fn a() void {
|
||||
for (b()) |_| {}
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :6:11: error: type '@typeInfo(@typeInfo(@TypeOf(tmp.b)).Fn.return_type.?).ErrorUnion.error_set!u32' is not indexable and not a range
|
||||
// :6:11: note: for loop operand must be a range, array, slice, tuple, or vector
|
||||
// :6:11: note: consider using 'try', 'catch', or 'if'
|
||||
Loading…
x
Reference in New Issue
Block a user