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 selector on shared var with embed #23394

Merged
merged 3 commits into from
Jan 7, 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
22 changes: 17 additions & 5 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -4121,6 +4121,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
g.write(')')
}
// struct embedding
mut has_embed := false
if sym.info in [ast.Struct, ast.Aggregate] {
if node.generic_from_embed_types.len > 0 && sym.info is ast.Struct {
if sym.info.embeds.len > 0 {
Expand All @@ -4130,36 +4131,43 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
if arr_val[0] == sym.info.embeds[0] {
g.write_selector_expr_embed_name(node, arr_val)
is_find = true
has_embed = true
break
}
}
}
if !is_find {
has_embed = node.from_embed_types.len > 0
g.write_selector_expr_embed_name(node, node.from_embed_types)
}
} else {
has_embed = node.from_embed_types.len > 0
Copy link
Member

Choose a reason for hiding this comment

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

This can be extracted as a single line, before the if sym.info.embeds.len > 0 { line above - it seems to be set in all cases, when node.from_embed_types.len > 0 .

Copy link
Member

Choose a reason for hiding this comment

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

Or even right after if sym.info in [ast.Struct, ast.Aggregate] {.

g.write_selector_expr_embed_name(node, node.from_embed_types)
}
} else if sym.info is ast.Aggregate {
agg_sym := g.table.sym(sym.info.types[g.aggregate_type_idx])
if !g.table.struct_has_field(agg_sym, field_name) {
has_embed = node.from_embed_types.len > 0
g.write_selector_expr_embed_name(node, node.from_embed_types)
}
} else {
has_embed = node.from_embed_types.len > 0
g.write_selector_expr_embed_name(node, node.from_embed_types)
}
}
alias_to_ptr := sym.info is ast.Alias && sym.info.parent_type.is_ptr()
is_dereferenced := node.expr is ast.SelectorExpr && node.expr.expr_type.is_ptr()
&& !node.expr.typ.is_ptr() && final_sym.kind in [.interface, .sum_type]
if field_is_opt || (((!is_dereferenced && unwrapped_expr_type.is_ptr())
|| sym.kind == .chan || alias_to_ptr) && node.from_embed_types.len == 0)
|| (node.expr.is_as_cast() && g.inside_smartcast) {
left_is_ptr := field_is_opt
|| (((!is_dereferenced && unwrapped_expr_type.is_ptr()) || sym.kind == .chan
|| alias_to_ptr) && node.from_embed_types.len == 0)
|| (node.expr.is_as_cast() && g.inside_smartcast)
if !has_embed && left_is_ptr {
g.write('->')
} else {
g.write('.')
}
if node.expr_type.has_flag(.shared_f) {
if !has_embed && node.expr_type.has_flag(.shared_f) {
g.write('val.')
}
if node.expr_type == 0 {
Expand Down Expand Up @@ -4239,14 +4247,18 @@ fn (mut g Gen) gen_closure_fn(expr_styp string, m ast.Fn, name string) {
}

fn (mut g Gen) write_selector_expr_embed_name(node ast.SelectorExpr, embed_types []ast.Type) {
is_shared := node.expr_type.has_flag(.shared_f)
for i, embed in embed_types {
embed_sym := g.table.sym(embed)
embed_name := embed_sym.embed_name()
is_left_ptr := if i == 0 {
node.expr_type.is_ptr()
node.expr_type.is_ptr() && !is_shared
} else {
embed_types[i - 1].is_ptr()
}
if i == 0 && is_shared {
g.write('->val')
}
if is_left_ptr {
g.write('->')
} else {
Expand Down
15 changes: 15 additions & 0 deletions vlib/v/tests/concurrency/selector_shared_var_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
struct Foo {
mut:
foo string
}

struct Bar {
Foo
}

fn test_main() {
shared bar := Bar{}
rlock bar {
assert bar.foo == ''
}
}
Loading