checker: add more op checks

pull/4540/head
yuyi 2020-04-21 19:26:02 +08:00 committed by GitHub
parent 7c1d6b60c2
commit a8dc0ccbcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
67 changed files with 309 additions and 14 deletions

View File

@ -313,9 +313,22 @@ pub fn (c mut Checker) infix_expr(infix_expr mut ast.InfixExpr) table.Type {
}
if infix_expr.op == .mod {
if left.is_int() && !right.is_int() {
c.error('right type of `${infix_expr.op.str()}` cannot be non-integer type $right.name', infix_expr.right.position())
c.error('mismatched types `$left.name` and `$right.name`', infix_expr.right.position())
} else if !left.is_int() && right.is_int() {
c.error('left type of `${infix_expr.op.str()}` cannot be non-integer type $left.name', infix_expr.left.position())
c.error('mismatched types `$left.name` and `$right.name`', infix_expr.left.position())
} else if left.kind in [.f32, .f64, .string, .array, .array_fixed, .map, .struct_] &&
!left.has_method(infix_expr.op.str()) {
c.error('mismatched types `$left.name` and `$right.name`', infix_expr.left.position())
} else if right.kind in [.f32, .f64, .string, .array, .array_fixed, .map, .struct_] &&
!right.has_method(infix_expr.op.str()) {
c.error('mismatched types `$left.name` and `$right.name`', infix_expr.right.position())
}
}
if infix_expr.op in [.plus, .minus, .mul, .div] {
if left.kind in [.array, .array_fixed, .map, .struct_] && !left.has_method(infix_expr.op.str()) {
c.error('mismatched types `$left.name` and `$right.name`', infix_expr.left.position())
} else if right.kind in [.array, .array_fixed, .map, .struct_] && !right.has_method(infix_expr.op.str()) {
c.error('mismatched types `$left.name` and `$right.name`', infix_expr.right.position())
}
}
if left_type == table.bool_type && !(infix_expr.op in [.eq, .ne, .logical_or, .and]) {

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/add_op_wrong_left_type_err_a.v:3:5: error: mismatched types `A` and `int`
1| struct A{}
2| fn main() {
3| A{} + 10
~~~
4| }

View File

@ -0,0 +1,4 @@
struct A{}
fn main() {
A{} + 10
}

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/inout/add_op_wrong_left_type_err_b.v:2:5: error: mismatched types `array_int` and `int`
1| fn main() {
2| [1,2,3] + 10
~~~~~~~
3| }

View File

@ -0,0 +1,3 @@
fn main() {
[1,2,3] + 10
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/add_op_wrong_left_type_err_c.v:3:5: error: mismatched types `map_string_int` and `int`
1| fn main() {
2| a := map[string]int
3| a + 10
^
4| }

View File

@ -0,0 +1,4 @@
fn main() {
a := map[string]int
a + 10
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/add_op_wrong_right_type_err_a.v:3:10: error: mismatched types `int` and `A`
1| struct A{}
2| fn main() {
3| 10 + A{}
~~~
4| }

View File

@ -0,0 +1,4 @@
struct A{}
fn main() {
10 + A{}
}

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/inout/add_op_wrong_right_type_err_b.v:2:10: error: mismatched types `int` and `array_int`
1| fn main() {
2| 10 + [1,2,3]
~~~~~~~
3| }

View File

@ -0,0 +1,3 @@
fn main() {
10 + [1,2,3]
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/add_op_wrong_right_type_err_c.v:3:10: error: mismatched types `int` and `map_string_int`
1| fn main() {
2| a := map[string]int
3| 10 + a
^
4| }

View File

@ -0,0 +1,4 @@
fn main() {
a := map[string]int
10 + a
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/div_op_wrong_left_type_err_a.v:3:5: error: mismatched types `A` and `int`
1| struct A{}
2| fn main() {
3| A{} / 10
~~~
4| }

View File

@ -0,0 +1,4 @@
struct A{}
fn main() {
A{} / 10
}

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/inout/div_op_wrong_left_type_err_b.v:2:5: error: mismatched types `array_int` and `int`
1| fn main() {
2| [1,2,3] / 10
~~~~~~~
3| }

View File

@ -0,0 +1,3 @@
fn main() {
[1,2,3] / 10
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/div_op_wrong_left_type_err_c.v:3:5: error: mismatched types `map_string_int` and `int`
1| fn main() {
2| a := map[string]int
3| a / 10
^
4| }

View File

@ -0,0 +1,4 @@
fn main() {
a := map[string]int
a / 10
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/div_op_wrong_right_type_err_a.v:3:10: error: mismatched types `int` and `A`
1| struct A{}
2| fn main() {
3| 10 / A{}
~~~
4| }

View File

@ -0,0 +1,4 @@
struct A{}
fn main() {
10 / A{}
}

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/inout/div_op_wrong_right_type_err_b.v:2:10: error: mismatched types `int` and `array_int`
1| fn main() {
2| 10 / [1,2,3]
~~~~~~~
3| }

View File

@ -0,0 +1,3 @@
fn main() {
10 / [1,2,3]
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/div_op_wrong_right_type_err_c.v:3:10: error: mismatched types `int` and `map_string_int`
1| fn main() {
2| a := map[string]int
3| 10 / a
^
4| }

View File

@ -0,0 +1,4 @@
fn main() {
a := map[string]int
10 / a
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/minus_op_wrong_left_type_err_a.v:3:5: error: mismatched types `A` and `int`
1| struct A{}
2| fn main() {
3| A{} - 10
~~~
4| }

View File

@ -0,0 +1,4 @@
struct A{}
fn main() {
A{} - 10
}

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/inout/minus_op_wrong_left_type_err_b.v:2:5: error: mismatched types `array_int` and `int`
1| fn main() {
2| [1,2,3] - 10
~~~~~~~
3| }

View File

@ -0,0 +1,3 @@
fn main() {
[1,2,3] - 10
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/minus_op_wrong_left_type_err_c.v:3:5: error: mismatched types `map_string_int` and `int`
1| fn main() {
2| a := map[string]int
3| a - 10
^
4| }

View File

@ -0,0 +1,4 @@
fn main() {
a := map[string]int
a - 10
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/minus_op_wrong_right_type_err_a.v:3:10: error: mismatched types `int` and `A`
1| struct A{}
2| fn main() {
3| 10 - A{}
~~~
4| }

View File

@ -0,0 +1,4 @@
struct A{}
fn main() {
10 - A{}
}

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/inout/minus_op_wrong_right_type_err_b.v:2:10: error: mismatched types `int` and `array_int`
1| fn main() {
2| 10 - [1,2,3]
~~~~~~~
3| }

View File

@ -0,0 +1,3 @@
fn main() {
10 - [1,2,3]
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/minus_op_wrong_right_type_err_c.v:3:10: error: mismatched types `int` and `map_string_int`
1| fn main() {
2| a := map[string]int
3| 10 - a
^
4| }

View File

@ -0,0 +1,4 @@
fn main() {
a := map[string]int
10 - a
}

View File

@ -1,5 +0,0 @@
vlib/v/checker/tests/inout/mod_op_wrong_left_type_err.v:2:2: error: left type of `%` cannot be non-integer type f64
1| fn main() {
2| 0.5 % 1
~~~
3| }

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/inout/mod_op_wrong_left_type_err_a.v:2:2: error: mismatched types `f64` and `int`
1| fn main() {
2| 0.5 % 1
~~~
3| }

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/inout/mod_op_wrong_left_type_err_b.v:2:2: error: mismatched types `array_int` and `int`
1| fn main() {
2| [1,2,3] % 1
~~~~~~~
3| }

View File

@ -0,0 +1,3 @@
fn main() {
[1,2,3] % 1
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/mod_op_wrong_left_type_err_c.v:4:2: error: mismatched types `A` and `int`
2| fn main() {
3| a := A{}
4| a % 1
^
5| }

View File

@ -0,0 +1,5 @@
struct A{}
fn main() {
a := A{}
a % 1
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/mod_op_wrong_left_type_err_d.v:3:2: error: mismatched types `map_string_int` and `int`
1| fn main() {
2| a := map[string]int
3| a % 1
^
4| }

View File

@ -0,0 +1,4 @@
fn main() {
a := map[string]int
a % 1
}

View File

@ -1,5 +0,0 @@
vlib/v/checker/tests/inout/mod_op_wrong_right_type_err.v:2:6: error: right type of `%` cannot be non-integer type f64
1| fn main() {
2| 1 % 0.5
~~~
3| }

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/inout/mod_op_wrong_right_type_err_a.v:2:6: error: mismatched types `int` and `f64`
1| fn main() {
2| 1 % 0.5
~~~
3| }

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/inout/mod_op_wrong_right_type_err_b.v:2:6: error: mismatched types `int` and `array_int`
1| fn main() {
2| 1 % [1,2,3]
~~~~~~~
3| }

View File

@ -0,0 +1,3 @@
fn main() {
1 % [1,2,3]
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/mod_op_wrong_right_type_err_c.v:4:6: error: mismatched types `int` and `A`
2| fn main() {
3| a := A{}
4| 1 % a
^
5| }

View File

@ -0,0 +1,5 @@
struct A{}
fn main() {
a := A{}
1 % a
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/mod_op_wrong_right_type_err_d.v:3:6: error: mismatched types `int` and `map_string_int`
1| fn main() {
2| a := map[string]int
3| 1 % a
^
4| }

View File

@ -0,0 +1,4 @@
fn main() {
a := map[string]int
1 % a
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/mul_op_wrong_left_type_err_a.v:3:5: error: mismatched types `A` and `int`
1| struct A{}
2| fn main() {
3| A{} * 10
~~~
4| }

View File

@ -0,0 +1,4 @@
struct A{}
fn main() {
A{} * 10
}

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/inout/mul_op_wrong_left_type_err_b.v:2:5: error: mismatched types `array_int` and `int`
1| fn main() {
2| [1,2,3] * 10
~~~~~~~
3| }

View File

@ -0,0 +1,3 @@
fn main() {
[1,2,3] * 10
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/mul_op_wrong_left_type_err_c.v:3:5: error: mismatched types `map_string_int` and `int`
1| fn main() {
2| a := map[string]int
3| a * 10
^
4| }

View File

@ -0,0 +1,4 @@
fn main() {
a := map[string]int
a * 10
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/mul_op_wrong_right_type_err_a.v:3:10: error: mismatched types `int` and `A`
1| struct A{}
2| fn main() {
3| 10 * A{}
~~~
4| }

View File

@ -0,0 +1,4 @@
struct A{}
fn main() {
10 * A{}
}

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/inout/mul_op_wrong_right_type_err_b.v:2:10: error: mismatched types `int` and `array_int`
1| fn main() {
2| 10 * [1,2,3]
~~~~~~~
3| }

View File

@ -0,0 +1,3 @@
fn main() {
10 * [1,2,3]
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/inout/mul_op_wrong_right_type_err_c.v:3:10: error: mismatched types `int` and `map_string_int`
1| fn main() {
2| a := map[string]int
3| 10 * a
^
4| }

View File

@ -0,0 +1,4 @@
fn main() {
a := map[string]int
10 * a
}