From dec703006ba0acc2b9dfdb29a09f4b8c80744462 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 15 Dec 2025 14:38:48 +0200 Subject: [PATCH] builtin,cgen: add a source .location field to `$for method in Type.methods {` (#25976) --- vlib/builtin/meta_function.v | 1 + vlib/v/gen/c/comptime.v | 2 ++ vlib/v/gen/c/testdata/attr_string_quotes_escape.out | 8 +------- vlib/v/gen/c/testdata/attr_string_quotes_escape.vv | 2 +- vlib/v/tests/comptime/comptime_call_test.v | 7 +++++++ 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/vlib/builtin/meta_function.v b/vlib/builtin/meta_function.v index d7e9b9c98..a96fd5054 100644 --- a/vlib/builtin/meta_function.v +++ b/vlib/builtin/meta_function.v @@ -11,6 +11,7 @@ pub: pub struct FunctionData { pub: name string + location string attrs []string args []FunctionParam return_type int diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index d3cc6e73a..ae06ce30e 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -662,6 +662,8 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) { g.comptime.comptime_for_method_var = node.val_var g.writeln('/* method ${i} : ${method.name} */ {') g.writeln('\t${node.val_var}.name = _S("${method.name}");') + mlocation := util.cescaped_path(util.path_styled_for_error_messages(method.file)) + g.writeln('\t${node.val_var}.location = _S("${mlocation}:${method.name_pos.line_nr + 1}:${method.name_pos.col}");') if method.attrs.len == 0 { g.writeln('\t${node.val_var}.attrs = builtin____new_array_with_default(0, 0, sizeof(string), 0);') } else { diff --git a/vlib/v/gen/c/testdata/attr_string_quotes_escape.out b/vlib/v/gen/c/testdata/attr_string_quotes_escape.out index 261e94460..6deec9a66 100644 --- a/vlib/v/gen/c/testdata/attr_string_quotes_escape.out +++ b/vlib/v/gen/c/testdata/attr_string_quotes_escape.out @@ -1,7 +1 @@ -FunctionData{ - name: 'sample_method' - attrs: ['option: "1"'] - args: [] - return_type: 1 - typ: 0 -} +['option: "1"'] diff --git a/vlib/v/gen/c/testdata/attr_string_quotes_escape.vv b/vlib/v/gen/c/testdata/attr_string_quotes_escape.vv index d1564c4cc..a59c7a889 100644 --- a/vlib/v/gen/c/testdata/attr_string_quotes_escape.vv +++ b/vlib/v/gen/c/testdata/attr_string_quotes_escape.vv @@ -6,6 +6,6 @@ fn (d Dummy) sample_method() { fn main() { $for method in Dummy.methods { - println(method) + println(method.attrs) } } diff --git a/vlib/v/tests/comptime/comptime_call_test.v b/vlib/v/tests/comptime/comptime_call_test.v index 9e464c4a2..8dc85327c 100644 --- a/vlib/v/tests/comptime/comptime_call_test.v +++ b/vlib/v/tests/comptime/comptime_call_test.v @@ -36,16 +36,23 @@ fn test_for_methods() { $if method.return_type is string { v := test.$method() r += v.str() + if method.name == 's' { + assert method.location.ends_with('vlib/v/tests/comptime/comptime_call_test.v:11:15') + } else if method.name == 's2' { + assert method.location.ends_with('vlib/v/tests/comptime/comptime_call_test.v:15:15') + } } $else $if method.return_type is int { // TODO // v := test.$method() v := '?' r += v.str() assert method.name == 'i' + assert method.location.ends_with('vlib/v/tests/comptime/comptime_call_test.v:7:15') } $else { // no return type test.$method() assert method.name == 'v' + assert method.location.ends_with('vlib/v/tests/comptime/comptime_call_test.v:3:15') } } assert r == '?testTwo' -- 2.39.5