From 6a1e0322bdb9d682f826a5175f4afa7403dcd31b Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 1 Dec 2020 18:40:38 +0800 Subject: [PATCH] gen: fix interpolation variadic (#7059) --- vlib/v/gen/str.v | 4 ++++ vlib/v/tests/string_interpolation_variadic_test.v | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/vlib/v/gen/str.v b/vlib/v/gen/str.v index 7a5b5b9ba7..aeb6651a82 100644 --- a/vlib/v/gen/str.v +++ b/vlib/v/gen/str.v @@ -221,6 +221,8 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) { } else { g.write('*.*s') } + } else if typ.has_flag(.variadic) { + g.write('.*s') } else if typ.is_float() { g.write('$fmt${fspec:c}') } else if typ.is_pointer() { @@ -266,6 +268,8 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) { } else { g.expr(expr) } + } else if typ.has_flag(.variadic) { + g.gen_expr_to_string(expr, typ) } else if typ == table.bool_type { g.expr(expr) g.write(' ? _SLIT("true") : _SLIT("false")') diff --git a/vlib/v/tests/string_interpolation_variadic_test.v b/vlib/v/tests/string_interpolation_variadic_test.v index 715f93aace..6f2cb3468f 100644 --- a/vlib/v/tests/string_interpolation_variadic_test.v +++ b/vlib/v/tests/string_interpolation_variadic_test.v @@ -9,7 +9,7 @@ struct Man { } 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() { @@ -29,3 +29,16 @@ fn test_vargs_string_interpolation() { // 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]' +}