mirror of
https://github.com/ziglang/zig.git
synced 2026-02-21 16:54:52 +00:00
parent
da9d8a6ecf
commit
3306e43984
@ -11601,8 +11601,10 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
|
||||
|
||||
TypeEnumField *field = find_enum_type_field(wanted_type, value->value.data.x_enum_literal);
|
||||
if (field == nullptr) {
|
||||
ir_add_error(ira, source_instr, buf_sprintf("enum '%s' has no field named '%s'",
|
||||
ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("enum '%s' has no field named '%s'",
|
||||
buf_ptr(&wanted_type->name), buf_ptr(value->value.data.x_enum_literal)));
|
||||
add_error_note(ira->codegen, msg, wanted_type->data.enumeration.decl_node,
|
||||
buf_sprintf("'%s' declared here", buf_ptr(&wanted_type->name)));
|
||||
return ira->codegen->invalid_instruction;
|
||||
}
|
||||
IrInstruction *result = ir_const(ira, source_instr, wanted_type);
|
||||
|
||||
@ -2,6 +2,20 @@ const tests = @import("tests.zig");
|
||||
const builtin = @import("builtin");
|
||||
|
||||
pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||
cases.add(
|
||||
"cast enum literal to enum but it doesn't match",
|
||||
\\const Foo = enum {
|
||||
\\ a,
|
||||
\\ b,
|
||||
\\};
|
||||
\\export fn entry() void {
|
||||
\\ const x: Foo = .c;
|
||||
\\}
|
||||
,
|
||||
"tmp.zig:6:20: error: enum 'Foo' has no field named 'c'",
|
||||
"tmp.zig:1:13: note: 'Foo' declared here",
|
||||
);
|
||||
|
||||
cases.add(
|
||||
"discarding error value",
|
||||
\\export fn entry() void {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user