gen: fix interpolation variadic (#7059)

pull/7038/head^2
yuyi 2020-12-01 18:40:38 +08:00 committed by GitHub
parent f1965c0510
commit 6a1e0322bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -221,6 +221,8 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
} else { } else {
g.write('*.*s') g.write('*.*s')
} }
} else if typ.has_flag(.variadic) {
g.write('.*s')
} else if typ.is_float() { } else if typ.is_float() {
g.write('$fmt${fspec:c}') g.write('$fmt${fspec:c}')
} else if typ.is_pointer() { } else if typ.is_pointer() {
@ -266,6 +268,8 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
} else { } else {
g.expr(expr) g.expr(expr)
} }
} else if typ.has_flag(.variadic) {
g.gen_expr_to_string(expr, typ)
} else if typ == table.bool_type { } else if typ == table.bool_type {
g.expr(expr) g.expr(expr)
g.write(' ? _SLIT("true") : _SLIT("false")') g.write(' ? _SLIT("true") : _SLIT("false")')

View File

@ -9,7 +9,7 @@ struct Man {
} }
fn my_variadic_function(x ...Man) string { fn my_variadic_function(x ...Man) string {
return '$x' // this interpolation should generate .str() methods for Man return '$x' // this interpolation should generate .str() methods for Man
} }
fn test_vargs_string_interpolation() { fn test_vargs_string_interpolation() {
@ -29,3 +29,16 @@ fn test_vargs_string_interpolation() {
// //
println(results) println(results)
} }
fn variadic_int(x ...int) string {
return '$x'
}
fn variadic_bool(x ...bool) string {
return '$x'
}
fn test_variadic_interpolation() {
assert variadic_int(3, 2, 1) == '[3, 2, 1]'
assert variadic_bool(true, false, true) == '[true, false, true]'
}