diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-04-07 21:04:55 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-04-07 21:04:55 -0700 |
| commit | b67378fb08fb3129b66385479a278a93940f09f5 (patch) | |
| tree | 337925698e538c1ea5646e0f75175844fc232dca | |
| parent | a62e19ec8ea4afcaf31a27dd32fab195a12a4877 (diff) | |
| download | zig-b67378fb08fb3129b66385479a278a93940f09f5.tar.gz zig-b67378fb08fb3129b66385479a278a93940f09f5.zip | |
Sema: `@intToEnum` error msg includes a "declared here" hint
| -rw-r--r-- | src/Sema.zig | 20 | ||||
| -rw-r--r-- | test/stage2/cbe.zig | 28 |
2 files changed, 45 insertions, 3 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 655fc4bd06..134e569948 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -1983,9 +1983,23 @@ fn zirIntToEnum(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErr if (try sema.resolveDefinedValue(block, operand_src, operand)) |int_val| { if (!dest_ty.enumHasInt(int_val, target)) { - return mod.fail(&block.base, src, "enum '{}' has no tag with value {}", .{ - dest_ty, int_val, - }); + const msg = msg: { + const msg = try mod.errMsg( + &block.base, + src, + "enum '{}' has no tag with value {}", + .{ dest_ty, int_val }, + ); + errdefer msg.destroy(sema.gpa); + try mod.errNoteNonLazy( + dest_ty.declSrcLoc(), + msg, + "enum declared here", + .{}, + ); + break :msg msg; + }; + return mod.failWithOwnedErrorMsg(&block.base, msg); } return mod.constInst(arena, src, .{ .ty = dest_ty, diff --git a/test/stage2/cbe.zig b/test/stage2/cbe.zig index 49a0ca686b..b0386f6246 100644 --- a/test/stage2/cbe.zig +++ b/test/stage2/cbe.zig @@ -674,6 +674,34 @@ pub fn addCases(ctx: *TestContext) !void { ":1:28: error: duplicate enum tag", ":1:22: note: other tag here", }); + + case.addError( + \\export fn foo() void { + \\ const a = true; + \\ const b = @enumToInt(a); + \\} + , &.{ + ":3:26: error: expected enum or tagged union, found bool", + }); + + case.addError( + \\export fn foo() void { + \\ const a = 1; + \\ const b = @intToEnum(bool, a); + \\} + , &.{ + ":3:26: error: expected enum, found bool", + }); + + case.addError( + \\const E = enum { a, b, c }; + \\export fn foo() void { + \\ const b = @intToEnum(E, 3); + \\} + , &.{ + ":3:15: error: enum 'E' has no tag with value 3", + ":1:11: note: enum declared here", + }); } ctx.c("empty start function", linux_x64, |
