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

cgen: add & before ptr arr elements (#17379)

This commit is contained in:
Swastik Baranwal 2023-02-22 17:40:07 +05:30 committed by GitHub
parent 6682911bc3
commit c173104295
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 3 deletions

View File

@ -558,7 +558,13 @@ fn (mut g Gen) gen_str_for_array(info ast.Array, styp string, str_fn_name string
}
if should_use_indent_func(sym.kind) && !sym_has_str_method {
if is_elem_ptr {
g.auto_str_funcs.writeln('\t\tstring x = indent_${elem_str_fn_name}(*it, indent_count);')
deref, deref_label := deref_kind(str_method_expects_ptr, is_elem_ptr,
typ)
g.auto_str_funcs.writeln('\t\tstring x = _SLIT("nil");')
g.auto_str_funcs.writeln('\t\tif (it != 0) {')
g.auto_str_funcs.writeln('\t\t\tstrings__Builder_write_string(&sb, _SLIT("${deref_label}"));')
g.auto_str_funcs.writeln('\t\t\tx = ${elem_str_fn_name}(${deref}it);')
g.auto_str_funcs.writeln('\t\t}')
} else {
g.auto_str_funcs.writeln('\t\tstring x = indent_${elem_str_fn_name}(it, indent_count);')
}

View File

@ -53,8 +53,8 @@ StructOfFixedArraysOfListeners{
[&0, &0]
[&&0, &&0]
-----------------------------------------------------
[EventListener{
[&EventListener{
x: 123
}, EventListener{
}, &EventListener{
x: 0
}]

View File

@ -0,0 +1,22 @@
struct Point {
x int
y int
}
fn test_array_of_ptr_str() {
point_1 := &Point{1, 2}
point_2 := &Point{3, 4}
point_3 := &Point{5, 6}
points := [point_1, point_2, point_3]
assert '${points}' == '[&Point{
x: 1
y: 2
}, &Point{
x: 3
y: 4
}, &Point{
x: 5
y: 6
}]
'.trim_indent()
}