Skip to content

Commit

Permalink
cgen,ast: fix interface conversion codegen race issue (fix vlang#22640,
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Oct 26, 2024
1 parent be0de59 commit 067c5b6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion vlib/v/ast/types.v
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub mut:
methods []Fn
embeds []Type
// `I1 is I2` conversions
conversions map[int][]Type
conversions shared map[int][]Type
// generic interface support
is_generic bool
generic_types []Type
Expand Down
10 changes: 6 additions & 4 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -7598,10 +7598,12 @@ fn (mut g Gen) as_cast(node ast.AsCast) {
g.write(')')

mut info := expr_type_sym.info as ast.Interface
if node.typ !in info.conversions {
left_variants := g.table.iface_types[expr_type_sym.name]
right_variants := g.table.iface_types[sym.name]
info.conversions[node.typ] = left_variants.filter(it in right_variants)
lock info.conversions {
if node.typ !in info.conversions {
left_variants := g.table.iface_types[expr_type_sym.name]
right_variants := g.table.iface_types[sym.name]
info.conversions[node.typ] = left_variants.filter(it in right_variants)
}
}
expr_type_sym.info = info
} else if mut expr_type_sym.info is ast.Interface && node.expr_type != node.typ {
Expand Down
25 changes: 13 additions & 12 deletions vlib/v/gen/c/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -776,18 +776,19 @@ fn (mut g Gen) gen_interface_is_op(node ast.InfixExpr) {
right_sym := g.table.sym(node.right_type)

mut info := left_sym.info as ast.Interface

common_variants := info.conversions[node.right_type] or {
left_variants := g.table.iface_types[left_sym.name]
right_variants := g.table.iface_types[right_sym.name]
c := left_variants.filter(it in right_variants)
info.conversions[node.right_type] = c
c
}
left_sym.info = info
if common_variants.len == 0 {
g.write('false')
return
lock info.conversions {
common_variants := info.conversions[node.right_type] or {
left_variants := g.table.iface_types[left_sym.name]
right_variants := g.table.iface_types[right_sym.name]
c := left_variants.filter(it in right_variants)
info.conversions[node.right_type] = c
c
}
left_sym.info = info
if common_variants.len == 0 {
g.write('false')
return
}
}
g.write('I_${left_sym.cname}_is_I_${right_sym.cname}(')
if node.left_type.is_ptr() {
Expand Down

0 comments on commit 067c5b6

Please sign in to comment.