zig

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

commit 01081cc8e8b79104f7992d60dbd1bc8682e8fedf (tree)
parent 5ee2816f6226eff8990749d44b1b9003b10c6170
Author: mlugg <mlugg@mlugg.co.uk>
Date:   Fri, 27 Dec 2024 21:21:11 +0000

AstGen: lower function addrspace expression correctly

Also, add some basic behavior tests for addrspace and linksection which
would have caught this bug in CI.

Diffstat:
Mlib/std/zig/AstGen.zig | 2+-
Mtest/behavior.zig | 2+-
Atest/behavior/addrspace_and_linksection.zig | 120+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dtest/behavior/type_info_mul_linksection_addrspace_decls.zig | 12------------
4 files changed, 122 insertions(+), 14 deletions(-)

diff --git a/lib/std/zig/AstGen.zig b/lib/std/zig/AstGen.zig @@ -4228,7 +4228,7 @@ fn fnDecl( if (fn_proto.ast.addrspace_expr != 0) { astgen.restoreSourceCursor(saved_cursor); const addrspace_ty = try addrspace_gz.addBuiltinValue(fn_proto.ast.addrspace_expr, .address_space); - const inst = try expr(&addrspace_gz, &addrspace_gz.base, .{ .rl = .{ .coerced_ty = addrspace_ty } }, fn_proto.ast.section_expr); + const inst = try expr(&addrspace_gz, &addrspace_gz.base, .{ .rl = .{ .coerced_ty = addrspace_ty } }, fn_proto.ast.addrspace_expr); _ = try addrspace_gz.addBreakWithSrcNode(.break_inline, decl_inst, inst, decl_node); } diff --git a/test/behavior.zig b/test/behavior.zig @@ -1,6 +1,7 @@ const builtin = @import("builtin"); test { + _ = @import("behavior/addrspace_and_linksection.zig"); _ = @import("behavior/align.zig"); _ = @import("behavior/alignof.zig"); _ = @import("behavior/array.zig"); @@ -100,7 +101,6 @@ test { _ = @import("behavior/tuple_declarations.zig"); _ = @import("behavior/type.zig"); _ = @import("behavior/type_info.zig"); - _ = @import("behavior/type_info_mul_linksection_addrspace_decls.zig"); _ = @import("behavior/typename.zig"); _ = @import("behavior/undefined.zig"); _ = @import("behavior/underscore.zig"); diff --git a/test/behavior/addrspace_and_linksection.zig b/test/behavior/addrspace_and_linksection.zig @@ -0,0 +1,120 @@ +const builtin = @import("builtin"); + +/// Elements are const_linksection, var_linksection, fn_linksection +const linksections: ?[3][]const u8 = switch (builtin.target.ofmt) { + .elf => .{ ".rodata", ".data", ".text" }, + .coff => .{ ".rdata", ".data", ".text" }, + .macho => .{ "__TEXT,__const", "__DATA,__data", "__TEXT,__text" }, + else => null, +}; +const const_linksection = linksections.?[0]; +const var_linksection = linksections.?[1]; +const fn_linksection = linksections.?[2]; + +test "addrspace on container-level const" { + const S = struct { + const a: u32 addrspace(.generic) = 123; + fn check(ptr: anytype) !void { + if (ptr.* != 123) return error.TestFailed; + } + }; + try S.check(&S.a); + try comptime S.check(&S.a); +} + +test "linksection on container-level const" { + if (linksections == null) return; + const S = struct { + const a: u32 linksection(const_linksection) = 123; + fn check(ptr: anytype) !void { + if (ptr.* != 123) return error.TestFailed; + } + }; + try S.check(&S.a); + try comptime S.check(&S.a); +} + +test "addrspace and linksection on container-level const" { + if (linksections == null) return; + const S = struct { + const a: u32 addrspace(.generic) linksection(const_linksection) = 123; + fn check(ptr: anytype) !void { + if (ptr.* != 123) return error.TestFailed; + } + }; + try S.check(&S.a); + try comptime S.check(&S.a); +} + +test "addrspace on container-level var" { + const S = struct { + var a: u32 addrspace(.generic) = 123; + fn check(ptr: anytype) !void { + if (ptr.* != 123) return error.TestFailed; + } + }; + try S.check(&S.a); +} + +test "linksection on container-level var" { + if (linksections == null) return; + const S = struct { + var a: u32 linksection(var_linksection) = 123; + fn check(ptr: anytype) !void { + if (ptr.* != 123) return error.TestFailed; + } + }; + try S.check(&S.a); +} + +test "addrspace and linksection on container-level var" { + if (linksections == null) return; + const S = struct { + var a: u32 addrspace(.generic) linksection(var_linksection) = 123; + fn check(ptr: anytype) !void { + if (ptr.* != 123) return error.TestFailed; + } + }; + try S.check(&S.a); +} + +test "addrspace on fn" { + const S = struct { + fn f() addrspace(.generic) u32 { + return 123; + } + fn check(fnPtr: anytype) !void { + if (fnPtr() != 123) return error.TestFailed; + } + }; + try S.check(&S.f); + try comptime S.check(&S.f); +} + +test "linksection on fn" { + if (linksections == null) return; + const S = struct { + fn f() linksection(fn_linksection) u32 { + return 123; + } + fn check(fnPtr: anytype) !void { + if (fnPtr() != 123) return error.TestFailed; + } + }; + try S.check(&S.f); + try comptime S.check(&S.f); +} + +test "addrspace and linksection on fn" { + if (linksections == null) return; + const S = struct { + fn f() addrspace(.generic) linksection(fn_linksection) u32 { + return 123; + } + fn check(fnPtr: anytype) !void { + if (fnPtr() != 123) return error.TestFailed; + } + }; + try S.check(&S.f); + try comptime S.check(&S.f); +} diff --git a/test/behavior/type_info_mul_linksection_addrspace_decls.zig b/test/behavior/type_info_mul_linksection_addrspace_decls.zig @@ -1,12 +0,0 @@ -const std = @import("std"); -const expect = std.testing.expect; - -pub const a linksection("sec_a") = 0; -pub const b linksection("sec_b") = 0; -pub const c addrspace("space_c") = 0; -pub const d addrspace("space_d") = 0; - -test { - const decls = @typeInfo(@This()).@"struct".decls; - try expect(decls.len == 4); -}