From 400006bbe790f2173fd6e40d80608691a95b437e Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Tue, 19 Feb 2019 21:18:57 +0100 Subject: [PATCH] Prevent crash in tagged enums rendering (#1986) * Prevent crash in tagged enums rendering * Add a test case --- src/analyze.cpp | 4 ++-- test/compile_errors.zig | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index b6d54c0da8..9fe656ff77 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -6276,8 +6276,8 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) { } case ZigTypeIdUnion: { - uint64_t tag = bigint_as_unsigned(&const_val->data.x_union.tag); - TypeUnionField *field = &type_entry->data.unionation.fields[tag]; + const BigInt *tag = &const_val->data.x_union.tag; + TypeUnionField *field = find_union_field_by_tag(type_entry, tag); buf_appendf(buf, "%s { .%s = ", buf_ptr(&type_entry->name), buf_ptr(field->name)); render_const_value(g, buf, const_val->data.x_union.payload); buf_append_str(buf, "}"); diff --git a/test/compile_errors.zig b/test/compile_errors.zig index e9fe6f589c..b935b4825a 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -5599,4 +5599,21 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { , ".tmp_source.zig:2:26: error: vector element type must be integer, float, or pointer; '@Vector(4, u8)' is invalid", ); + + cases.add( + "compileLog of tagged enum doesn't crash the compiler", + \\const Bar = union(enum(u32)) { + \\ X: i32 = 1 + \\}; + \\ + \\fn testCompileLog(x: Bar) void { + \\ @compileLog(x); + \\} + \\ + \\pub fn main () void { + \\ comptime testCompileLog(Bar{.X = 123}); + \\} + , + ".tmp_source.zig:6:5: error: found compile log statement" + ); }