From 5edd9cdc3a62a3cdb545dc0b883c80649d1f9b92 Mon Sep 17 00:00:00 2001 From: Ruofan XU <47302112+SleepyRoy@users.noreply.github.com> Date: Mon, 20 Apr 2020 04:29:45 +0800 Subject: [PATCH] checker: add type check for bit op --- vlib/v/checker/checker.v | 8 ++++++++ vlib/v/checker/tests/inout/bit_op_wrong_left_type_err.out | 5 +++++ vlib/v/checker/tests/inout/bit_op_wrong_left_type_err.vv | 3 +++ .../v/checker/tests/inout/bit_op_wrong_right_type_err.out | 5 +++++ vlib/v/checker/tests/inout/bit_op_wrong_right_type_err.v | 3 +++ 5 files changed, 24 insertions(+) create mode 100644 vlib/v/checker/tests/inout/bit_op_wrong_left_type_err.out create mode 100644 vlib/v/checker/tests/inout/bit_op_wrong_left_type_err.vv create mode 100644 vlib/v/checker/tests/inout/bit_op_wrong_right_type_err.out create mode 100644 vlib/v/checker/tests/inout/bit_op_wrong_right_type_err.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 6570ed7471..1016e91b2a 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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) diff --git a/vlib/v/checker/tests/inout/bit_op_wrong_left_type_err.out b/vlib/v/checker/tests/inout/bit_op_wrong_left_type_err.out new file mode 100644 index 0000000000..98429e9a88 --- /dev/null +++ b/vlib/v/checker/tests/inout/bit_op_wrong_left_type_err.out @@ -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| } diff --git a/vlib/v/checker/tests/inout/bit_op_wrong_left_type_err.vv b/vlib/v/checker/tests/inout/bit_op_wrong_left_type_err.vv new file mode 100644 index 0000000000..604c8485f3 --- /dev/null +++ b/vlib/v/checker/tests/inout/bit_op_wrong_left_type_err.vv @@ -0,0 +1,3 @@ +fn main() { + 0.5 & 1 +} \ No newline at end of file diff --git a/vlib/v/checker/tests/inout/bit_op_wrong_right_type_err.out b/vlib/v/checker/tests/inout/bit_op_wrong_right_type_err.out new file mode 100644 index 0000000000..c37abc4908 --- /dev/null +++ b/vlib/v/checker/tests/inout/bit_op_wrong_right_type_err.out @@ -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| } diff --git a/vlib/v/checker/tests/inout/bit_op_wrong_right_type_err.v b/vlib/v/checker/tests/inout/bit_op_wrong_right_type_err.v new file mode 100644 index 0000000000..e46d383406 --- /dev/null +++ b/vlib/v/checker/tests/inout/bit_op_wrong_right_type_err.v @@ -0,0 +1,3 @@ +fn main() { + 1 | 0.5 +} \ No newline at end of file