| author | |
| committer | |
| log | eb4024036d3e8e0c4a8b0dcf09da107421f3fa01 |
| tree | 6694d3bb97fa0aaec6e947536263ce78f60e72af |
| parent | f2e249e92020462ac29f34fc3d45cef3dc90a3b2 |
| signature |
2 files changed, 20 insertions(+), 0 deletions(-)
src/Sema.zig+5| ... | ... | @@ -4221,6 +4221,11 @@ fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 4221 | 4221 | const msg = try sema.errMsg(block, arg_src, "type '{}' is not indexable and not a range", .{object_ty.fmt(sema.mod)}); |
| 4222 | 4222 | errdefer msg.destroy(sema.gpa); |
| 4223 | 4223 | try sema.errNote(block, arg_src, msg, "for loop operand must be a range, array, slice, tuple, or vector", .{}); |
| 4224 | ||
| 4225 | if (object_ty.zigTypeTag(mod) == .ErrorUnion) { | |
| 4226 | try sema.errNote(block, arg_src, msg, "consider using 'try', 'catch', or 'if'", .{}); | |
| 4227 | } | |
| 4228 | ||
| 4224 | 4229 | break :msg msg; |
| 4225 | 4230 | }; |
| 4226 | 4231 | return sema.failWithOwnedErrorMsg(block, msg); |
test/cases/compile_errors/for_loop_error_union.zig created+15| ... | ... | @@ -0,0 +1,15 @@ |
| 1 | fn b() !u32 { | |
| 2 | return 2; | |
| 3 | } | |
| 4 | ||
| 5 | export fn a() void { | |
| 6 | for (b()) |_| {} | |
| 7 | } | |
| 8 | ||
| 9 | // error | |
| 10 | // backend=stage2 | |
| 11 | // target=native | |
| 12 | // | |
| 13 | // :6:11: error: type '@typeInfo(@typeInfo(@TypeOf(tmp.b)).Fn.return_type.?).ErrorUnion.error_set!u32' is not indexable and not a range | |
| 14 | // :6:11: note: for loop operand must be a range, array, slice, tuple, or vector | |
| 15 | // :6:11: note: consider using 'try', 'catch', or 'if' |