mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
cgen: fix comptime ref argument passing (#21335)
This commit is contained in:
parent
59013f1700
commit
007305921a
2 changed files with 109 additions and 0 deletions
|
@ -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
|
||||
|
|
101
vlib/v/tests/comptime_ref_arg_test.v
Normal file
101
vlib/v/tests/comptime_ref_arg_test.v
Normal file
|
@ -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…
Add table
Add a link
Reference in a new issue