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

cgen, checker: allow a << none, where a is []?&int (#18539)

This commit is contained in:
Felipe Pena 2023-06-24 14:54:49 -03:00 committed by GitHub
parent 11fa28edff
commit 752e4c2e47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 5 deletions

View File

@ -522,6 +522,9 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
} else if c.check_types(unwrapped_right_type, c.unwrap_generic(left_type)) {
return ast.void_type
}
if left_value_type.has_flag(.option) && right_type == ast.none_type {
return ast.void_type
}
c.error('cannot append `${right_sym.name}` to `${left_sym.name}`', right_pos)
return ast.void_type
} else {

View File

@ -578,10 +578,14 @@ fn (mut g Gen) gen_str_for_array(info ast.Array, styp string, str_fn_name string
deref, deref_label := deref_kind(str_method_expects_ptr, is_elem_ptr,
typ)
g.auto_str_funcs.writeln('\t\tstring x = _SLIT("nil");')
g.auto_str_funcs.writeln('\t\tif (it != 0) {')
if !typ.has_flag(.option) {
g.auto_str_funcs.writeln('\t\tif (it != 0) {')
}
g.auto_str_funcs.writeln('\t\t\tstrings__Builder_write_string(&sb, _SLIT("${deref_label}"));')
g.auto_str_funcs.writeln('\t\t\tx = ${elem_str_fn_name}(${deref}it);')
g.auto_str_funcs.writeln('\t\t}')
if !typ.has_flag(.option) {
g.auto_str_funcs.writeln('\t\t}')
}
} else {
g.auto_str_funcs.writeln('\t\tstring x = indent_${elem_str_fn_name}(it, indent_count);')
}
@ -610,10 +614,14 @@ fn (mut g Gen) gen_str_for_array(info ast.Array, styp string, str_fn_name string
deref, deref_label := deref_kind(str_method_expects_ptr, is_elem_ptr, typ)
if is_elem_ptr {
g.auto_str_funcs.writeln('\t\tstring x = _SLIT("nil");')
g.auto_str_funcs.writeln('\t\tif (it != 0) {')
if !typ.has_flag(.option) {
g.auto_str_funcs.writeln('\t\tif (it != 0) {')
}
g.auto_str_funcs.writeln('\t\t\tstrings__Builder_write_string(&sb, _SLIT("${deref_label}"));')
g.auto_str_funcs.writeln('\t\t\tx = ${elem_str_fn_name}(${deref}it);')
g.auto_str_funcs.writeln('\t\t}')
if !typ.has_flag(.option) {
g.auto_str_funcs.writeln('\t\t}')
}
} else {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write_string(&sb, _SLIT("${deref_label}"));')
g.auto_str_funcs.writeln('\t\tstring x = ${elem_str_fn_name}(${deref}it);')

View File

@ -990,7 +990,7 @@ fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) {
}
fn (mut g Gen) gen_is_none_check(node ast.InfixExpr) {
if node.left in [ast.Ident, ast.SelectorExpr] {
if node.left in [ast.Ident, ast.SelectorExpr, ast.IndexExpr] {
old_inside_opt_or_res := g.inside_opt_or_res
g.inside_opt_or_res = true
g.expr(node.left)

View File

@ -0,0 +1,11 @@
fn test_main() {
mut a := []?&int{}
a << ?&int(none)
a << none
dump(a[0])
dump(a[1])
println(a)
assert a[0] == none
assert a[1] == none
}