1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen, checker, parser: fix fixed array with channel (#18315)

This commit is contained in:
Felipe Pena 2023-06-02 04:55:08 -03:00 committed by GitHub
parent 82035f7610
commit a8ea1f9d50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 5 deletions

View File

@ -188,6 +188,15 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
} else if mut right is ast.PrefixExpr { } else if mut right is ast.PrefixExpr {
if right.op == .amp && right.right is ast.StructInit { if right.op == .amp && right.right is ast.StructInit {
right_type = c.expr(right) right_type = c.expr(right)
} else if right.op == .arrow {
right_type = c.expr(right)
right_type_sym := c.table.sym(right_type)
if right_type_sym.kind == .array_fixed
&& (right_type_sym.info as ast.ArrayFixed).is_fn_ret {
info := right_type_sym.info as ast.ArrayFixed
right_type = c.table.find_or_register_array_fixed(info.elem_type,
info.size, info.size_expr, false)
}
} }
} else if mut right is ast.Ident { } else if mut right is ast.Ident {
if right.kind == .function { if right.kind == .function {

View File

@ -353,6 +353,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
is_fixed_array_var := unaliased_right_sym.kind == .array_fixed && val !is ast.ArrayInit is_fixed_array_var := unaliased_right_sym.kind == .array_fixed && val !is ast.ArrayInit
&& (val in [ast.Ident, ast.IndexExpr, ast.CallExpr, ast.SelectorExpr, ast.DumpExpr] && (val in [ast.Ident, ast.IndexExpr, ast.CallExpr, ast.SelectorExpr, ast.DumpExpr]
|| (val is ast.CastExpr && (val as ast.CastExpr).expr !is ast.ArrayInit) || (val is ast.CastExpr && (val as ast.CastExpr).expr !is ast.ArrayInit)
|| (val is ast.PrefixExpr && (val as ast.PrefixExpr).op == .arrow)
|| (val is ast.UnsafeExpr && (val as ast.UnsafeExpr).expr is ast.Ident)) || (val is ast.UnsafeExpr && (val as ast.UnsafeExpr).expr is ast.Ident))
&& !g.pref.translated && !g.pref.translated
g.is_assign_lhs = true g.is_assign_lhs = true

View File

@ -1408,16 +1408,24 @@ pub fn (mut g Gen) write_typedef_types() {
chan_inf := sym.chan_info() chan_inf := sym.chan_info()
chan_elem_type := chan_inf.elem_type chan_elem_type := chan_inf.elem_type
if !chan_elem_type.has_flag(.generic) { if !chan_elem_type.has_flag(.generic) {
el_stype := g.typ(chan_elem_type) mut el_stype := g.typ(chan_elem_type)
is_fixed_arr := g.table.sym(chan_elem_type).kind == .array_fixed
val_arg_pop := if is_fixed_arr { '&val.ret_arr' } else { '&val' }
val_arg_push := if is_fixed_arr { 'val' } else { '&val' }
push_arg := if is_fixed_arr {
el_stype.trim_string_left('_v_') + '*'
} else {
el_stype
}
g.channel_definitions.writeln(' g.channel_definitions.writeln('
static inline ${el_stype} __${sym.cname}_popval(${sym.cname} ch) { static inline ${el_stype} __${sym.cname}_popval(${sym.cname} ch) {
${el_stype} val; ${el_stype} val;
sync__Channel_try_pop_priv(ch, &val, false); sync__Channel_try_pop_priv(ch, ${val_arg_pop}, false);
return val; return val;
}') }')
g.channel_definitions.writeln(' g.channel_definitions.writeln('
static inline void __${sym.cname}_pushval(${sym.cname} ch, ${el_stype} val) { static inline void __${sym.cname}_pushval(${sym.cname} ch, ${push_arg} val) {
sync__Channel_try_push_priv(ch, &val, false); sync__Channel_try_push_priv(ch, ${val_arg_push}, false);
}') }')
} }
} }

View File

@ -73,7 +73,7 @@ fn (mut p Parser) parse_array_type(expecting token.Kind, is_option bool) ast.Typ
p.error_with_pos('fixed size cannot be zero or negative', size_expr.pos()) p.error_with_pos('fixed size cannot be zero or negative', size_expr.pos())
} }
idx := p.table.find_or_register_array_fixed(elem_type, fixed_size, size_expr, idx := p.table.find_or_register_array_fixed(elem_type, fixed_size, size_expr,
!is_option && p.inside_fn_return) !is_option && (p.inside_fn_return || p.inside_chan_decl))
if elem_type.has_flag(.generic) { if elem_type.has_flag(.generic) {
return ast.new_type(idx).set_flag(.generic) return ast.new_type(idx).set_flag(.generic)
} }
@ -162,8 +162,10 @@ fn (mut p Parser) parse_chan_type() ast.Type {
} }
p.register_auto_import('sync') p.register_auto_import('sync')
p.next() p.next()
p.inside_chan_decl = true
is_mut := p.tok.kind == .key_mut is_mut := p.tok.kind == .key_mut
elem_type := p.parse_type() elem_type := p.parse_type()
p.inside_chan_decl = false
idx := p.table.find_or_register_chan(elem_type, is_mut) idx := p.table.find_or_register_chan(elem_type, is_mut)
if elem_type.has_flag(.generic) { if elem_type.has_flag(.generic) {
return ast.new_type(idx).set_flag(.generic) return ast.new_type(idx).set_flag(.generic)

View File

@ -64,6 +64,7 @@ mut:
inside_struct_attr_decl bool inside_struct_attr_decl bool
inside_map_init bool inside_map_init bool
inside_orm bool inside_orm bool
inside_chan_decl bool
or_is_handled bool // ignore `or` in this expression or_is_handled bool // ignore `or` in this expression
builtin_mod bool // are we in the `builtin` module? builtin_mod bool // are we in the `builtin` module?
mod string // current module name mod string // current module name