authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-07 14:05:55-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-08 07:47:14-07:00
logcf87a1a7cf57123aad533a73d8e0fa4d4916a674
treeeec5763a778319fbc8aafe991a2ff504223fdc48
parentca012e5b69b9ff275c6e366c0348219d1c816f18

language: add module name field to `@src`

closes #20963

3 files changed, 38 insertions(+), 4 deletions(-)

lib/std/builtin.zig+3
...@@ -243,6 +243,9 @@ pub const AddressSpace = enum(u5) {...@@ -243,6 +243,9 @@ pub const AddressSpace = enum(u5) {
243/// This data structure is used by the Zig language code generation and243/// This data structure is used by the Zig language code generation and
244/// therefore must be kept in sync with the compiler implementation.244/// therefore must be kept in sync with the compiler implementation.
245pub const SourceLocation = struct {245pub const SourceLocation = struct {
246 /// The name chosen when compiling. Not a file path.
247 module: [:0]const u8,
248 /// Relative to the root directory of its module.
246 file: [:0]const u8,249 file: [:0]const u8,
247 fn_name: [:0]const u8,250 fn_name: [:0]const u8,
248 line: u32,251 line: u32,
src/Sema.zig+33-4
...@@ -17769,11 +17769,12 @@ fn zirBuiltinSrc(...@@ -17769,11 +17769,12 @@ fn zirBuiltinSrc(
17769 defer tracy.end();17769 defer tracy.end();
1777017770
17771 const pt = sema.pt;17771 const pt = sema.pt;
17772 const mod = pt.zcu;17772 const zcu = pt.zcu;
17773 const extra = sema.code.extraData(Zir.Inst.Src, extended.operand).data;17773 const extra = sema.code.extraData(Zir.Inst.Src, extended.operand).data;
17774 const fn_owner_decl = mod.funcOwnerDeclPtr(sema.func_index);17774 const fn_owner_decl = zcu.funcOwnerDeclPtr(sema.func_index);
17775 const ip = &mod.intern_pool;17775 const ip = &zcu.intern_pool;
17776 const gpa = sema.gpa;17776 const gpa = sema.gpa;
17777 const file_scope = fn_owner_decl.getFileScope(zcu);
1777717778
17778 const func_name_val = v: {17779 const func_name_val = v: {
17779 const func_name_len = fn_owner_decl.name.length(ip);17780 const func_name_len = fn_owner_decl.name.length(ip);
...@@ -17799,8 +17800,34 @@ fn zirBuiltinSrc(...@@ -17799,8 +17800,34 @@ fn zirBuiltinSrc(
17799 } });17800 } });
17800 };17801 };
1780117802
17803 const module_name_val = v: {
17804 const module_name = file_scope.mod.fully_qualified_name;
17805 const array_ty = try pt.intern(.{ .array_type = .{
17806 .len = module_name.len,
17807 .sentinel = .zero_u8,
17808 .child = .u8_type,
17809 } });
17810 break :v try pt.intern(.{ .slice = .{
17811 .ty = .slice_const_u8_sentinel_0_type,
17812 .ptr = try pt.intern(.{ .ptr = .{
17813 .ty = .manyptr_const_u8_sentinel_0_type,
17814 .base_addr = .{ .anon_decl = .{
17815 .orig_ty = .slice_const_u8_sentinel_0_type,
17816 .val = try pt.intern(.{ .aggregate = .{
17817 .ty = array_ty,
17818 .storage = .{
17819 .bytes = try ip.getOrPutString(gpa, pt.tid, module_name, .maybe_embedded_nulls),
17820 },
17821 } }),
17822 } },
17823 .byte_offset = 0,
17824 } }),
17825 .len = (try pt.intValue(Type.usize, module_name.len)).toIntern(),
17826 } });
17827 };
17828
17802 const file_name_val = v: {17829 const file_name_val = v: {
17803 const file_name = fn_owner_decl.getFileScope(mod).sub_file_path;17830 const file_name = file_scope.sub_file_path;
17804 const array_ty = try pt.intern(.{ .array_type = .{17831 const array_ty = try pt.intern(.{ .array_type = .{
17805 .len = file_name.len,17832 .len = file_name.len,
17806 .sentinel = .zero_u8,17833 .sentinel = .zero_u8,
...@@ -17827,6 +17854,8 @@ fn zirBuiltinSrc(...@@ -17827,6 +17854,8 @@ fn zirBuiltinSrc(
1782717854
17828 const src_loc_ty = try pt.getBuiltinType("SourceLocation");17855 const src_loc_ty = try pt.getBuiltinType("SourceLocation");
17829 const fields = .{17856 const fields = .{
17857 // module: [:0]const u8,
17858 module_name_val,
17830 // file: [:0]const u8,17859 // file: [:0]const u8,
17831 file_name_val,17860 file_name_val,
17832 // fn_name: [:0]const u8,17861 // fn_name: [:0]const u8,
test/behavior/src.zig+2
...@@ -7,11 +7,13 @@ fn doTheTest() !void {...@@ -7,11 +7,13 @@ fn doTheTest() !void {
7 try expect(std.mem.endsWith(u8, src.file, "src.zig"));7 try expect(std.mem.endsWith(u8, src.file, "src.zig"));
8 try expect(src.fn_name[src.fn_name.len] == 0);8 try expect(src.fn_name[src.fn_name.len] == 0);
9 try expect(src.file[src.file.len] == 0);9 try expect(src.file[src.file.len] == 0);
10 if (!std.mem.eql(u8, src.module, "test") and !std.mem.eql(u8, src.module, "root")) return error.TestFailure;
10}11}
1112
12const std = @import("std");13const std = @import("std");
13const builtin = @import("builtin");14const builtin = @import("builtin");
14const expect = std.testing.expect;15const expect = std.testing.expect;
16const expectEqualStrings = std.testing.expectEqualStrings;
1517
16test "@src" {18test "@src" {
17 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO19 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO