mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v.gen.c: fix fn variadic of reference param (#11115)
This commit is contained in:
parent
e7b8cf17e5
commit
11794039e2
@ -34,6 +34,7 @@ const (
|
|||||||
'vlib/builtin/int.v' /* TODO byteptr: vfmt converts `pub fn (nn byteptr) str() string {` to `nn &byte` and that conflicts with `nn byte` */,
|
'vlib/builtin/int.v' /* TODO byteptr: vfmt converts `pub fn (nn byteptr) str() string {` to `nn &byte` and that conflicts with `nn byte` */,
|
||||||
'vlib/builtin/string_charptr_byteptr_helpers.v' /* TODO byteptr: a temporary shim to ease the byteptr=>&byte transition */,
|
'vlib/builtin/string_charptr_byteptr_helpers.v' /* TODO byteptr: a temporary shim to ease the byteptr=>&byte transition */,
|
||||||
'vlib/v/tests/interop_test.v', /* bad comment formatting */
|
'vlib/v/tests/interop_test.v', /* bad comment formatting */
|
||||||
|
'vlib/v/tests/vargs_reference_param_test.v', /* variadic reference params */
|
||||||
'vlib/v/gen/js/tests/js.v', /* local `hello` fn, gets replaced with module `hello` aliased as `hl` */
|
'vlib/v/gen/js/tests/js.v', /* local `hello` fn, gets replaced with module `hello` aliased as `hl` */
|
||||||
]
|
]
|
||||||
vfmt_verify_list = [
|
vfmt_verify_list = [
|
||||||
|
@ -588,9 +588,11 @@ fn (mut g Gen) base_type(t ast.Type) string {
|
|||||||
if t.has_flag(.shared_f) {
|
if t.has_flag(.shared_f) {
|
||||||
styp = g.find_or_register_shared(t, styp)
|
styp = g.find_or_register_shared(t, styp)
|
||||||
}
|
}
|
||||||
nr_muls := g.unwrap_generic(t).nr_muls()
|
if !t.has_flag(.variadic) {
|
||||||
if nr_muls > 0 {
|
nr_muls := g.unwrap_generic(t).nr_muls()
|
||||||
styp += strings.repeat(`*`, nr_muls)
|
if nr_muls > 0 {
|
||||||
|
styp += strings.repeat(`*`, nr_muls)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return styp
|
return styp
|
||||||
}
|
}
|
||||||
|
@ -775,8 +775,10 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
|
|||||||
g.write('${name}(')
|
g.write('${name}(')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if node.receiver_type.is_ptr() && (!node.left_type.is_ptr()
|
if node.receiver_type.is_ptr()
|
||||||
|| node.from_embed_type != 0 || (node.left_type.has_flag(.shared_f) && node.name != 'str')) {
|
&& (!node.left_type.is_ptr() || node.left_type.has_flag(.variadic)
|
||||||
|
|| node.from_embed_type != 0
|
||||||
|
|| (node.left_type.has_flag(.shared_f) && node.name != 'str')) {
|
||||||
// The receiver is a reference, but the caller provided a value
|
// The receiver is a reference, but the caller provided a value
|
||||||
// Add `&` automatically.
|
// Add `&` automatically.
|
||||||
// TODO same logic in call_args()
|
// TODO same logic in call_args()
|
||||||
|
27
vlib/v/tests/vargs_reference_param_test.v
Normal file
27
vlib/v/tests/vargs_reference_param_test.v
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
[heap]
|
||||||
|
struct Foo {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
fn agg_stuff(stuffs ...&Foo) []&Foo {
|
||||||
|
stuffs2 := stuffs.clone()
|
||||||
|
return stuffs2
|
||||||
|
}
|
||||||
|
|
||||||
|
fn arr_stuff(stuffs []&Foo) []&Foo {
|
||||||
|
stuffs2 := stuffs.clone()
|
||||||
|
return stuffs2
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_vargs_with_reference_params() {
|
||||||
|
foo1 := &Foo{'foo'}
|
||||||
|
foo2 := &Foo{'bar'}
|
||||||
|
|
||||||
|
foo11 := agg_stuff(foo1, foo2)
|
||||||
|
println(foo11)
|
||||||
|
|
||||||
|
foo22 := arr_stuff([foo1, foo2])
|
||||||
|
println(foo22)
|
||||||
|
|
||||||
|
assert '$foo11' == '$foo22'
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user