authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-11-08 18:25:59+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-11 19:14:03-07:00
logaa6fc29744de4008e97faf0f54ccb52ebbdf5ca2
tree1ac1a1a0c2630c57f0cb54115e29ba96fff22677
parent01a927a0cbc068a2174124ec6fe224200849dd3e

stage1: Fix crash in comptime struct generation

Using the gen_index rather than the src_index is needed to handle structures containing zero-sized or comptime only types. Closes #7027

3 files changed, 24 insertions(+), 3 deletions(-)

src/stage1/codegen.cpp+6-3
......@@ -6878,9 +6878,12 @@ static LLVMValueRef gen_parent_ptr(CodeGen *g, ZigValue *val, ConstParent *paren
68786878 render_const_val(g, val, "");
68796879 render_const_val_global(g, val, "");
68806880 return val->llvm_global;
6881 case ConstParentIdStruct:
6882 return gen_const_ptr_struct_recursive(g, parent->data.p_struct.struct_val,
6883 parent->data.p_struct.field_index);
6881 case ConstParentIdStruct: {
6882 ZigValue *struct_val = parent->data.p_struct.struct_val;
6883 size_t src_field_index = parent->data.p_struct.field_index;
6884 size_t gen_field_index = struct_val->type->data.structure.fields[src_field_index]->gen_index;
6885 return gen_const_ptr_struct_recursive(g, struct_val, gen_field_index);
6886 }
68846887 case ConstParentIdErrUnionCode:
68856888 return gen_const_ptr_err_union_code_recursive(g, parent->data.p_err_union_code.err_union_val);
68866889 case ConstParentIdErrUnionPayload:
test/stage1/behavior.zig+1
......@@ -56,6 +56,7 @@ comptime {
5656 _ = @import("behavior/bugs/6456.zig");
5757 _ = @import("behavior/bugs/6781.zig");
5858 _ = @import("behavior/bugs/6850.zig");
59 _ = @import("behavior/bugs/7027.zig");
5960 _ = @import("behavior/bugs/7047.zig");
6061 _ = @import("behavior/bugs/394.zig");
6162 _ = @import("behavior/bugs/421.zig");
test/stage1/behavior/bugs/7027.zig created+17
......@@ -0,0 +1,17 @@
1const Foobar = struct {
2 myTypes: [128]type,
3 str: [1024]u8,
4
5 fn foo() @This() {
6 comptime var foobar: Foobar = undefined;
7 foobar.str = [_]u8{'a'} ** 1024;
8 return foobar;
9 }
10};
11
12fn foo(arg: anytype) void {}
13
14test "" {
15 comptime var foobar = Foobar.foo();
16 foo(foobar.str[0..10]);
17}