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...@@ -10560,22 +10560,33 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so
10560 assert(union_field != nullptr);10560 assert(union_field != nullptr);
10561 if ((err = type_resolve(ira->codegen, union_field->type_entry, ResolveStatusZeroBitsKnown)))10561 if ((err = type_resolve(ira->codegen, union_field->type_entry, ResolveStatusZeroBitsKnown)))
10562 return ira->codegen->invalid_instruction;10562 return ira->codegen->invalid_instruction;
10563 if (type_has_bits(union_field->type_entry)) {10563
10564 AstNode *field_node = wanted_type->data.unionation.decl_node->data.container_decl.fields.at(10564 switch (type_has_one_possible_value(ira->codegen, union_field->type_entry)) {
10565 union_field->enum_field->decl_index);10565 case OnePossibleValueInvalid:
10566 ErrorMsg *msg = ir_add_error(ira, source_instr,10566 return ira->codegen->invalid_instruction;
10567 buf_sprintf("cast to union '%s' must initialize '%s' field '%s'",10567 case OnePossibleValueNo: {
10568 buf_ptr(&wanted_type->name),10568 AstNode *field_node = wanted_type->data.unionation.decl_node->data.container_decl.fields.at(
10569 buf_ptr(&union_field->type_entry->name),10569 union_field->enum_field->decl_index);
10570 buf_ptr(union_field->name)));10570 ErrorMsg *msg = ir_add_error(ira, source_instr,
10571 add_error_note(ira->codegen, msg, field_node,10571 buf_sprintf("cast to union '%s' must initialize '%s' field '%s'",
10572 buf_sprintf("field '%s' declared here", buf_ptr(union_field->name)));10572 buf_ptr(&wanted_type->name),
10573 return ira->codegen->invalid_instruction;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;
10574 }10581 }
10582
10575 IrInstruction *result = ir_const(ira, source_instr, wanted_type);10583 IrInstruction *result = ir_const(ira, source_instr, wanted_type);
10576 result->value.special = ConstValSpecialStatic;10584 result->value.special = ConstValSpecialStatic;
10577 result->value.type = wanted_type;10585 result->value.type = wanted_type;
10578 bigint_init_bigint(&result->value.data.x_union.tag, &val->data.x_enum_tag);10586 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;
10579 return result;10590 return result;
10580 }10591 }
1058110592
...@@ -15506,13 +15517,6 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_...@@ -15506,13 +15517,6 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
15506 ConstExprValue *payload_val = union_val->data.x_union.payload;15517 ConstExprValue *payload_val = union_val->data.x_union.payload;
1550715518
15508 ZigType *field_type = field->type_entry;15519 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
15516 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field_type,15520 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field_type,
15517 is_const, is_volatile, PtrLenSingle, 0, 0, 0);15521 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...@@ -18038,6 +18042,12 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
18038 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown)))18042 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown)))
18039 return err;18043 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
18041 ConstExprValue *result = nullptr;18051 ConstExprValue *result = nullptr;
18042 switch (type_entry->id) {18052 switch (type_entry->id) {
18043 case ZigTypeIdInvalid:18053 case ZigTypeIdInvalid:
...@@ -18052,19 +18062,8 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr...@@ -18052,19 +18062,8 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
18052 case ZigTypeIdNull:18062 case ZigTypeIdNull:
18053 case ZigTypeIdArgTuple:18063 case ZigTypeIdArgTuple:
18054 case ZigTypeIdOpaque:18064 case ZigTypeIdOpaque:
18055 *out = nullptr;18065 result = &ira->codegen->const_void_val;
18056 return ErrorNone;18066 break;
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 }
18068 case ZigTypeIdInt:18067 case ZigTypeIdInt:
18069 {18068 {
18070 result = create_const_vals(1);18069 result = create_const_vals(1);
...@@ -18636,7 +18635,6 @@ static IrInstruction *ir_analyze_instruction_type_info(IrAnalyze *ira,...@@ -18636,7 +18635,6 @@ static IrInstruction *ir_analyze_instruction_type_info(IrAnalyze *ira,
18636 out_val->data.x_union.payload = payload;18635 out_val->data.x_union.payload = payload;
1863718636
18638 if (payload != nullptr) {18637 if (payload != nullptr) {
18639 assert(payload->type->id == ZigTypeIdStruct);
18640 payload->parent.id = ConstParentIdUnion;18638 payload->parent.id = ConstParentIdUnion;
18641 payload->parent.data.p_union.union_val = out_val;18639 payload->parent.data.p_union.union_val = out_val;
18642 }18640 }
test/stage1/behavior/type_info.zig+14
...@@ -299,3 +299,17 @@ test "type info: optional field unwrapping" {...@@ -299,3 +299,17 @@ test "type info: optional field unwrapping" {
299299
300 _ = field.offset orelse 0;300 _ = field.offset orelse 0;
301}301}
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}