Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix comptime ref argument passing #21335

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,14 @@ fn (mut g Gen) resolve_comptime_args(func ast.Fn, mut node_ ast.CallExpr, concre
if param_typ.nr_muls() > 0 && comptime_args[k].nr_muls() > 0 {
comptime_args[k] = comptime_args[k].set_nr_muls(0)
}
} else if mut call_arg.expr.right is ast.Ident {
mut ctyp := g.comptime.get_comptime_var_type(call_arg.expr.right)
if ctyp != ast.void_type {
comptime_args[k] = ctyp
if param_typ.nr_muls() > 0 && comptime_args[k].nr_muls() > 0 {
comptime_args[k] = comptime_args[k].set_nr_muls(0)
}
}
}
} else if mut call_arg.expr is ast.ComptimeSelector {
comptime_args[k] = g.comptime.comptime_for_field_type
Expand Down
101 changes: 101 additions & 0 deletions vlib/v/tests/comptime_ref_arg_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Declare structures covering a subset of a HAR file

struct HarContent {
size i64
mime_type string @[json: 'mimeType']
}

struct HarResponse {
content HarContent
}

struct HarEntry {
response HarResponse
}

pub struct HarLog {
entries []HarEntry
}

struct Har {
log HarLog
}

// Declare function printing object contents using generics

fn show[T](val T) {
$if T is string {
show_string(val)
} $else $if T is $array {
show_array(val)
} $else $if T is $struct {
show_struct(&val)
} $else {
print('primitive: ${val.str()}')
}
}

fn show_array[T](array []T) {
println('array []${T.name}')
for i, item in array {
println('item ${i}')
show(item)
println('')
}
}

fn show_struct[T](object &T) {
println('struct ${T.name}')
$for field in T.fields {
mut json_name := field.name
for attr in field.attrs {
if attr.starts_with('json: ') {
json_name = attr[6..]
}
}

print('key: ')
show_string(json_name)
println('')

println('value ${T.name}.${field.name} (json: ${json_name}), field.typ: ${field.typ}')
$if field.typ is string {
print('string: ')
show_string(object.$(field.name))
} $else $if field.is_array {
show_array(object.$(field.name))
} $else $if field.is_struct {
item := object.$(field.name)
show_struct(&item)
} $else {
print('primitive: ')
print(object.$(field.name).str())
}
println('')
}
}

fn show_string(s string) {
print(`"`)
print(s)
print(`"`)
}

fn test_main() {
har := Har{
log: HarLog{
entries: [
HarEntry{
response: HarResponse{
content: HarContent{
size: 48752
mime_type: 'text/html'
}
}
},
]
}
}
show(har)
assert true
}
Loading