Skip to content

Commit

Permalink
cgen: fix generic struct to string (#10191)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed May 25, 2021
1 parent 306c16f commit f327470
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions vlib/v/gen/c/auto_str_struct.v
Expand Up @@ -94,12 +94,12 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri

for i, field in info.fields {
mut ptr_amp := if field.typ.is_ptr() { '&' } else { '' }
base_fmt := g.type_to_fmt1(field.typ)
base_fmt := g.type_to_fmt1(g.unwrap_generic(field.typ))

// manage prefix and quote symbol for the filed
mut quote_str := ''
mut prefix := ''
sym := g.table.get_type_symbol(field.typ)
sym := g.table.get_type_symbol(g.unwrap_generic(field.typ))
if sym.kind == .string {
quote_str = "'"
} else if field.typ in ast.charptr_types {
Expand Down
33 changes: 33 additions & 0 deletions vlib/v/tests/generics_struct_to_string_test.v
@@ -0,0 +1,33 @@
struct Info<T> {
data T
}

fn get_info<T>(res Info<T>) string {
return '$res'
}

fn test_generic_struct_to_string() {
mut ret := get_info(Info<bool>{true})
println(ret)
assert ret.contains('data: true')

ret = get_info(Info<int>{123})
println(ret)
assert ret.contains('data: 123')

ret = get_info(Info<f32>{f32(2.2)})
println(ret)
assert ret.contains('data: 2.2')

ret = get_info(Info<f64>{2.2})
println(ret)
assert ret.contains('data: 2.2')

ret = get_info(Info<string>{'aaa'})
println(ret)
assert ret.contains("data: 'aaa'")

ret = get_info(Info<u64>{u64(234)})
println(ret)
assert ret.contains('data: 234')
}

0 comments on commit f327470

Please sign in to comment.