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

cgen: fix str() fails when the structure 'charptr' type field is nil (fix #15970) (#16002)

This commit is contained in:
shove 2022-10-09 13:33:45 +08:00 committed by GitHub
parent bce420c34d
commit e2398cafd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 2 deletions

View File

@ -932,7 +932,7 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
} else {
// manage C charptr
if field.typ in ast.charptr_types {
fn_body.write_string('tos2((byteptr)$func)')
fn_body.write_string('tos4((byteptr)$func)')
} else {
if field.typ.is_ptr() && sym.kind == .struct_ {
funcprefix += '(indent_count > 25)? _SLIT("<probably circular>") : '

View File

@ -763,7 +763,7 @@ fn (mut g JsGen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name st
} else {
// manage C charptr
if field.typ in ast.charptr_types {
fn_builder.write_string('tos2((byteptr)$func)')
fn_builder.write_string('tos4((byteptr)$func)')
} else {
if field.typ.is_ptr() && sym.kind == .struct_ {
fn_builder.write_string('(indent_count > 25)? new string("<probably circular>") : ')

View File

@ -463,3 +463,14 @@ fn test_c_struct_typedef() {
}'
}
}
struct CharptrToStr {
cp charptr = charptr(0)
}
fn test_charptr_nil_to_str() {
c := CharptrToStr{}
assert c.str() == r'CharptrToStr{
cp: C""
}'
}