authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-24 03:58:47-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-10-24 03:58:47-04:00
logb477279c37d1ca1c141a3e30589bd974687b85cc
tree9dda7e46b7f5b916a5b21fe240deecc32bbdfd71
parentb798aaf49937568541e8d89eb0be0058871a969f
parentc01aa26bd3715f2426f45f67de944b3192293d37
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #17688 from ziglang/comptime-src

Sema: make `@src().line` comptime-known

3 files changed, 17 insertions(+), 41 deletions(-)

src/Sema.zig+1-4
...@@ -16861,10 +16861,7 @@ fn zirBuiltinSrc(...@@ -16861,10 +16861,7 @@ fn zirBuiltinSrc(
16861 // fn_name: [:0]const u8,16861 // fn_name: [:0]const u8,
16862 func_name_val,16862 func_name_val,
16863 // line: u32,16863 // line: u32,
16864 try mod.intern(.{ .runtime_value = .{16864 (try mod.intValue(Type.u32, extra.line + 1)).toIntern(),
16865 .ty = .u32_type,
16866 .val = (try mod.intValue(Type.u32, extra.line + 1)).toIntern(),
16867 } }),
16868 // column: u32,16865 // column: u32,
16869 (try mod.intValue(Type.u32, extra.column + 1)).toIntern(),16866 (try mod.intValue(Type.u32, extra.column + 1)).toIntern(),
16870 };16867 };
src/tracy.zig+16-20
...@@ -63,44 +63,40 @@ pub const Ctx = if (enable) ___tracy_c_zone_context else struct {...@@ -63,44 +63,40 @@ pub const Ctx = if (enable) ___tracy_c_zone_context else struct {
63pub inline fn trace(comptime src: std.builtin.SourceLocation) Ctx {63pub inline fn trace(comptime src: std.builtin.SourceLocation) Ctx {
64 if (!enable) return .{};64 if (!enable) return .{};
6565
66 if (enable_callstack) {66 const global = struct {
67 return ___tracy_emit_zone_begin_callstack(&.{67 const loc: ___tracy_source_location_data = .{
68 .name = null,68 .name = null,
69 .function = src.fn_name.ptr,69 .function = src.fn_name.ptr,
70 .file = src.file.ptr,70 .file = src.file.ptr,
71 .line = src.line,71 .line = src.line,
72 .color = 0,72 .color = 0,
73 }, callstack_depth, 1);73 };
74 };
75
76 if (enable_callstack) {
77 return ___tracy_emit_zone_begin_callstack(&global.loc, callstack_depth, 1);
74 } else {78 } else {
75 return ___tracy_emit_zone_begin(&.{79 return ___tracy_emit_zone_begin(&global.loc, 1);
76 .name = null,
77 .function = src.fn_name.ptr,
78 .file = src.file.ptr,
79 .line = src.line,
80 .color = 0,
81 }, 1);
82 }80 }
83}81}
8482
85pub inline fn traceNamed(comptime src: std.builtin.SourceLocation, comptime name: [:0]const u8) Ctx {83pub inline fn traceNamed(comptime src: std.builtin.SourceLocation, comptime name: [:0]const u8) Ctx {
86 if (!enable) return .{};84 if (!enable) return .{};
8785
88 if (enable_callstack) {86 const global = struct {
89 return ___tracy_emit_zone_begin_callstack(&.{87 const loc: ___tracy_source_location_data = .{
90 .name = name.ptr,88 .name = name.ptr,
91 .function = src.fn_name.ptr,89 .function = src.fn_name.ptr,
92 .file = src.file.ptr,90 .file = src.file.ptr,
93 .line = src.line,91 .line = src.line,
94 .color = 0,92 .color = 0,
95 }, callstack_depth, 1);93 };
94 };
95
96 if (enable_callstack) {
97 return ___tracy_emit_zone_begin_callstack(&global.loc, callstack_depth, 1);
96 } else {98 } else {
97 return ___tracy_emit_zone_begin(&.{99 return ___tracy_emit_zone_begin(&global.loc, 1);
98 .name = name.ptr,
99 .function = src.fn_name.ptr,
100 .file = src.file.ptr,
101 .line = src.line,
102 .color = 0,
103 }, 1);
104 }100 }
105}101}
106102
test/cases/compile_errors/src_fields_runtime.zig deleted-17
...@@ -1,17 +0,0 @@
1pub export fn entry1() void {
2 const s = @src();
3 comptime var a: []const u8 = s.file;
4 comptime var b: []const u8 = s.fn_name;
5 comptime var c: u32 = s.column;
6 comptime var d: u32 = s.line;
7 _ = a;
8 _ = b;
9 _ = c;
10 _ = d;
11}
12
13// error
14// backend=stage2
15// target=native
16//
17// :6:28: error: cannot store runtime value in compile time variable