1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

gen/c: fix gen_str_for_struct with custom ref str (fix #7179) (#8820)

This commit is contained in:
yuyi 2021-02-19 00:19:42 +08:00 committed by GitHub
parent 252074836b
commit 4878077c62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -530,12 +530,12 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp string, str_fn_name st
}
fn struct_auto_str_func(sym table.TypeSymbol, field_type table.Type, fn_name string, field_name string) string {
has_custom_str := sym.has_method('str')
has_custom_str, expects_ptr, _ := sym.str_method_info()
if sym.kind in [.enum_, .interface_] {
return '${fn_name}(it.${c_name(field_name)})'
} else if sym.kind == .struct_ {
mut obj := 'it.${c_name(field_name)}'
if field_type.is_ptr() {
if field_type.is_ptr() && !expects_ptr {
obj = '*$obj'
}
if has_custom_str {
@ -553,7 +553,8 @@ fn struct_auto_str_func(sym table.TypeSymbol, field_type table.Type, fn_name str
mut method_str := 'it.${c_name(field_name)}'
if sym.kind == .bool {
method_str += ' ? _SLIT("true") : _SLIT("false")'
} else if (field_type.is_int() || field_type.is_float()) && field_type.is_ptr() {
} else if (field_type.is_int() || field_type.is_float()) && field_type.is_ptr()
&& !expects_ptr {
// ptr int can be "nil", so this needs to be castet to a string
fmt := if sym.kind in [.f32, .f64] {
'%g\\000'

View File

@ -0,0 +1,20 @@
struct Foo {
bar int
}
fn (f &Foo) str() string {
return '${f.bar}'
}
struct Bar {
foo &Foo
}
fn test_interpolation_with_custom_ref_str() {
foo := Foo{}
bar := Bar { &foo }
println(bar)
assert '$bar'.contains('Bar{')
assert '$bar'.contains('foo: &0')
assert '$bar'.contains('}')
}