From a1e0f2bc46fb828e55d3f605191042468913a19d Mon Sep 17 00:00:00 2001 From: Miccah Date: Sun, 7 Mar 2021 07:28:43 -0600 Subject: [PATCH] gen: add argument names to compile-time method struct (#9174) --- vlib/builtin/builtin.v | 3 ++- vlib/v/gen/c/comptime.v | 2 +- vlib/v/tests/comptime_method_args_test.v | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/comptime_method_args_test.v diff --git a/vlib/builtin/builtin.v b/vlib/builtin/builtin.v index 0d089c97fd..cbf2161a26 100644 --- a/vlib/builtin/builtin.v +++ b/vlib/builtin/builtin.v @@ -88,7 +88,8 @@ fn __print_assert_failure(i &VAssertMetaInfo) { // MethodArgs holds type information for function and/or method arguments. pub struct MethodArgs { pub: - typ int + typ int + name string } // FunctionData holds information about a parsed function. diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 09fe309cd0..90e3be7df0 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -336,7 +336,7 @@ fn (mut g Gen) comp_for(node ast.CompFor) { // Skip receiver arg for j, arg in method.params[1..] { typ := arg.typ.idx() - g.write(typ.str()) + g.write('{$typ.str(), _SLIT("$arg.name")}') if j < len - 1 { g.write(', ') } diff --git a/vlib/v/tests/comptime_method_args_test.v b/vlib/v/tests/comptime_method_args_test.v new file mode 100644 index 0000000000..5cec5fe050 --- /dev/null +++ b/vlib/v/tests/comptime_method_args_test.v @@ -0,0 +1,14 @@ +struct TestStruct {} + +fn (t TestStruct) test(arg1 string, arg2 string, arg3 string) {} + +fn test_comptime_method_names() { + $for method in TestStruct.methods { + if method.name == 'test' { + args := method.args + assert args[0].name == 'arg1' + assert args[1].name == 'arg2' + assert args[2].name == 'arg3' + } + } +}