Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix codegen for method call on rangeexpr #22291

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -1681,7 +1681,9 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
if idx is ast.RangeExpr {
// expr is arr[range].clone()
// use array_clone_static instead of array_clone
name = util.no_dots('${receiver_type_name}_${node.name}_static')
if node.name == 'clone' {
name = util.no_dots('${receiver_type_name}_${node.name}_static')
}
is_range_slice = true
}
}
Expand Down Expand Up @@ -1759,6 +1761,11 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
&& g.typ(left_type) == g.typ(node.receiver_type)) {
g.write('&')
}
} else {
if node.name != 'clone' && !left_type.is_ptr() {
g.write('ADDR(${rec_cc_type}, ')
cast_n++
}
}
} else if !node.receiver_type.is_ptr() && left_type.is_ptr() && node.name != 'str'
&& node.from_embed_types.len == 0 {
Expand Down
16 changes: 16 additions & 0 deletions vlib/v/tests/fns/method_call_on_rangeexpr_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type Buffer = []u8

pub fn (mut sb Buffer) vbytes() string {
return sb[0..sb.len].a().str()
}

pub fn (mut sb Buffer) a() Buffer {
return sb
}

fn test_main() {
mut b := Buffer([]u8{cap: 10})
b << 1
b << 2
assert b.vbytes() == 'Buffer([1, 2])'
}
Loading