Commit 02d69f5009

Andrew Kelley <andrew@ziglang.org>
2022-03-31 02:20:12
Sema: fix usingnamespace decl Value in wrong arena
closes #11297
1 parent 5d5b1b6
Changed files (2)
src/Module.zig
@@ -3909,19 +3909,18 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
     const decl_arena_state = try decl_arena_allocator.create(std.heap.ArenaAllocator.State);
 
     if (decl.is_usingnamespace) {
-        const ty_ty = Type.initTag(.type);
-        if (!decl_tv.ty.eql(ty_ty, target)) {
+        if (!decl_tv.ty.eql(Type.type, target)) {
             return sema.fail(&block_scope, src, "expected type, found {}", .{
                 decl_tv.ty.fmt(target),
             });
         }
         var buffer: Value.ToTypeBuffer = undefined;
-        const ty = decl_tv.val.toType(&buffer);
+        const ty = try decl_tv.val.toType(&buffer).copy(decl_arena_allocator);
         if (ty.getNamespace() == null) {
             return sema.fail(&block_scope, src, "type {} has no namespace", .{ty.fmt(target)});
         }
 
-        decl.ty = ty_ty;
+        decl.ty = Type.type;
         decl.val = try Value.Tag.ty.create(decl_arena_allocator, ty);
         decl.@"align" = 0;
         decl.@"linksection" = null;
test/behavior/usingnamespace.zig
@@ -56,3 +56,23 @@ test "two files usingnamespace import each other" {
 
     try expect(@This().ok());
 }
+
+test {
+    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
+    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+
+    const AA = struct {
+        x: i32,
+        fn b(x: i32) @This() {
+            return .{ .x = x };
+        }
+        fn c() type {
+            return if (true) struct {
+                const expected: i32 = 42;
+            } else struct {};
+        }
+        usingnamespace c();
+    };
+    const a = AA.b(42);
+    try expect(a.x == AA.c().expected);
+}