mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker, fmt: check infix_expr with 'and' op (#15466)
This commit is contained in:
parent
31067d4a6c
commit
f10ff0353e
@ -20,6 +20,11 @@ pub fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
|
||||
if node.op == .key_is {
|
||||
c.inside_x_is_type = false
|
||||
}
|
||||
if node.op == .amp && left_type.is_bool() && right_type.is_bool() && right_type.is_ptr() {
|
||||
pos := node.pos.extend(node.right.pos())
|
||||
c.error('the right expression should be separated from the `&&` by a space', pos)
|
||||
return ast.bool_type
|
||||
}
|
||||
node.right_type = right_type
|
||||
if left_type.is_number() && !left_type.is_ptr()
|
||||
&& right_type in [ast.int_literal_type, ast.float_literal_type] {
|
||||
|
7
vlib/v/checker/tests/infix_and_op_expr_err.out
Normal file
7
vlib/v/checker/tests/infix_and_op_expr_err.out
Normal file
@ -0,0 +1,7 @@
|
||||
vlib/v/checker/tests/infix_and_op_expr_err.vv:3:17: error: the right expression should be separated from the `&&` by a space
|
||||
1 | fn main() {
|
||||
2 | ok1 := true
|
||||
3 | ok2 := false&&ok1
|
||||
| ~~
|
||||
4 | println(ok2)
|
||||
5 | }
|
5
vlib/v/checker/tests/infix_and_op_expr_err.vv
Normal file
5
vlib/v/checker/tests/infix_and_op_expr_err.vv
Normal file
@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
ok1 := true
|
||||
ok2 := false&&ok1
|
||||
println(ok2)
|
||||
}
|
@ -2055,16 +2055,21 @@ pub fn (mut f Fmt) infix_expr(node ast.InfixExpr) {
|
||||
f.expr(node.left)
|
||||
is_one_val_array_init := node.op in [.key_in, .not_in] && node.right is ast.ArrayInit
|
||||
&& (node.right as ast.ArrayInit).exprs.len == 1
|
||||
is_and := node.op == .amp && f.node_str(node.right).starts_with('&')
|
||||
if is_one_val_array_init {
|
||||
// `var in [val]` => `var == val`
|
||||
op := if node.op == .key_in { ' == ' } else { ' != ' }
|
||||
f.write(op)
|
||||
} else if is_and {
|
||||
f.write(' && ')
|
||||
} else {
|
||||
f.write(' $node.op.str() ')
|
||||
}
|
||||
if is_one_val_array_init {
|
||||
// `var in [val]` => `var == val`
|
||||
f.expr((node.right as ast.ArrayInit).exprs[0])
|
||||
} else if is_and {
|
||||
f.write(f.node_str(node.right).trim_string_left('&'))
|
||||
} else {
|
||||
f.expr(node.right)
|
||||
}
|
||||
|
5
vlib/v/fmt/tests/infix_expr_and_op_expected.vv
Normal file
5
vlib/v/fmt/tests/infix_expr_and_op_expected.vv
Normal file
@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
ok1 := true
|
||||
ok2 := false && ok1
|
||||
println(ok2)
|
||||
}
|
5
vlib/v/fmt/tests/infix_expr_and_op_input.vv
Normal file
5
vlib/v/fmt/tests/infix_expr_and_op_input.vv
Normal file
@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
ok1 := true
|
||||
ok2 := false&&ok1
|
||||
println(ok2)
|
||||
}
|
Loading…
Reference in New Issue
Block a user