diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 9329c71909..e99c537aae 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -147,12 +147,13 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) { } else { // last argument; try to expand if it's []string idx := i - node.args.len + last_arg := g.expr_string(node.args.last().expr) if m.params[i].typ.is_int() || m.params[i].typ.idx() == ast.bool_type_idx { // Gets the type name and cast the string to the type with the string_ function type_name := g.table.type_symbols[int(m.params[i].typ)].str() - g.write('string_${type_name}(((string*)${node.args[node.args.len - 1]}.data) [$idx])') + g.write('string_${type_name}(((string*)${last_arg}.data) [$idx])') } else { - g.write('((string*)${node.args[node.args.len - 1]}.data) [$idx] ') + g.write('((string*)${last_arg}.data) [$idx] ') } if i < m.params.len - 1 { g.write(', ') diff --git a/vlib/v/tests/comptime_for_method_call_with_args_test.v b/vlib/v/tests/comptime_for_method_call_with_args_test.v new file mode 100644 index 0000000000..a53ae4d730 --- /dev/null +++ b/vlib/v/tests/comptime_for_method_call_with_args_test.v @@ -0,0 +1,17 @@ +import os + +struct Dummy {} + +fn (d Dummy) sample(file_name string) { + println(file_name) +} + +fn test_comptime_for_method_call_with_args() { + $for method in Dummy.methods { + if os.args.len > 1 { + d := Dummy{} + d.$method(os.args) + } + } + assert true +}