From c9d8fecc758334df925fef1e1a9c163ac8076ae8 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 7 Jan 2022 19:00:48 +0800 Subject: [PATCH] cgen: fix string range index in for mut var in (#13076) --- vlib/v/gen/c/index.v | 3 +++ .../v/tests/string_index_in_for_mut_in_test.v | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 vlib/v/tests/string_index_in_for_mut_in_test.v diff --git a/vlib/v/gen/c/index.v b/vlib/v/gen/c/index.v index 53068f794a..20642b84a8 100644 --- a/vlib/v/gen/c/index.v +++ b/vlib/v/gen/c/index.v @@ -65,6 +65,9 @@ fn (mut g Gen) range_expr(node ast.IndexExpr, range ast.RangeExpr) { } else { g.write('string_substr(') } + if node.left_type.is_ptr() { + g.write('*') + } g.expr(node.left) } else if sym.kind == .array { if node.is_gated { diff --git a/vlib/v/tests/string_index_in_for_mut_in_test.v b/vlib/v/tests/string_index_in_for_mut_in_test.v new file mode 100644 index 0000000000..3c2f30f54a --- /dev/null +++ b/vlib/v/tests/string_index_in_for_mut_in_test.v @@ -0,0 +1,25 @@ +module main + +[heap] +pub struct Grid { +pub mut: + header []string +} + +fn test_string_index_in_for_mut_in() { + h := ['yore', 'yaya'] + + mut grid := Grid{ + header: h + } + wrap_text(mut grid) +} + +fn wrap_text(mut gv Grid) { + for mut ch in gv.header { + ch = ch[1..2] + } + + println(gv) + assert gv.header == ['o', 'a'] +}