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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix array auto cloning #20078

Merged
merged 6 commits into from
Dec 4, 2023
Merged

Conversation

felipensp
Copy link
Member

@felipensp felipensp commented Dec 3, 2023

Fix #20071

馃[deprecated] Generated by Copilot at 2142430

Implement range slices for fixed arrays in the C backend. Use new_array_from_c_array_no_alloc to create V arrays from C arrays without copying.

馃[deprecated] Generated by Copilot at 2142430

  • Implement range slices for fixed arrays, strings, and maps in V (link,

@felipensp felipensp marked this pull request as ready for review December 4, 2023 11:22
@@ -1525,7 +1525,21 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
g.write(')')
}
} else {
g.expr(node.left)
if is_range_slice && node.left is ast.IndexExpr && node.left.index is ast.RangeExpr {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no need to nest this if further; it can be:
} else if ... { ... } else { g.expr(node.left) }


if arr_sym.kind == .array_fixed {
arr_info := arr_sym.array_fixed_info()
g.write('new_array_from_c_array_no_alloc(${arr_info.size}, ${arr_info.size}, sizeof(${g.typ(arr_info.elem_type)}), ')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new_array_from_c_array_no_alloc will create a dynamic array, that will have its .data pointer the same as the last argument. This is not safe in the general case, and can lead to hard to diagnose bugs (the fixed arrays are usually on the stack, dynamic arrays and slices to them usually have their data on the heap; breaking that assumption can lead to leaking/returning dynamic arrays, with their .data pointer set to an address on the stack, and thus after the return, a potential dangling pointer).

Imho use new_array_from_c_array instead, which will copy the data.

Copy link
Member

@spytheman spytheman Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note also that:

fixed := [8]int{}
dynamic := fixed[..]
dump(dynamic)
dump(dynamic.data)
dump(fixed)
dump(unsafe { voidptr(&fixed[0]) })

also creates a copy, it does not reuse the data, for the same reason, that it is not safe to do so in the general case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed the slicing on array/array_fixed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the end, it was just one line fix.

@felipensp felipensp marked this pull request as draft December 4, 2023 14:35
@felipensp felipensp changed the title cgen: fix fixed array auto cloning cgen: fix array auto cloning Dec 4, 2023
Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very elegant fix.

@spytheman spytheman marked this pull request as ready for review December 4, 2023 19:07
@spytheman spytheman merged commit 1c10d2f into vlang:master Dec 4, 2023
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cgen: fixed array cgen error
2 participants