From 1a15fbe9607c74096c875f6d871213c7d4db1483 Mon Sep 17 00:00:00 2001 From: mikastiv Date: Sun, 3 Nov 2024 22:48:41 -0500 Subject: [PATCH] Sema: add note suggesting dropping try on non error-unions --- src/Sema.zig | 24 ++++++++++++++----- .../compile_errors/comptime_try_non_error.zig | 1 + test/cases/compile_errors/redundant_try.zig | 6 +++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 9345c4bcac..5f22e48530 100644 --- a/src/Sema.zig +++ b/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); diff --git a/test/cases/compile_errors/comptime_try_non_error.zig b/test/cases/compile_errors/comptime_try_non_error.zig index 8d61df6e9a..44f7dbe614 100644 --- a/test/cases/compile_errors/comptime_try_non_error.zig +++ b/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 diff --git a/test/cases/compile_errors/redundant_try.zig b/test/cases/compile_errors/redundant_try.zig index 5472701ce0..73beca2c2d 100644 --- a/test/cases/compile_errors/redundant_try.zig +++ b/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'