authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-07 21:04:55-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-07 21:04:55-07:00
logb67378fb08fb3129b66385479a278a93940f09f5
tree337925698e538c1ea5646e0f75175844fc232dca
parenta62e19ec8ea4afcaf31a27dd32fab195a12a4877

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


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

src/Sema.zig+17-3
...@@ -1983,9 +1983,23 @@ fn zirIntToEnum(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErr...@@ -1983,9 +1983,23 @@ fn zirIntToEnum(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErr
19831983
1984 if (try sema.resolveDefinedValue(block, operand_src, operand)) |int_val| {1984 if (try sema.resolveDefinedValue(block, operand_src, operand)) |int_val| {
1985 if (!dest_ty.enumHasInt(int_val, target)) {1985 if (!dest_ty.enumHasInt(int_val, target)) {
1986 return mod.fail(&block.base, src, "enum '{}' has no tag with value {}", .{1986 const msg = msg: {
1987 dest_ty, int_val,1987 const msg = try mod.errMsg(
1988 });1988 &block.base,
1989 src,
1990 "enum '{}' has no tag with value {}",
1991 .{ dest_ty, int_val },
1992 );
1993 errdefer msg.destroy(sema.gpa);
1994 try mod.errNoteNonLazy(
1995 dest_ty.declSrcLoc(),
1996 msg,
1997 "enum declared here",
1998 .{},
1999 );
2000 break :msg msg;
2001 };
2002 return mod.failWithOwnedErrorMsg(&block.base, msg);
1989 }2003 }
1990 return mod.constInst(arena, src, .{2004 return mod.constInst(arena, src, .{
1991 .ty = dest_ty,2005 .ty = dest_ty,
test/stage2/cbe.zig+28
...@@ -674,6 +674,34 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -674,6 +674,34 @@ pub fn addCases(ctx: *TestContext) !void {
674 ":1:28: error: duplicate enum tag",674 ":1:28: error: duplicate enum tag",
675 ":1:22: note: other tag here",675 ":1:22: note: other tag here",
676 });676 });
677
678 case.addError(
679 \\export fn foo() void {
680 \\ const a = true;
681 \\ const b = @enumToInt(a);
682 \\}
683 , &.{
684 ":3:26: error: expected enum or tagged union, found bool",
685 });
686
687 case.addError(
688 \\export fn foo() void {
689 \\ const a = 1;
690 \\ const b = @intToEnum(bool, a);
691 \\}
692 , &.{
693 ":3:26: error: expected enum, found bool",
694 });
695
696 case.addError(
697 \\const E = enum { a, b, c };
698 \\export fn foo() void {
699 \\ const b = @intToEnum(E, 3);
700 \\}
701 , &.{
702 ":3:15: error: enum 'E' has no tag with value 3",
703 ":1:11: note: enum declared here",
704 });
677 }705 }
678706
679 ctx.c("empty start function", linux_x64,707 ctx.c("empty start function", linux_x64,