Commit 1a15fbe960

mikastiv <mikastiv@outlook.com>
2024-11-04 04:48:41
Sema: add note suggesting dropping try on non error-unions
1 parent 19fc5f4
Changed files (3)
src/Sema.zig
@@ -1905,8 +1905,12 @@ fn analyzeBodyInner(
                 const err_union = try sema.resolveInst(extra.data.operand);
                 const err_union_ty = sema.typeOf(err_union);
                 if (err_union_ty.zigTypeTag(zcu) != .error_union) {
-                    return sema.fail(block, operand_src, "expected error union type, found '{f}'", .{
-                        err_union_ty.fmt(pt),
+                    return sema.failWithOwnedErrorMsg(block, msg: {
+                        const msg = try sema.errMsg(operand_src, "expected error union type, found '{f}'", .{err_union_ty.fmt(pt)});
+                        errdefer msg.destroy(sema.gpa);
+                        try sema.addDeclaredHereNote(msg, err_union_ty);
+                        try sema.errNote(operand_src, msg, "consider omitting 'try'", .{});
+                        break :msg msg;
                     });
                 }
                 const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union);
@@ -18175,8 +18179,12 @@ fn zirTry(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileError!
     const pt = sema.pt;
     const zcu = pt.zcu;
     if (err_union_ty.zigTypeTag(zcu) != .error_union) {
-        return sema.fail(parent_block, operand_src, "expected error union type, found '{f}'", .{
-            err_union_ty.fmt(pt),
+        return sema.failWithOwnedErrorMsg(parent_block, msg: {
+            const msg = try sema.errMsg(operand_src, "expected error union type, found '{f}'", .{err_union_ty.fmt(pt)});
+            errdefer msg.destroy(sema.gpa);
+            try sema.addDeclaredHereNote(msg, err_union_ty);
+            try sema.errNote(operand_src, msg, "consider omitting 'try'", .{});
+            break :msg msg;
         });
     }
     const is_non_err = try sema.analyzeIsNonErrComptimeOnly(parent_block, operand_src, err_union);
@@ -18235,8 +18243,12 @@ fn zirTryPtr(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileErr
     const pt = sema.pt;
     const zcu = pt.zcu;
     if (err_union_ty.zigTypeTag(zcu) != .error_union) {
-        return sema.fail(parent_block, operand_src, "expected error union type, found '{f}'", .{
-            err_union_ty.fmt(pt),
+        return sema.failWithOwnedErrorMsg(parent_block, msg: {
+            const msg = try sema.errMsg(operand_src, "expected error union type, found '{f}'", .{err_union_ty.fmt(pt)});
+            errdefer msg.destroy(sema.gpa);
+            try sema.addDeclaredHereNote(msg, err_union_ty);
+            try sema.errNote(operand_src, msg, "consider omitting 'try'", .{});
+            break :msg msg;
         });
     }
     const is_non_err = try sema.analyzeIsNonErrComptimeOnly(parent_block, operand_src, err_union);
test/cases/compile_errors/comptime_try_non_error.zig
@@ -13,4 +13,5 @@ pub fn bar() u8 {
 // error
 //
 // :6:12: error: expected error union type, found 'u8'
+// :6:12: note: consider omitting 'try'
 // :2:8: note: called at comptime here
test/cases/compile_errors/redundant_try.zig
@@ -43,10 +43,16 @@ comptime {
 // error
 //
 // :5:23: error: expected error union type, found 'comptime_int'
+// :5:23: note: consider omitting 'try'
 // :10:23: error: expected error union type, found '@TypeOf(.{})'
+// :10:23: note: consider omitting 'try'
 // :15:23: error: expected error union type, found 'tmp.S'
 // :1:11: note: struct declared here
+// :15:23: note: consider omitting 'try'
 // :20:27: error: expected error union type, found 'tmp.S'
 // :1:11: note: struct declared here
+// :20:27: note: consider omitting 'try'
 // :25:23: error: expected error union type, found 'struct { comptime *const [5:0]u8 = "hello" }'
+// :25:23: note: consider omitting 'try'
 // :31:13: error: expected error union type, found 'u32'
+// :31:13: note: consider omitting 'try'