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 shared array slice #23427

Merged
merged 3 commits into from
Jan 11, 2025
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
5 changes: 4 additions & 1 deletion vlib/v/checker/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,10 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
left_type = left_type.set_nr_muls(1)
}
} else if left_type.has_flag(.shared_f) {
left_type = left_type.clear_flag(.shared_f).deref()
left_type = left_type.clear_flag(.shared_f)
if left_type.is_ptr() {
left_type = left_type.deref()
}
}
if ident_var_info.share == .atomic_t {
left_type = left_type.set_flag(.atomic_f)
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -5463,6 +5463,9 @@ fn (mut c Checker) fail_if_unreadable(expr ast.Expr, typ ast.Type, what string)
if c.fail_if_unreadable(expr.left, expr.left_type, what) {
return true
}
if typ.has_flag(.shared_f) && expr.left is ast.SelectorExpr {
return false
}
}
ast.InfixExpr {
pos = expr.left.pos().extend(expr.pos)
Expand Down
4 changes: 3 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -2928,7 +2928,9 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ
g.write('&')
}
g.expr(expr)
g.write('->val')
if expr !is ast.IndexExpr {
g.write('->val')
}
return
}
if got_is_ptr && !expected_is_ptr && neither_void && exp_sym.kind != .placeholder
Expand Down
22 changes: 18 additions & 4 deletions vlib/v/gen/c/index.v
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
}

fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) {
sym := g.table.final_sym(g.unwrap_generic(node.left_type))
unwrapped_left_type := g.unwrap_generic(node.left_type)
sym := g.table.final_sym(unwrapped_left_type)
mut tmp_opt := ''
mut cur_line := ''
mut gen_or := node.or_expr.kind != .absent || node.is_option
left_is_shared := unwrapped_left_type.has_flag(.shared_f)
if sym.kind == .string {
if node.is_gated {
g.write('string_substr_ni(')
Expand All @@ -92,10 +94,16 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) {
} else {
g.write('array_slice(')
}
if left_is_shared {
g.write('(')
}
if node.left_type.is_ptr() {
g.write('*')
}
g.expr(node.left)
if left_is_shared {
g.write(').val')
}
} else if sym.info is ast.ArrayFixed {
// Convert a fixed array to V array when doing `fixed_arr[start..end]`
noscan := g.check_noscan(sym.info.elem_type)
Expand All @@ -107,6 +115,9 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) {
g.write('new_array_from_c_array${noscan}(')
ctype := g.styp(sym.info.elem_type)
g.write('${sym.info.size}, ${sym.info.size}, sizeof(${ctype}), ')
if left_is_shared {
g.write('(')
}
if node.left_type.is_ptr() {
g.write('*')
}
Expand All @@ -118,11 +129,14 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) {
g.write('${styp} ${var} = ')
g.expr(node.left)
g.writeln(';')
g.write2(line, ' ${var})')
g.write2(line, ' ${var}')
} else {
g.expr(node.left)
g.write(')')
}
if left_is_shared {
g.write(').val')
}
g.write(')')
} else {
g.expr(node.left)
}
Expand Down Expand Up @@ -192,7 +206,7 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) {
}

if left_is_shared {
if left_is_ptr {
if node.index !is ast.RangeExpr && left_is_ptr {
g.write('->val')
} else {
g.write('.val')
Expand Down
27 changes: 27 additions & 0 deletions vlib/v/tests/concurrency/shared_array_slice_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
struct Foo {
pub mut:
buf shared []u8 = []u8{len: 20, init: 6}
buf2 shared [20]u8
}

fn test_main() {
mut foo := Foo{
buf2: [20]u8{init: 5}
}
rlock foo.buf {
if foo.buf.len > 0 {
x := 10
println('first ${x} bytes: ' + foo.buf[..x].str())
sliced := foo.buf[..x]
assert sliced == [u8(6), 6, 6, 6, 6, 6, 6, 6, 6, 6]
} else {
println('no data')
assert false
}
}
rlock foo.buf2 {
x := 4
println('first ${x} bytes: ' + foo.buf2[..x].str())
assert foo.buf2[..x] == [u8(5), 5, 5, 5]
}
}
Loading