zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit b67378fb08fb3129b66385479a278a93940f09f5 (tree)
parent a62e19ec8ea4afcaf31a27dd32fab195a12a4877
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Wed,  7 Apr 2021 21:04:55 -0700

Sema: `@intToEnum` error msg includes a "declared here" hint

Diffstat:
Msrc/Sema.zig | 20+++++++++++++++++---
Mtest/stage2/cbe.zig | 28++++++++++++++++++++++++++++
2 files changed, 45 insertions(+), 3 deletions(-)

diff --git 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 @@ -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,