authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-11 11:09:55-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-11 11:09:55-04:00
logb4e6e301e1a5411d09a306fb96cf4a5c8d0b754f
tree3ba6ab7e80295a12cee83117ba6b772c3829aaee
parent83b79522d2c555d7bce8640f31b9ba20658cb021
parent1d5e349a52de9f4d056d50a1e41b89e7b0184a05
signaturelock-open Commit is signed but in an unrecognized format.

Merge branch 'fix-1600'

closes #2036

2 files changed, 44 insertions(+), 32 deletions(-)

src/ir.cpp+30-32
......@@ -10560,22 +10560,33 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so
1056010560 assert(union_field != nullptr);
1056110561 if ((err = type_resolve(ira->codegen, union_field->type_entry, ResolveStatusZeroBitsKnown)))
1056210562 return ira->codegen->invalid_instruction;
10563 if (type_has_bits(union_field->type_entry)) {
10564 AstNode *field_node = wanted_type->data.unionation.decl_node->data.container_decl.fields.at(
10565 union_field->enum_field->decl_index);
10566 ErrorMsg *msg = ir_add_error(ira, source_instr,
10567 buf_sprintf("cast to union '%s' must initialize '%s' field '%s'",
10568 buf_ptr(&wanted_type->name),
10569 buf_ptr(&union_field->type_entry->name),
10570 buf_ptr(union_field->name)));
10571 add_error_note(ira->codegen, msg, field_node,
10572 buf_sprintf("field '%s' declared here", buf_ptr(union_field->name)));
10573 return ira->codegen->invalid_instruction;
10563
10564 switch (type_has_one_possible_value(ira->codegen, union_field->type_entry)) {
10565 case OnePossibleValueInvalid:
10566 return ira->codegen->invalid_instruction;
10567 case OnePossibleValueNo: {
10568 AstNode *field_node = wanted_type->data.unionation.decl_node->data.container_decl.fields.at(
10569 union_field->enum_field->decl_index);
10570 ErrorMsg *msg = ir_add_error(ira, source_instr,
10571 buf_sprintf("cast to union '%s' must initialize '%s' field '%s'",
10572 buf_ptr(&wanted_type->name),
10573 buf_ptr(&union_field->type_entry->name),
10574 buf_ptr(union_field->name)));
10575 add_error_note(ira->codegen, msg, field_node,
10576 buf_sprintf("field '%s' declared here", buf_ptr(union_field->name)));
10577 return ira->codegen->invalid_instruction;
10578 }
10579 case OnePossibleValueYes:
10580 break;
1057410581 }
10582
1057510583 IrInstruction *result = ir_const(ira, source_instr, wanted_type);
1057610584 result->value.special = ConstValSpecialStatic;
1057710585 result->value.type = wanted_type;
1057810586 bigint_init_bigint(&result->value.data.x_union.tag, &val->data.x_enum_tag);
10587 result->value.data.x_union.payload = create_const_vals(1);
10588 result->value.data.x_union.payload->special = ConstValSpecialStatic;
10589 result->value.data.x_union.payload->type = union_field->type_entry;
1057910590 return result;
1058010591 }
1058110592
......@@ -15506,13 +15517,6 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1550615517 ConstExprValue *payload_val = union_val->data.x_union.payload;
1550715518
1550815519 ZigType *field_type = field->type_entry;
15509 if (field_type->id == ZigTypeIdVoid) {
15510 assert(payload_val == nullptr);
15511 payload_val = create_const_vals(1);
15512 payload_val->special = ConstValSpecialStatic;
15513 payload_val->type = field_type;
15514 }
15515
1551615520 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field_type,
1551715521 is_const, is_volatile, PtrLenSingle, 0, 0, 0);
1551815522
......@@ -18038,6 +18042,12 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1803818042 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown)))
1803918043 return err;
1804018044
18045 auto entry = ira->codegen->type_info_cache.maybe_get(type_entry);
18046 if (entry != nullptr) {
18047 *out = entry->value;
18048 return ErrorNone;
18049 }
18050
1804118051 ConstExprValue *result = nullptr;
1804218052 switch (type_entry->id) {
1804318053 case ZigTypeIdInvalid:
......@@ -18052,19 +18062,8 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1805218062 case ZigTypeIdNull:
1805318063 case ZigTypeIdArgTuple:
1805418064 case ZigTypeIdOpaque:
18055 *out = nullptr;
18056 return ErrorNone;
18057 default:
18058 {
18059 // Lookup an available value in our cache.
18060 auto entry = ira->codegen->type_info_cache.maybe_get(type_entry);
18061 if (entry != nullptr) {
18062 *out = entry->value;
18063 return ErrorNone;
18064 }
18065
18066 // Fallthrough if we don't find one.
18067 }
18065 result = &ira->codegen->const_void_val;
18066 break;
1806818067 case ZigTypeIdInt:
1806918068 {
1807018069 result = create_const_vals(1);
......@@ -18636,7 +18635,6 @@ static IrInstruction *ir_analyze_instruction_type_info(IrAnalyze *ira,
1863618635 out_val->data.x_union.payload = payload;
1863718636
1863818637 if (payload != nullptr) {
18639 assert(payload->type->id == ZigTypeIdStruct);
1864018638 payload->parent.id = ConstParentIdUnion;
1864118639 payload->parent.data.p_union.union_val = out_val;
1864218640 }
test/stage1/behavior/type_info.zig+14
......@@ -299,3 +299,17 @@ test "type info: optional field unwrapping" {
299299
300300 _ = field.offset orelse 0;
301301}
302
303test "type info: pass to function" {
304 _ = passTypeInfo(@typeInfo(void));
305 _ = comptime passTypeInfo(@typeInfo(void));
306}
307
308fn passTypeInfo(comptime info: TypeInfo) type {
309 return void;
310}
311
312test "type info: TypeId -> TypeInfo impl cast" {
313 _ = passTypeInfo(TypeId.Void);
314 _ = comptime passTypeInfo(TypeId.Void);
315}