Skip to content

Commit 3306e43

Browse files
committed
add compile error test for invalid enum literal implicit cast
See #683
1 parent da9d8a6 commit 3306e43

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/ir.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -11601,8 +11601,10 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1160111601

1160211602
TypeEnumField *field = find_enum_type_field(wanted_type, value->value.data.x_enum_literal);
1160311603
if (field == nullptr) {
11604-
ir_add_error(ira, source_instr, buf_sprintf("enum '%s' has no field named '%s'",
11604+
ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("enum '%s' has no field named '%s'",
1160511605
buf_ptr(&wanted_type->name), buf_ptr(value->value.data.x_enum_literal)));
11606+
add_error_note(ira->codegen, msg, wanted_type->data.enumeration.decl_node,
11607+
buf_sprintf("'%s' declared here", buf_ptr(&wanted_type->name)));
1160611608
return ira->codegen->invalid_instruction;
1160711609
}
1160811610
IrInstruction *result = ir_const(ira, source_instr, wanted_type);

test/compile_errors.zig

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ const tests = @import("tests.zig");
22
const builtin = @import("builtin");
33

44
pub fn addCases(cases: *tests.CompileErrorContext) void {
5+
cases.add(
6+
"cast enum literal to enum but it doesn't match",
7+
\\const Foo = enum {
8+
\\ a,
9+
\\ b,
10+
\\};
11+
\\export fn entry() void {
12+
\\ const x: Foo = .c;
13+
\\}
14+
,
15+
"tmp.zig:6:20: error: enum 'Foo' has no field named 'c'",
16+
"tmp.zig:1:13: note: 'Foo' declared here",
17+
);
18+
519
cases.add(
620
"discarding error value",
721
\\export fn entry() void {

0 commit comments

Comments
 (0)