authorgravatar for mikastiv@outlook.commikastiv <mikastiv@outlook.com> 2024-11-03 22:48:41-05:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-07-31 16:55:17+01:00
log1a15fbe9607c74096c875f6d871213c7d4db1483
tree454ca74cfcfb77cb7beed54036fd62215f95870d
parent19fc5f4fb29a525252b2eaf3f6388d07f97bd32f

Sema: add note suggesting dropping try on non error-unions


3 files changed, 25 insertions(+), 6 deletions(-)

src/Sema.zig+18-6
......@@ -1905,8 +1905,12 @@ fn analyzeBodyInner(
19051905 const err_union = try sema.resolveInst(extra.data.operand);
19061906 const err_union_ty = sema.typeOf(err_union);
19071907 if (err_union_ty.zigTypeTag(zcu) != .error_union) {
1908 return sema.fail(block, operand_src, "expected error union type, found '{f}'", .{
1909 err_union_ty.fmt(pt),
1908 return sema.failWithOwnedErrorMsg(block, msg: {
1909 const msg = try sema.errMsg(operand_src, "expected error union type, found '{f}'", .{err_union_ty.fmt(pt)});
1910 errdefer msg.destroy(sema.gpa);
1911 try sema.addDeclaredHereNote(msg, err_union_ty);
1912 try sema.errNote(operand_src, msg, "consider omitting 'try'", .{});
1913 break :msg msg;
19101914 });
19111915 }
19121916 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!
1817518179 const pt = sema.pt;
1817618180 const zcu = pt.zcu;
1817718181 if (err_union_ty.zigTypeTag(zcu) != .error_union) {
18178 return sema.fail(parent_block, operand_src, "expected error union type, found '{f}'", .{
18179 err_union_ty.fmt(pt),
18182 return sema.failWithOwnedErrorMsg(parent_block, msg: {
18183 const msg = try sema.errMsg(operand_src, "expected error union type, found '{f}'", .{err_union_ty.fmt(pt)});
18184 errdefer msg.destroy(sema.gpa);
18185 try sema.addDeclaredHereNote(msg, err_union_ty);
18186 try sema.errNote(operand_src, msg, "consider omitting 'try'", .{});
18187 break :msg msg;
1818018188 });
1818118189 }
1818218190 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
1823518243 const pt = sema.pt;
1823618244 const zcu = pt.zcu;
1823718245 if (err_union_ty.zigTypeTag(zcu) != .error_union) {
18238 return sema.fail(parent_block, operand_src, "expected error union type, found '{f}'", .{
18239 err_union_ty.fmt(pt),
18246 return sema.failWithOwnedErrorMsg(parent_block, msg: {
18247 const msg = try sema.errMsg(operand_src, "expected error union type, found '{f}'", .{err_union_ty.fmt(pt)});
18248 errdefer msg.destroy(sema.gpa);
18249 try sema.addDeclaredHereNote(msg, err_union_ty);
18250 try sema.errNote(operand_src, msg, "consider omitting 'try'", .{});
18251 break :msg msg;
1824018252 });
1824118253 }
1824218254 const is_non_err = try sema.analyzeIsNonErrComptimeOnly(parent_block, operand_src, err_union);
test/cases/compile_errors/comptime_try_non_error.zig+1
......@@ -13,4 +13,5 @@ pub fn bar() u8 {
1313// error
1414//
1515// :6:12: error: expected error union type, found 'u8'
16// :6:12: note: consider omitting 'try'
1617// :2:8: note: called at comptime here
test/cases/compile_errors/redundant_try.zig+6
......@@ -43,10 +43,16 @@ comptime {
4343// error
4444//
4545// :5:23: error: expected error union type, found 'comptime_int'
46// :5:23: note: consider omitting 'try'
4647// :10:23: error: expected error union type, found '@TypeOf(.{})'
48// :10:23: note: consider omitting 'try'
4749// :15:23: error: expected error union type, found 'tmp.S'
4850// :1:11: note: struct declared here
51// :15:23: note: consider omitting 'try'
4952// :20:27: error: expected error union type, found 'tmp.S'
5053// :1:11: note: struct declared here
54// :20:27: note: consider omitting 'try'
5155// :25:23: error: expected error union type, found 'struct { comptime *const [5:0]u8 = "hello" }'
56// :25:23: note: consider omitting 'try'
5257// :31:13: error: expected error union type, found 'u32'
58// :31:13: note: consider omitting 'try'