authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-24 18:51:24-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-24 18:51:24-04:00
log3306e43984a8b17472ecc4b13a2f2815d6630eef
tree02a0a482a9887c5d439d0e2ec3e442da58a3f89c
parentda9d8a6ecfb0428ce3d6575905f96fa1661c9b80
signaturelock-open Commit is signed but in an unrecognized format.

add compile error test for invalid enum literal implicit cast

See #683

2 files changed, 17 insertions(+), 1 deletions(-)

src/ir.cpp+3-1
...@@ -11601,8 +11601,10 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -11601,8 +11601,10 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1160111601
11602 TypeEnumField *field = find_enum_type_field(wanted_type, value->value.data.x_enum_literal);11602 TypeEnumField *field = find_enum_type_field(wanted_type, value->value.data.x_enum_literal);
11603 if (field == nullptr) {11603 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'",
11605 buf_ptr(&wanted_type->name), buf_ptr(value->value.data.x_enum_literal)));11605 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)));
11606 return ira->codegen->invalid_instruction;11608 return ira->codegen->invalid_instruction;
11607 }11609 }
11608 IrInstruction *result = ir_const(ira, source_instr, wanted_type);11610 IrInstruction *result = ir_const(ira, source_instr, wanted_type);
test/compile_errors.zig+14
...@@ -2,6 +2,20 @@ const tests = @import("tests.zig");...@@ -2,6 +2,20 @@ const tests = @import("tests.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub 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
5 cases.add(19 cases.add(
6 "discarding error value",20 "discarding error value",
7 \\export fn entry() void {21 \\export fn entry() void {