aboutsummaryrefslogtreecommitdiff
path: root/src/Sema.zig
diff options
context:
space:
mode:
authorWill Lillis <wlillis@umass.edu>2025-01-26 13:38:07 -0500
committerGitHub <noreply@github.com>2025-01-26 19:38:07 +0100
commit672bc8141fd26b29eb645571be27420db8e4c27e (patch)
tree2f6879b2a1bb3190493a09de66f14f479fff8237 /src/Sema.zig
parent9c6d728b0c9350b0661c8da1134d4fd623b88713 (diff)
downloadzig-672bc8141fd26b29eb645571be27420db8e4c27e.tar.gz
zig-672bc8141fd26b29eb645571be27420db8e4c27e.zip
fix: Only suggest try on destructure of error union if payload type can be destructured (#21510)
Diffstat (limited to 'src/Sema.zig')
-rw-r--r--src/Sema.zig20
1 files changed, 12 insertions, 8 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index 6ad12d1382..72b05d078d 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -5525,6 +5525,14 @@ fn zirValidateDeref(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
}
}
+fn typeIsDestructurable(ty: Type, zcu: *const Zcu) bool {
+ return switch (ty.zigTypeTag(zcu)) {
+ .array, .vector => true,
+ .@"struct" => ty.isTuple(zcu),
+ else => false,
+ };
+}
+
fn zirValidateDestructure(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
const pt = sema.pt;
const zcu = pt.zcu;
@@ -5535,19 +5543,15 @@ fn zirValidateDestructure(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
const operand = try sema.resolveInst(extra.operand);
const operand_ty = sema.typeOf(operand);
- const can_destructure = switch (operand_ty.zigTypeTag(zcu)) {
- .array, .vector => true,
- .@"struct" => operand_ty.isTuple(zcu),
- else => false,
- };
-
- if (!can_destructure) {
+ if (!typeIsDestructurable(operand_ty, zcu)) {
return sema.failWithOwnedErrorMsg(block, msg: {
const msg = try sema.errMsg(src, "type '{}' cannot be destructured", .{operand_ty.fmt(pt)});
errdefer msg.destroy(sema.gpa);
try sema.errNote(destructure_src, msg, "result destructured here", .{});
if (operand_ty.zigTypeTag(pt.zcu) == .error_union) {
- try sema.errNote(src, msg, "consider using 'try', 'catch', or 'if'", .{});
+ const base_op_ty = operand_ty.errorUnionPayload(zcu);
+ if (typeIsDestructurable(base_op_ty, zcu))
+ try sema.errNote(src, msg, "consider using 'try', 'catch', or 'if'", .{});
}
break :msg msg;
});