From 19961c50e4db10fc4ada428928a7f5d1a2966da6 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 27 Jun 2018 13:15:55 -0400 Subject: [PATCH] fix comptime @tagName crashing sometimes closes #1118 --- src/analyze.cpp | 1 + src/ir.cpp | 3 +++ test/cases/eval.zig | 5 +++++ test/cases/widening.zig | 9 +++++---- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index 25cc1c79d0..d5e69de1eb 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -3728,6 +3728,7 @@ TypeUnionField *find_union_field_by_tag(TypeTableEntry *type_entry, const BigInt } TypeEnumField *find_enum_field_by_tag(TypeTableEntry *enum_type, const BigInt *tag) { + assert(enum_type->data.enumeration.zero_bits_known); for (uint32_t i = 0; i < enum_type->data.enumeration.src_field_count; i += 1) { TypeEnumField *field = &enum_type->data.enumeration.fields[i]; if (bigint_cmp(&field->value, tag) == CmpEQ) { diff --git a/src/ir.cpp b/src/ir.cpp index 6e424980f8..9ba01d1411 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -16053,6 +16053,9 @@ static TypeTableEntry *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrIn assert(target->value.type->id == TypeTableEntryIdEnum); if (instr_is_comptime(target)) { + type_ensure_zero_bits_known(ira->codegen, target->value.type); + if (type_is_invalid(target->value.type)) + return ira->codegen->builtin_types.entry_invalid; TypeEnumField *field = find_enum_field_by_tag(target->value.type, &target->value.data.x_bigint); ConstExprValue *array_val = create_const_str_lit(ira->codegen, field->name); ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base); diff --git a/test/cases/eval.zig b/test/cases/eval.zig index 756ffe339a..83d2e80176 100644 --- a/test/cases/eval.zig +++ b/test/cases/eval.zig @@ -637,3 +637,8 @@ test "call method with comptime pass-by-non-copying-value self parameter" { var b = s.b(); assert(b == 2); } + +test "@tagName of @typeId" { + const str = @tagName(@typeId(u8)); + assert(std.mem.eql(u8, str, "Int")); +} diff --git a/test/cases/widening.zig b/test/cases/widening.zig index 18c12806d3..cf6ab4ca0f 100644 --- a/test/cases/widening.zig +++ b/test/cases/widening.zig @@ -19,8 +19,9 @@ test "implicit unsigned integer to signed integer" { } test "float widening" { - var a: f32 = 12.34; - var b: f64 = a; - var c: f128 = b; - assert(c == a); + var a: f16 = 12.34; + var b: f32 = a; + var c: f64 = b; + var d: f128 = c; + assert(d == a); }