authorgravatar for will.lillis24@gmail.comWill Lillis <will.lillis24@gmail.com> 2024-09-23 16:04:24-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-09-23 13:04:24-07:00
log943176bbfcf3125451fa64e082ee357c28e70413
tree7b72a227aadaaf5d96d59240ea6c97c12cbf1753
parent509639717ac8903fe340a02cef844383183bf716
signaturebadge-check Signed by PGP key B5690EEEBB952194

fix: Add error note when attempt is made to destructure error union (#21491)

closes #21417

2 files changed, 18 insertions(+), 0 deletions(-)

src/Sema.zig+3
......@@ -5428,6 +5428,9 @@ fn zirValidateDestructure(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
54285428 const msg = try sema.errMsg(src, "type '{}' cannot be destructured", .{operand_ty.fmt(pt)});
54295429 errdefer msg.destroy(sema.gpa);
54305430 try sema.errNote(destructure_src, msg, "result destructured here", .{});
5431 if (operand_ty.zigTypeTag(pt.zcu) == .error_union) {
5432 try sema.errNote(src, msg, "consider using 'try', 'catch', or 'if'", .{});
5433 }
54315434 break :msg msg;
54325435 });
54335436 }
test/cases/compile_errors/destructure_error_union.zig created+15
......@@ -0,0 +1,15 @@
1pub export fn entry() void {
2 const Foo = struct { u8, u8 };
3 const foo: anyerror!Foo = error.Failure;
4 const bar, const baz = foo;
5 _ = bar;
6 _ = baz;
7}
8
9// error
10// backend=stage2
11// target=native
12//
13// :4:28: error: type 'anyerror!tmp.entry.Foo' cannot be destructured
14// :4:26: note: result destructured here
15// :4:28: note: consider using 'try', 'catch', or 'if'