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(...@@ -1905,8 +1905,12 @@ fn analyzeBodyInner(
1905 const err_union = try sema.resolveInst(extra.data.operand);1905 const err_union = try sema.resolveInst(extra.data.operand);
1906 const err_union_ty = sema.typeOf(err_union);1906 const err_union_ty = sema.typeOf(err_union);
1907 if (err_union_ty.zigTypeTag(zcu) != .error_union) {1907 if (err_union_ty.zigTypeTag(zcu) != .error_union) {
1908 return sema.fail(block, operand_src, "expected error union type, found '{f}'", .{1908 return sema.failWithOwnedErrorMsg(block, msg: {
1909 err_union_ty.fmt(pt),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;
1910 });1914 });
1911 }1915 }
1912 const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union);1916 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!...@@ -18175,8 +18179,12 @@ fn zirTry(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileError!
18175 const pt = sema.pt;18179 const pt = sema.pt;
18176 const zcu = pt.zcu;18180 const zcu = pt.zcu;
18177 if (err_union_ty.zigTypeTag(zcu) != .error_union) {18181 if (err_union_ty.zigTypeTag(zcu) != .error_union) {
18178 return sema.fail(parent_block, operand_src, "expected error union type, found '{f}'", .{18182 return sema.failWithOwnedErrorMsg(parent_block, msg: {
18179 err_union_ty.fmt(pt),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;
18180 });18188 });
18181 }18189 }
18182 const is_non_err = try sema.analyzeIsNonErrComptimeOnly(parent_block, operand_src, err_union);18190 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...@@ -18235,8 +18243,12 @@ fn zirTryPtr(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileErr
18235 const pt = sema.pt;18243 const pt = sema.pt;
18236 const zcu = pt.zcu;18244 const zcu = pt.zcu;
18237 if (err_union_ty.zigTypeTag(zcu) != .error_union) {18245 if (err_union_ty.zigTypeTag(zcu) != .error_union) {
18238 return sema.fail(parent_block, operand_src, "expected error union type, found '{f}'", .{18246 return sema.failWithOwnedErrorMsg(parent_block, msg: {
18239 err_union_ty.fmt(pt),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;
18240 });18252 });
18241 }18253 }
18242 const is_non_err = try sema.analyzeIsNonErrComptimeOnly(parent_block, operand_src, err_union);18254 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 {...@@ -13,4 +13,5 @@ pub fn bar() u8 {
13// error13// error
14//14//
15// :6:12: error: expected error union type, found 'u8'15// :6:12: error: expected error union type, found 'u8'
16// :6:12: note: consider omitting 'try'
16// :2:8: note: called at comptime here17// :2:8: note: called at comptime here
test/cases/compile_errors/redundant_try.zig+6
...@@ -43,10 +43,16 @@ comptime {...@@ -43,10 +43,16 @@ comptime {
43// error43// error
44//44//
45// :5:23: error: expected error union type, found 'comptime_int'45// :5:23: error: expected error union type, found 'comptime_int'
46// :5:23: note: consider omitting 'try'
46// :10:23: error: expected error union type, found '@TypeOf(.{})'47// :10:23: error: expected error union type, found '@TypeOf(.{})'
48// :10:23: note: consider omitting 'try'
47// :15:23: error: expected error union type, found 'tmp.S'49// :15:23: error: expected error union type, found 'tmp.S'
48// :1:11: note: struct declared here50// :1:11: note: struct declared here
51// :15:23: note: consider omitting 'try'
49// :20:27: error: expected error union type, found 'tmp.S'52// :20:27: error: expected error union type, found 'tmp.S'
50// :1:11: note: struct declared here53// :1:11: note: struct declared here
54// :20:27: note: consider omitting 'try'
51// :25:23: error: expected error union type, found 'struct { comptime *const [5:0]u8 = "hello" }'55// :25:23: error: expected error union type, found 'struct { comptime *const [5:0]u8 = "hello" }'
56// :25:23: note: consider omitting 'try'
52// :31:13: error: expected error union type, found 'u32'57// :31:13: error: expected error union type, found 'u32'
58// :31:13: note: consider omitting 'try'