zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 02d69f50092ee9ecfa552ff94d20fde62edd7adc (tree)
parent 5d5b1b68fc1b3e1f5a8c7871c9f31716c89eb062
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Wed, 30 Mar 2022 17:20:12 -0700

Sema: fix usingnamespace decl Value in wrong arena

closes #11297

Diffstat:
Msrc/Module.zig | 7+++----
Mtest/behavior/usingnamespace.zig | 20++++++++++++++++++++
2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/src/Module.zig b/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; diff --git a/test/behavior/usingnamespace.zig b/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); +}