authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-30 17:20:12-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-30 17:24:01-07:00
log02d69f50092ee9ecfa552ff94d20fde62edd7adc
treee2e5537b8aede2f407863a15834641226631620e
parent5d5b1b68fc1b3e1f5a8c7871c9f31716c89eb062

Sema: fix usingnamespace decl Value in wrong arena

closes #11297

2 files changed, 23 insertions(+), 4 deletions(-)

src/Module.zig+3-4
......@@ -3909,19 +3909,18 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
39093909 const decl_arena_state = try decl_arena_allocator.create(std.heap.ArenaAllocator.State);
39103910
39113911 if (decl.is_usingnamespace) {
3912 const ty_ty = Type.initTag(.type);
3913 if (!decl_tv.ty.eql(ty_ty, target)) {
3912 if (!decl_tv.ty.eql(Type.type, target)) {
39143913 return sema.fail(&block_scope, src, "expected type, found {}", .{
39153914 decl_tv.ty.fmt(target),
39163915 });
39173916 }
39183917 var buffer: Value.ToTypeBuffer = undefined;
3919 const ty = decl_tv.val.toType(&buffer);
3918 const ty = try decl_tv.val.toType(&buffer).copy(decl_arena_allocator);
39203919 if (ty.getNamespace() == null) {
39213920 return sema.fail(&block_scope, src, "type {} has no namespace", .{ty.fmt(target)});
39223921 }
39233922
3924 decl.ty = ty_ty;
3923 decl.ty = Type.type;
39253924 decl.val = try Value.Tag.ty.create(decl_arena_allocator, ty);
39263925 decl.@"align" = 0;
39273926 decl.@"linksection" = null;
test/behavior/usingnamespace.zig+20
......@@ -56,3 +56,23 @@ test "two files usingnamespace import each other" {
5656
5757 try expect(@This().ok());
5858}
59
60test {
61 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
62 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
63
64 const AA = struct {
65 x: i32,
66 fn b(x: i32) @This() {
67 return .{ .x = x };
68 }
69 fn c() type {
70 return if (true) struct {
71 const expected: i32 = 42;
72 } else struct {};
73 }
74 usingnamespace c();
75 };
76 const a = AA.b(42);
77 try expect(a.x == AA.c().expected);
78}