authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-11-29 12:39:15+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-29 10:39:10-08:00
logdf99cfdf1ea6f3c0ec7245c771d42d988313a07a
treeba163366da1ec98802ca03bacd41430db75ecbf7
parentc80d196094de3e0258b4a1146cff56576ec8e95d

stage1: Fix typeInfo generation for arrays w/o sentinel

ZigTypeIdOptional types have a different way of specifying their payload value depending on whether the child type is a pointer or not (plus some other special cases). Fixes #7251

2 files changed, 25 insertions(+), 13 deletions(-)

src/stage1/ir.cpp+10-6
......@@ -25264,12 +25264,12 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
2526425264 0, 0, 0, false);
2526525265 fn_decl_fields[5]->type = get_optional_type(ira->codegen, get_slice_type(ira->codegen, u8_ptr));
2526625266 if (fn_node->is_extern && fn_node->lib_name != nullptr && buf_len(fn_node->lib_name) > 0) {
25267 fn_decl_fields[5]->data.x_optional = ira->codegen->pass1_arena->create<ZigValue>();
25267 ZigValue *slice_val = ira->codegen->pass1_arena->create<ZigValue>();
2526825268 ZigValue *lib_name = create_const_str_lit(ira->codegen, fn_node->lib_name)->data.x_ptr.data.ref.pointee;
25269 init_const_slice(ira->codegen, fn_decl_fields[5]->data.x_optional, lib_name, 0,
25270 buf_len(fn_node->lib_name), true);
25269 init_const_slice(ira->codegen, slice_val, lib_name, 0, buf_len(fn_node->lib_name), true);
25270 set_optional_payload(fn_decl_fields[5], slice_val);
2527125271 } else {
25272 fn_decl_fields[5]->data.x_optional = nullptr;
25272 set_optional_payload(fn_decl_fields[5], nullptr);
2527325273 }
2527425274 // return_type: type
2527525275 ensure_field_index(fn_decl_val->type, "return_type", 6);
......@@ -25549,8 +25549,12 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
2554925549 fields[1]->data.x_type = type_entry->data.array.child_type;
2555025550 // sentinel: anytype
2555125551 fields[2]->special = ConstValSpecialStatic;
25552 fields[2]->type = get_optional_type(ira->codegen, type_entry->data.array.child_type);
25553 fields[2]->data.x_optional = type_entry->data.array.sentinel;
25552 if (type_entry->data.array.child_type != nullptr) {
25553 fields[2]->type = get_optional_type(ira->codegen, type_entry->data.array.child_type);
25554 set_optional_payload(fields[2], type_entry->data.array.sentinel);
25555 } else {
25556 fields[2]->type = ira->codegen->builtin_types.entry_null;
25557 }
2555425558 break;
2555525559 }
2555625560 case ZigTypeIdVector: {
test/stage1/behavior/type_info.zig+15-7
......@@ -82,9 +82,6 @@ fn testNullTerminatedPtr() void {
8282 expect(ptr_info.Pointer.sentinel.? == 0);
8383
8484 expect(@typeInfo([:0]u8).Pointer.sentinel != null);
85 expect(@typeInfo([10:0]u8).Array.sentinel != null);
86 expect(@typeInfo([10:0]u8).Array.len == 10);
87 expect(@sizeOf([10:0]u8) == 11);
8885}
8986
9087test "type info: C pointer type info" {
......@@ -123,10 +120,21 @@ test "type info: array type info" {
123120}
124121
125122fn testArray() void {
126 const arr_info = @typeInfo([42]bool);
127 expect(arr_info == .Array);
128 expect(arr_info.Array.len == 42);
129 expect(arr_info.Array.child == bool);
123 {
124 const info = @typeInfo([42]u8);
125 expect(info == .Array);
126 expect(info.Array.len == 42);
127 expect(info.Array.child == u8);
128 expect(info.Array.sentinel == null);
129 }
130
131 {
132 const info = @typeInfo([10:0]u8);
133 expect(info.Array.len == 10);
134 expect(info.Array.child == u8);
135 expect(info.Array.sentinel.? == @as(u8, 0));
136 expect(@sizeOf([10:0]u8) == info.Array.len + 1);
137 }
130138}
131139
132140test "type info: optional type info" {