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

This commit is contained in:
mikastiv 2024-11-03 22:48:41 -05:00 committed by Matthew Lugg
parent 19fc5f4fb2
commit 1a15fbe960
3 changed files with 25 additions and 6 deletions

View File

@ -1905,8 +1905,12 @@ fn analyzeBodyInner(
const err_union = try sema.resolveInst(extra.data.operand); const err_union = try sema.resolveInst(extra.data.operand);
const err_union_ty = sema.typeOf(err_union); const err_union_ty = sema.typeOf(err_union);
if (err_union_ty.zigTypeTag(zcu) != .error_union) { if (err_union_ty.zigTypeTag(zcu) != .error_union) {
return sema.fail(block, operand_src, "expected error union type, found '{f}'", .{ return sema.failWithOwnedErrorMsg(block, msg: {
err_union_ty.fmt(pt), 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); 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 pt = sema.pt;
const zcu = pt.zcu; const zcu = pt.zcu;
if (err_union_ty.zigTypeTag(zcu) != .error_union) { if (err_union_ty.zigTypeTag(zcu) != .error_union) {
return sema.fail(parent_block, operand_src, "expected error union type, found '{f}'", .{ return sema.failWithOwnedErrorMsg(parent_block, msg: {
err_union_ty.fmt(pt), 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); 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 pt = sema.pt;
const zcu = pt.zcu; const zcu = pt.zcu;
if (err_union_ty.zigTypeTag(zcu) != .error_union) { if (err_union_ty.zigTypeTag(zcu) != .error_union) {
return sema.fail(parent_block, operand_src, "expected error union type, found '{f}'", .{ return sema.failWithOwnedErrorMsg(parent_block, msg: {
err_union_ty.fmt(pt), 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); const is_non_err = try sema.analyzeIsNonErrComptimeOnly(parent_block, operand_src, err_union);

View File

@ -13,4 +13,5 @@ pub fn bar() u8 {
// error // error
// //
// :6:12: error: expected error union type, found 'u8' // :6:12: error: expected error union type, found 'u8'
// :6:12: note: consider omitting 'try'
// :2:8: note: called at comptime here // :2:8: note: called at comptime here

View File

@ -43,10 +43,16 @@ comptime {
// error // error
// //
// :5:23: error: expected error union type, found 'comptime_int' // :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: error: expected error union type, found '@TypeOf(.{})'
// :10:23: note: consider omitting 'try'
// :15:23: error: expected error union type, found 'tmp.S' // :15:23: error: expected error union type, found 'tmp.S'
// :1:11: note: struct declared here // :1:11: note: struct declared here
// :15:23: note: consider omitting 'try'
// :20:27: error: expected error union type, found 'tmp.S' // :20:27: error: expected error union type, found 'tmp.S'
// :1:11: note: struct declared here // :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: 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: error: expected error union type, found 'u32'
// :31:13: note: consider omitting 'try'