1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix comptime for method call with arguments (#15236)

This commit is contained in:
yuyi 2022-07-27 08:22:05 +08:00 committed by GitHub
parent afaab7ab4d
commit 6a7eb82d9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -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_<type> 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(', ')

View File

@ -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
}