authorgravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2023-07-21 02:58:41+02:00
committergravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2024-05-14 01:13:49+09:00
log8579904ddd489b73cb61721c31b9e9c14ed9e264
tree00e595203d3b0eab326be9dc189a7d434b670a6c
parent31689d0dd92a683e5ed04facea7bc677ef47785b

Sema: add error note for !?Type types when optional type is expected


2 files changed, 15 insertions(+), 3 deletions(-)

src/Sema.zig+14-3
...@@ -2228,8 +2228,19 @@ fn failWithModRemNegative(sema: *Sema, block: *Block, src: LazySrcLoc, lhs_ty: T...@@ -2228,8 +2228,19 @@ fn failWithModRemNegative(sema: *Sema, block: *Block, src: LazySrcLoc, lhs_ty: T
2228 });2228 });
2229}2229}
22302230
2231fn failWithExpectedOptionalType(sema: *Sema, block: *Block, src: LazySrcLoc, optional_ty: Type) CompileError {2231fn failWithExpectedOptionalType(sema: *Sema, block: *Block, src: LazySrcLoc, non_optional_ty: Type) CompileError {
2232 return sema.fail(block, src, "expected optional type, found '{}'", .{optional_ty.fmt(sema.mod)});2232 const mod = sema.mod;
2233 const msg = msg: {
2234 const msg = try sema.errMsg(block, src, "expected optional type, found '{}'", .{
2235 non_optional_ty.fmt(mod),
2236 });
2237 errdefer msg.destroy(sema.gpa);
2238 if (non_optional_ty.zigTypeTag(mod) == .ErrorUnion) {
2239 try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{});
2240 }
2241 break :msg msg;
2242 };
2243 return sema.failWithOwnedErrorMsg(block, msg);
2233}2244}
22342245
2235fn failWithArrayInitNotSupported(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError {2246fn failWithArrayInitNotSupported(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError {
...@@ -9038,7 +9049,7 @@ fn analyzeOptionalPayloadPtr(...@@ -9038,7 +9049,7 @@ fn analyzeOptionalPayloadPtr(
90389049
9039 const opt_type = optional_ptr_ty.childType(zcu);9050 const opt_type = optional_ptr_ty.childType(zcu);
9040 if (opt_type.zigTypeTag(zcu) != .Optional) {9051 if (opt_type.zigTypeTag(zcu) != .Optional) {
9041 return sema.fail(block, src, "expected optional type, found '{}'", .{opt_type.fmt(zcu)});9052 return sema.failWithExpectedOptionalType(block, src, opt_type);
9042 }9053 }
90439054
9044 const child_type = opt_type.optionalChild(zcu);9055 const child_type = opt_type.optionalChild(zcu);
test/cases/compile_errors/while_expected_optional_got_error_union.zig+1
...@@ -12,3 +12,4 @@ fn bar() anyerror!i32 {...@@ -12,3 +12,4 @@ fn bar() anyerror!i32 {
12// target=native12// target=native
13//13//
14// :2:15: error: expected optional type, found 'anyerror!i32'14// :2:15: error: expected optional type, found 'anyerror!i32'
15// :2:15: note: consider using 'try', 'catch', or 'if'