compiler: generic variadic fix

pull/2514/head
joe-conigliaro 2019-10-24 03:44:05 +11:00 committed by Alexander Medvednikov
parent 0cf3e2e677
commit 2f5a742f3e
2 changed files with 18 additions and 7 deletions

View File

@ -1059,7 +1059,7 @@ fn (p mut Parser) fn_call_args(f mut Fn) {
// varargs
varg_type, varg_values := p.fn_call_vargs(f)
if f.is_variadic {
saved_args << '...$varg_type'
saved_args << varg_type
}
if p.tok == .comma {
p.error('wrong number of arguments for fn `$f.name`: expected $f.args.len, but got more')
@ -1158,11 +1158,12 @@ fn (p mut Parser) replace_type_params(f &Fn, ti TypeInst) []string {
fi = fi.right(6)
fr += 'array_'
}
if fi.starts_with('...') {
fi = fi.right(3)
}
is_varg := fi.starts_with('...')
if is_varg { fi = fi.right(3) }
if fi in ti.inst.keys() {
fr += ti.inst[fi]
mut t := ti.inst[fi]
if is_varg { t = '...$t' }
fr += t
// println("replaced $a => $fr")
} else {
fr += fi

View File

@ -2,7 +2,7 @@ struct VaTestGroup {
name string
}
fn variadic_test_a(name string, groups ...VaTestGroup) {
fn variadic_test(name string, groups ...VaTestGroup) {
assert groups.len == 2
assert groups[0].name == 'users'
assert groups[1].name == 'admins'
@ -11,5 +11,15 @@ fn variadic_test_a(name string, groups ...VaTestGroup) {
fn test_fn_variadic() {
group1 := VaTestGroup{name: 'users'}
group2 := VaTestGroup{name: 'admins'}
variadic_test_a('joe', group1, group2)
variadic_test('joe', group1, group2)
}
fn variadic_test_generic<T>(a int, b ...T) T {
b1 := b[0]
b2 := b[1]
return '$a $b1 $b2'
}
fn test_fn_variadic_generic() {
assert variadic_test_generic(111, 'hello', 'v') == '111 hello v'
}