fix comptime @tagName crashing sometimes

closes #1118
This commit is contained in:
Andrew Kelley 2018-06-27 13:15:55 -04:00
parent 6f88ecc9b6
commit 19961c50e4
4 changed files with 14 additions and 4 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -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"));
}

View File

@ -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);
}