checker: add type check for bit op

pull/4516/head
Ruofan XU 2020-04-20 04:29:45 +08:00 committed by GitHub
parent dbb81b89fd
commit 5edd9cdc3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 0 deletions

View File

@ -310,6 +310,14 @@ pub fn (c mut Checker) infix_expr(infix_expr mut ast.InfixExpr) table.Type {
c.error('infix expr: cannot use `$right.name` (right expression) as `$left.name`',
infix_expr.pos)
}
if infix_expr.op in [.amp, .pipe, .xor] {
if !left.is_int() {
c.error('operator ${infix_expr.op.str()} not defined on left type `$left.name`', infix_expr.pos)
}
else if !right.is_int() {
c.error('operator ${infix_expr.op.str()} not defined on right type `$right.name`', infix_expr.pos)
}
}
if left_type == table.bool_type && !(infix_expr.op in [.eq, .ne, .logical_or, .and]) {
c.error('bool types only have the following operators defined: `==`, `!=`, `||`, and `&&`',
infix_expr.pos)

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/inout/bit_op_wrong_left_type_err.v:2:6: error: operator & not defined on left type `f64`
1| fn main() {
2| 0.5 & 1
^
3| }

View File

@ -0,0 +1,3 @@
fn main() {
0.5 & 1
}

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/inout/bit_op_wrong_right_type_err.v:2:4: error: operator | not defined on right type `f64`
1| fn main() {
2| 1 | 0.5
^
3| }

View File

@ -0,0 +1,3 @@
fn main() {
1 | 0.5
}