aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2024-05-21 22:37:00 +0300
committerGitHub <noreply@github.com>2024-05-21 22:37:00 +0300
commiteb0f871cb90cf7e3a1cfa7a160d3f224b42fd799 (patch)
tree9f7eb7266e469439ecb3ab3205060b1b6eb47914 /src
parentae44e199a8caa8d26db04b81867acb81d54ba217 (diff)
parentac55685a94b3db97e9d2eadef0432948b3c16a03 (diff)
downloadzig-eb0f871cb90cf7e3a1cfa7a160d3f224b42fd799.tar.gz
zig-eb0f871cb90cf7e3a1cfa7a160d3f224b42fd799.zip
Merge pull request #19961 from wooster0/errorstuff
Sema: improvements to error messages related to the handling of (error) values
Diffstat (limited to 'src')
-rw-r--r--src/Sema.zig35
1 files changed, 26 insertions, 9 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index 37fdf0adb5..7a1885d652 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -2228,8 +2228,20 @@ fn failWithModRemNegative(sema: *Sema, block: *Block, src: LazySrcLoc, lhs_ty: T
});
}
-fn failWithExpectedOptionalType(sema: *Sema, block: *Block, src: LazySrcLoc, optional_ty: Type) CompileError {
- return sema.fail(block, src, "expected optional type, found '{}'", .{optional_ty.fmt(sema.mod)});
+fn failWithExpectedOptionalType(sema: *Sema, block: *Block, src: LazySrcLoc, non_optional_ty: Type) CompileError {
+ const mod = sema.mod;
+ const msg = msg: {
+ const msg = try sema.errMsg(block, src, "expected optional type, found '{}'", .{
+ non_optional_ty.fmt(mod),
+ });
+ errdefer msg.destroy(sema.gpa);
+ if (non_optional_ty.zigTypeTag(mod) == .ErrorUnion) {
+ try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{});
+ }
+ try addDeclaredHereNote(sema, msg, non_optional_ty);
+ break :msg msg;
+ };
+ return sema.failWithOwnedErrorMsg(block, msg);
}
fn failWithArrayInitNotSupported(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError {
@@ -3557,9 +3569,10 @@ fn ensureResultUsed(
const mod = sema.mod;
switch (ty.zigTypeTag(mod)) {
.Void, .NoReturn => return,
- .ErrorSet, .ErrorUnion => {
+ .ErrorSet => return sema.fail(block, src, "error set is ignored", .{}),
+ .ErrorUnion => {
const msg = msg: {
- const msg = try sema.errMsg(block, src, "error is ignored", .{});
+ const msg = try sema.errMsg(block, src, "error union is ignored", .{});
errdefer msg.destroy(sema.gpa);
try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{});
break :msg msg;
@@ -3571,7 +3584,7 @@ fn ensureResultUsed(
const msg = try sema.errMsg(block, src, "value of type '{}' ignored", .{ty.fmt(sema.mod)});
errdefer msg.destroy(sema.gpa);
try sema.errNote(block, src, msg, "all non-void values must be used", .{});
- try sema.errNote(block, src, msg, "this error can be suppressed by assigning the value to '_'", .{});
+ try sema.errNote(block, src, msg, "to discard the value, assign it to '_'", .{});
break :msg msg;
};
return sema.failWithOwnedErrorMsg(block, msg);
@@ -3589,9 +3602,10 @@ fn zirEnsureResultNonError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
const src = inst_data.src();
const operand_ty = sema.typeOf(operand);
switch (operand_ty.zigTypeTag(mod)) {
- .ErrorSet, .ErrorUnion => {
+ .ErrorSet => return sema.fail(block, src, "error set is discarded", .{}),
+ .ErrorUnion => {
const msg = msg: {
- const msg = try sema.errMsg(block, src, "error is discarded", .{});
+ const msg = try sema.errMsg(block, src, "error union is discarded", .{});
errdefer msg.destroy(sema.gpa);
try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{});
break :msg msg;
@@ -9038,7 +9052,7 @@ fn analyzeOptionalPayloadPtr(
const opt_type = optional_ptr_ty.childType(zcu);
if (opt_type.zigTypeTag(zcu) != .Optional) {
- return sema.fail(block, src, "expected optional type, found '{}'", .{opt_type.fmt(zcu)});
+ return sema.failWithExpectedOptionalType(block, src, opt_type);
}
const child_type = opt_type.optionalChild(zcu);
@@ -9531,7 +9545,7 @@ fn handleExternLibName(
return sema.fail(
block,
src_loc,
- "dependency on dynamic library '{s}' requires enabling Position Independent Code. Fixed by '-l{s}' or '-fPIC'.",
+ "dependency on dynamic library '{s}' requires enabling Position Independent Code; fixed by '-l{s}' or '-fPIC'",
.{ lib_name, lib_name },
);
}
@@ -27875,6 +27889,9 @@ fn fieldCallBind(
const decl = mod.declPtr(decl_idx);
try mod.errNoteNonLazy(decl.srcLoc(mod), msg, "'{}' is not a member function", .{field_name.fmt(ip)});
}
+ if (concrete_ty.zigTypeTag(mod) == .ErrorUnion) {
+ try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{});
+ }
break :msg msg;
};
return sema.failWithOwnedErrorMsg(block, msg);