authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-06 12:28:26-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-06 12:28:26-05:00
logd28aa38db71a861fd2036efbb22e57c1d34a5615
tree16d5d36c4a0e38081c8580cfeda6a41267212b76
parente41495de9d56ff269a9aa83ce33407fae1e21053
parentb1895da9b8c41152d146b54e16162de44a08a087
signaturelock-open Commit is signed but in an unrecognized format.

Merge branch 'LemonBoy-fix-3842'

closes #3855 closes #3842

2 files changed, 30 insertions(+), 8 deletions(-)

src/ir.cpp+16-8
...@@ -21786,10 +21786,12 @@ static void ensure_field_index(ZigType *type, const char *field_name, size_t ind...@@ -21786,10 +21786,12 @@ static void ensure_field_index(ZigType *type, const char *field_name, size_t ind
21786 Buf *field_name_buf;21786 Buf *field_name_buf;
2178721787
21788 assert(type != nullptr && !type_is_invalid(type));21788 assert(type != nullptr && !type_is_invalid(type));
21789 // Check for our field by creating a buffer in place then using the comma operator to free it so that we don't21789 field_name_buf = buf_create_from_str(field_name);
21790 // leak memory in debug mode.21790 TypeStructField *field = find_struct_type_field(type, field_name_buf);
21791 assert(find_struct_type_field(type, field_name_buf = buf_create_from_str(field_name))->src_index == index &&21791 buf_deinit(field_name_buf);
21792 (buf_deinit(field_name_buf), true));21792
21793 if (field == nullptr || field->src_index != index)
21794 zig_panic("reference to unknown field %s", field_name);
21793}21795}
2179421796
21795static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, ZigType *root) {21797static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, ZigType *root) {
...@@ -22113,7 +22115,7 @@ static ZigValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_type_ent...@@ -22113,7 +22115,7 @@ static ZigValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_type_ent
22113 zig_unreachable();22115 zig_unreachable();
22114 }22116 }
2211522117
22116 if ((err = type_resolve(ira->codegen, attrs_type->data.pointer.child_type, ResolveStatusAlignmentKnown)))22118 if ((err = type_resolve(ira->codegen, attrs_type->data.pointer.child_type, ResolveStatusSizeKnown)))
22117 return nullptr;22119 return nullptr;
2211822120
22119 ZigType *type_info_pointer_type = ir_type_info_get_type(ira, "Pointer", nullptr);22121 ZigType *type_info_pointer_type = ir_type_info_get_type(ira, "Pointer", nullptr);
...@@ -22162,11 +22164,10 @@ static ZigValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_type_ent...@@ -22162,11 +22164,10 @@ static ZigValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_type_ent
22162 // sentinel: var22164 // sentinel: var
22163 ensure_field_index(result->type, "sentinel", 6);22165 ensure_field_index(result->type, "sentinel", 6);
22164 fields[6]->special = ConstValSpecialStatic;22166 fields[6]->special = ConstValSpecialStatic;
22167 fields[6]->type = get_optional_type(ira->codegen, attrs_type->data.pointer.child_type);
22165 if (attrs_type->data.pointer.sentinel != nullptr) {22168 if (attrs_type->data.pointer.sentinel != nullptr) {
22166 fields[6]->type = get_optional_type(ira->codegen, attrs_type->data.pointer.child_type);
22167 fields[6]->data.x_optional = attrs_type->data.pointer.sentinel;22169 fields[6]->data.x_optional = attrs_type->data.pointer.sentinel;
22168 } else {22170 } else {
22169 fields[6]->type = ira->codegen->builtin_types.entry_null;
22170 fields[6]->data.x_optional = nullptr;22171 fields[6]->data.x_optional = nullptr;
22171 }22172 }
2217222173
...@@ -22814,13 +22815,20 @@ static Error get_const_field_sentinel(IrAnalyze *ira, IrInstruction *source_inst...@@ -22814,13 +22815,20 @@ static Error get_const_field_sentinel(IrAnalyze *ira, IrInstruction *source_inst
22814 ZigValue *field_val = get_const_field(ira, source_instr->source_node, struct_value, name, field_index);22815 ZigValue *field_val = get_const_field(ira, source_instr->source_node, struct_value, name, field_index);
22815 if (field_val == nullptr)22816 if (field_val == nullptr)
22816 return ErrorSemanticAnalyzeFail;22817 return ErrorSemanticAnalyzeFail;
22818
22817 IrInstruction *field_inst = ir_const(ira, source_instr, field_val->type);22819 IrInstruction *field_inst = ir_const(ira, source_instr, field_val->type);
22818 IrInstruction *casted_field_inst = ir_implicit_cast(ira, field_inst,22820 IrInstruction *casted_field_inst = ir_implicit_cast(ira, field_inst,
22819 get_optional_type(ira->codegen, elem_type));22821 get_optional_type(ira->codegen, elem_type));
22820 if (type_is_invalid(casted_field_inst->value->type))22822 if (type_is_invalid(casted_field_inst->value->type))
22821 return ErrorSemanticAnalyzeFail;22823 return ErrorSemanticAnalyzeFail;
2282222824
22823 *result = casted_field_inst->value->data.x_optional;22825 if (optional_value_is_null(casted_field_inst->value)) {
22826 *result = nullptr;
22827 } else {
22828 assert(type_has_optional_repr(casted_field_inst->value->type));
22829 *result = casted_field_inst->value->data.x_optional;
22830 }
22831
22824 return ErrorNone;22832 return ErrorNone;
22825}22833}
2282622834
test/stage1/behavior/type.zig+14
...@@ -130,3 +130,17 @@ test "Type.Undefined" {...@@ -130,3 +130,17 @@ test "Type.Undefined" {
130test "Type.Null" {130test "Type.Null" {
131 testTypes(&[_]type{@typeOf(null)});131 testTypes(&[_]type{@typeOf(null)});
132}132}
133test "@Type create slice with null sentinel" {
134 const Slice = @Type(builtin.TypeInfo{
135 .Pointer = .{
136 .size = .Slice,
137 .is_const = true,
138 .is_volatile = false,
139 .is_allowzero = false,
140 .alignment = 8,
141 .child = *i32,
142 .sentinel = null,
143 },
144 });
145 testing.expect(Slice == []align(8) const *i32);
146}