From fd3b179e118c94d72f7b92832f98c0d005b5a727 Mon Sep 17 00:00:00 2001 From: pfg Date: Wed, 14 Sep 2022 22:01:58 -0400 Subject: [PATCH 1/4] remove requirement for `@src` to be in a function --- src/Sema.zig | 14 +++++++----- test/behavior/src.zig | 22 +++++++++++++++++-- .../compile_errors/src_outside_function.zig | 9 -------- 3 files changed, 29 insertions(+), 16 deletions(-) delete mode 100644 test/cases/compile_errors/src_outside_function.zig diff --git a/src/Sema.zig b/src/Sema.zig index 1afecef46147..369c2091c01b 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -13842,14 +13842,18 @@ fn zirBuiltinSrc( const extra = sema.code.extraData(Zir.Inst.Src, extended.operand).data; const src = LazySrcLoc.nodeOffset(extra.node); - const func = sema.func orelse return sema.fail(block, src, "@src outside function", .{}); - const fn_owner_decl = sema.mod.declPtr(func.owner_decl); + const func_name = blk: { + const func = sema.func orelse { + break :blk ""; + }; + const fn_owner_decl = sema.mod.declPtr(func.owner_decl); + break :blk std.mem.span(fn_owner_decl.name); + }; const func_name_val = blk: { var anon_decl = try block.startAnonDecl(src); defer anon_decl.deinit(); - const name = std.mem.span(fn_owner_decl.name); - const bytes = try anon_decl.arena().dupe(u8, name[0 .. name.len + 1]); + const bytes = try anon_decl.arena().dupe(u8, func_name[0 .. func_name.len + 1]); const new_decl = try anon_decl.finish( try Type.Tag.array_u8_sentinel_0.create(anon_decl.arena(), bytes.len - 1), try Value.Tag.bytes.create(anon_decl.arena(), bytes), @@ -13861,7 +13865,7 @@ fn zirBuiltinSrc( const file_name_val = blk: { var anon_decl = try block.startAnonDecl(src); defer anon_decl.deinit(); - const relative_path = try fn_owner_decl.getFileScope().fullPath(sema.arena); + const relative_path = try block.getFileScope().fullPath(sema.arena); const absolute_path = std.fs.realpathAlloc(sema.arena, relative_path) catch |err| { return sema.fail(block, src, "failed to get absolute path of file '{s}': {s}", .{ relative_path, @errorName(err) }); }; diff --git a/test/behavior/src.zig b/test/behavior/src.zig index 017395a7c6c7..2230e4a1cdc1 100644 --- a/test/behavior/src.zig +++ b/test/behavior/src.zig @@ -1,9 +1,11 @@ +const src_outside_function = @src(); // do not move + fn doTheTest() !void { const src = @src(); // do not move - try expect(src.line == 2); + try expect(src.line == 4); try expect(src.column == 17); - try expect(std.mem.endsWith(u8, src.fn_name, "doTheTest")); + try expect(std.mem.eql(u8, src.fn_name, "doTheTest")); try expect(std.mem.endsWith(u8, src.file, "src.zig")); try expect(src.fn_name[src.fn_name.len] == 0); try expect(src.file[src.file.len] == 0); @@ -20,3 +22,19 @@ test "@src" { try doTheTest(); } + +test "@src outside function" { + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage1) return error.SkipZigTest; + + const src = src_outside_function; + + try expect(src.line == 1); + try expect(src.column == 30); + try expect(std.mem.eql(u8, src.fn_name, "")); + try expect(std.mem.endsWith(u8, src.file, "src.zig")); + try expect(src.fn_name[src.fn_name.len] == 0); + try expect(src.file[src.file.len] == 0); +} \ No newline at end of file diff --git a/test/cases/compile_errors/src_outside_function.zig b/test/cases/compile_errors/src_outside_function.zig deleted file mode 100644 index 8b66ba730bf8..000000000000 --- a/test/cases/compile_errors/src_outside_function.zig +++ /dev/null @@ -1,9 +0,0 @@ -comptime { - @src(); -} - -// error -// backend=stage2 -// target=native -// -// :2:5: error: @src outside function From ff3889672670b576fcf01acaee7d1c1f8b877092 Mon Sep 17 00:00:00 2001 From: pfg Date: Wed, 14 Sep 2022 22:12:48 -0400 Subject: [PATCH 2/4] change source location line to be comptime known --- src/Sema.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sema.zig b/src/Sema.zig index 369c2091c01b..081e3151390e 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -13884,7 +13884,7 @@ fn zirBuiltinSrc( // fn_name: [:0]const u8, field_values[1] = func_name_val; // line: u32 - field_values[2] = try Value.Tag.runtime_int.create(sema.arena, extra.line + 1); + field_values[2] = try Value.Tag.int_u64.create(sema.arena, extra.line + 1); // column: u32, field_values[3] = try Value.Tag.int_u64.create(sema.arena, extra.column + 1); From 71ffacd78d69f51ec72866eef8c3c88987eb893b Mon Sep 17 00:00:00 2001 From: pfg Date: Wed, 14 Sep 2022 22:19:30 -0400 Subject: [PATCH 3/4] change the test to run at comptime --- a.zig | 8 ++++++++ test/behavior/src.zig | 27 +++++++++++++-------------- 2 files changed, 21 insertions(+), 14 deletions(-) create mode 100644 a.zig diff --git a/a.zig b/a.zig new file mode 100644 index 000000000000..2d4a697ba398 --- /dev/null +++ b/a.zig @@ -0,0 +1,8 @@ +fn demo() void { + @compileLog(@src()); +} + +comptime { + _ = demo; + @compileLog(@src()); +} diff --git a/test/behavior/src.zig b/test/behavior/src.zig index 2230e4a1cdc1..75d7cd658f79 100644 --- a/test/behavior/src.zig +++ b/test/behavior/src.zig @@ -23,18 +23,17 @@ test "@src" { try doTheTest(); } -test "@src outside function" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage1) return error.SkipZigTest; - - const src = src_outside_function; - - try expect(src.line == 1); - try expect(src.column == 30); - try expect(std.mem.eql(u8, src.fn_name, "")); - try expect(std.mem.endsWith(u8, src.file, "src.zig")); - try expect(src.fn_name[src.fn_name.len] == 0); - try expect(src.file[src.file.len] == 0); +test "@src comptime, outside function" { + comptime { + if (builtin.zig_backend == .stage1) return error.SkipZigTest; + + const src = src_outside_function; + + try expect(src.line == 1); + try expect(src.column == 30); + try expect(std.mem.eql(u8, src.fn_name, "")); + try expect(std.mem.endsWith(u8, src.file, "src.zig")); + try expect(src.fn_name[src.fn_name.len] == 0); + try expect(src.file[src.file.len] == 0); + } } \ No newline at end of file From db59e356505988ed4f3297510bcaf73aab3b5b4b Mon Sep 17 00:00:00 2001 From: pfg Date: Wed, 14 Sep 2022 23:23:15 -0400 Subject: [PATCH 4/4] remove the test expecting src fields to be runtime --- a.zig | 8 -------- test/cases/compile_errors/src_fields_runtime.zig | 14 -------------- 2 files changed, 22 deletions(-) delete mode 100644 a.zig delete mode 100644 test/cases/compile_errors/src_fields_runtime.zig diff --git a/a.zig b/a.zig deleted file mode 100644 index 2d4a697ba398..000000000000 --- a/a.zig +++ /dev/null @@ -1,8 +0,0 @@ -fn demo() void { - @compileLog(@src()); -} - -comptime { - _ = demo; - @compileLog(@src()); -} diff --git a/test/cases/compile_errors/src_fields_runtime.zig b/test/cases/compile_errors/src_fields_runtime.zig deleted file mode 100644 index 0bdb5af81cbe..000000000000 --- a/test/cases/compile_errors/src_fields_runtime.zig +++ /dev/null @@ -1,14 +0,0 @@ -pub export fn entry1() void { - const s = @src(); - comptime var a: []const u8 = s.file; - comptime var b: []const u8 = s.fn_name; - comptime var c: u32 = s.column; - comptime var d: u32 = s.line; - _ = a; _ = b; _ = c; _ = d; -} - -// error -// backend=stage2 -// target=native -// -// :6:28: error: cannot store runtime value in compile time variable