authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-08-21 15:00:40-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-08-21 20:47:42-07:00
log2b40815a220bbbd657bfa441e304090f11f1eb4c
tree01ba879103b9f49d58ea90a3244933c58dc34be2
parentf28868e8fd4028cad29bd2de8c0aa1c1713c69dd

stage2: fix wrong value for Decl owns_tv

In the case of a comptime function call of a function that returns a type, resulting in a compiler crash on deinit().

4 files changed, 27 insertions(+), 24 deletions(-)

src/Module.zig-1
......@@ -4037,7 +4037,6 @@ pub fn createAnonymousDeclFromDeclNamed(
40374037 new_decl.ty = typed_value.ty;
40384038 new_decl.val = typed_value.val;
40394039 new_decl.has_tv = true;
4040 new_decl.owns_tv = true;
40414040 new_decl.analysis = .complete;
40424041 new_decl.generation = mod.generation;
40434042
src/Sema.zig+4
......@@ -866,6 +866,7 @@ fn zirStructDecl(
866866 .ty = Type.initTag(.type),
867867 .val = struct_val,
868868 }, type_name);
869 new_decl.owns_tv = true;
869870 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
870871 struct_obj.* = .{
871872 .owner_decl = new_decl,
......@@ -986,6 +987,7 @@ fn zirEnumDecl(
986987 .ty = Type.initTag(.type),
987988 .val = enum_val,
988989 }, type_name);
990 new_decl.owns_tv = true;
989991 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
990992
991993 enum_obj.* = .{
......@@ -1152,6 +1154,7 @@ fn zirUnionDecl(
11521154 .ty = Type.initTag(.type),
11531155 .val = union_val,
11541156 }, type_name);
1157 new_decl.owns_tv = true;
11551158 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
11561159 union_obj.* = .{
11571160 .owner_decl = new_decl,
......@@ -1223,6 +1226,7 @@ fn zirErrorSetDecl(
12231226 .ty = Type.initTag(.type),
12241227 .val = error_set_val,
12251228 }, type_name);
1229 new_decl.owns_tv = true;
12261230 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
12271231 const names = try new_decl_arena.allocator.alloc([]const u8, fields.len);
12281232 for (fields) |str_index, i| {
test/behavior/generics.zig+23-2
......@@ -80,7 +80,7 @@ fn max_f64(a: f64, b: f64) f64 {
8080}
8181
8282test "type constructed by comptime function call" {
83 var l: List(10) = undefined;
83 var l: SimpleList(10) = undefined;
8484 l.array[0] = 10;
8585 l.array[1] = 11;
8686 l.array[2] = 12;
......@@ -90,9 +90,30 @@ test "type constructed by comptime function call" {
9090 try expect(ptr[2] == 12);
9191}
9292
93fn List(comptime L: usize) type {
93fn SimpleList(comptime L: usize) type {
9494 var T = u8;
9595 return struct {
9696 array: [L]T,
9797 };
9898}
99
100test "function with return type type" {
101 var list: List(i32) = undefined;
102 var list2: List(i32) = undefined;
103 list.length = 10;
104 list2.length = 10;
105 try expect(list.prealloc_items.len == 8);
106 try expect(list2.prealloc_items.len == 8);
107}
108
109pub fn List(comptime T: type) type {
110 return SmallList(T, 8);
111}
112
113pub fn SmallList(comptime T: type, comptime STATIC_SIZE: usize) type {
114 return struct {
115 items: []T,
116 length: usize,
117 prealloc_items: [STATIC_SIZE]T,
118 };
119}
test/behavior/generics_stage1.zig-21
......@@ -3,27 +3,6 @@ const testing = std.testing;
33const expect = testing.expect;
44const expectEqual = testing.expectEqual;
55
6pub fn List(comptime T: type) type {
7 return SmallList(T, 8);
8}
9
10pub fn SmallList(comptime T: type, comptime STATIC_SIZE: usize) type {
11 return struct {
12 items: []T,
13 length: usize,
14 prealloc_items: [STATIC_SIZE]T,
15 };
16}
17
18test "function with return type type" {
19 var list: List(i32) = undefined;
20 var list2: List(i32) = undefined;
21 list.length = 10;
22 list2.length = 10;
23 try expect(list.prealloc_items.len == 8);
24 try expect(list2.prealloc_items.len == 8);
25}
26
276test "generic struct" {
287 var a1 = GenNode(i32){
298 .value = 13,