Commit 2b40815a22

Andrew Kelley <andrew@ziglang.org>
2021-08-22 00:00:40
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().
1 parent f28868e
Changed files (4)
src/Module.zig
@@ -4037,7 +4037,6 @@ pub fn createAnonymousDeclFromDeclNamed(
     new_decl.ty = typed_value.ty;
     new_decl.val = typed_value.val;
     new_decl.has_tv = true;
-    new_decl.owns_tv = true;
     new_decl.analysis = .complete;
     new_decl.generation = mod.generation;
 
src/Sema.zig
@@ -866,6 +866,7 @@ fn zirStructDecl(
         .ty = Type.initTag(.type),
         .val = struct_val,
     }, type_name);
+    new_decl.owns_tv = true;
     errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
     struct_obj.* = .{
         .owner_decl = new_decl,
@@ -986,6 +987,7 @@ fn zirEnumDecl(
         .ty = Type.initTag(.type),
         .val = enum_val,
     }, type_name);
+    new_decl.owns_tv = true;
     errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
 
     enum_obj.* = .{
@@ -1152,6 +1154,7 @@ fn zirUnionDecl(
         .ty = Type.initTag(.type),
         .val = union_val,
     }, type_name);
+    new_decl.owns_tv = true;
     errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
     union_obj.* = .{
         .owner_decl = new_decl,
@@ -1223,6 +1226,7 @@ fn zirErrorSetDecl(
         .ty = Type.initTag(.type),
         .val = error_set_val,
     }, type_name);
+    new_decl.owns_tv = true;
     errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
     const names = try new_decl_arena.allocator.alloc([]const u8, fields.len);
     for (fields) |str_index, i| {
test/behavior/generics.zig
@@ -80,7 +80,7 @@ fn max_f64(a: f64, b: f64) f64 {
 }
 
 test "type constructed by comptime function call" {
-    var l: List(10) = undefined;
+    var l: SimpleList(10) = undefined;
     l.array[0] = 10;
     l.array[1] = 11;
     l.array[2] = 12;
@@ -90,9 +90,30 @@ test "type constructed by comptime function call" {
     try expect(ptr[2] == 12);
 }
 
-fn List(comptime L: usize) type {
+fn SimpleList(comptime L: usize) type {
     var T = u8;
     return struct {
         array: [L]T,
     };
 }
+
+test "function with return type type" {
+    var list: List(i32) = undefined;
+    var list2: List(i32) = undefined;
+    list.length = 10;
+    list2.length = 10;
+    try expect(list.prealloc_items.len == 8);
+    try expect(list2.prealloc_items.len == 8);
+}
+
+pub fn List(comptime T: type) type {
+    return SmallList(T, 8);
+}
+
+pub fn SmallList(comptime T: type, comptime STATIC_SIZE: usize) type {
+    return struct {
+        items: []T,
+        length: usize,
+        prealloc_items: [STATIC_SIZE]T,
+    };
+}
test/behavior/generics_stage1.zig
@@ -3,27 +3,6 @@ const testing = std.testing;
 const expect = testing.expect;
 const expectEqual = testing.expectEqual;
 
-pub fn List(comptime T: type) type {
-    return SmallList(T, 8);
-}
-
-pub fn SmallList(comptime T: type, comptime STATIC_SIZE: usize) type {
-    return struct {
-        items: []T,
-        length: usize,
-        prealloc_items: [STATIC_SIZE]T,
-    };
-}
-
-test "function with return type type" {
-    var list: List(i32) = undefined;
-    var list2: List(i32) = undefined;
-    list.length = 10;
-    list2.length = 10;
-    try expect(list.prealloc_items.len == 8);
-    try expect(list2.prealloc_items.len == 8);
-}
-
 test "generic struct" {
     var a1 = GenNode(i32){
         .value = 13,