mirror of
https://github.com/ziglang/zig.git
synced 2026-02-12 20:37:54 +00:00
Sema: @intToEnum error msg includes a "declared here" hint
This commit is contained in:
parent
a62e19ec8e
commit
b67378fb08
20
src/Sema.zig
20
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,
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user