diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index f471a86869..38ed4e446f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -777,9 +777,9 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type { match infix_expr.op { // .eq, .ne, .gt, .lt, .ge, .le, .and, .logical_or, .dot, .key_as, .right_shift {} .eq, .ne { - is_alias_eq_struct := left.kind == .alias && right.kind == .struct_ - is_struct_eq_alias := left.kind == .struct_ && right.kind == .alias - if is_alias_eq_struct || is_struct_eq_alias { + is_mismatch := (left.kind == .alias && right.kind in [.struct_, .array]) + || (right.kind == .alias && left.kind in [.struct_, .array]) + if is_mismatch { c.error('possible type mismatch of compared values of `$infix_expr.op` operation', infix_expr.pos) } diff --git a/vlib/v/checker/tests/eq_ne_op_wrong_type_err.out b/vlib/v/checker/tests/eq_ne_op_wrong_type_err.out new file mode 100644 index 0000000000..08a0cefd34 --- /dev/null +++ b/vlib/v/checker/tests/eq_ne_op_wrong_type_err.out @@ -0,0 +1,83 @@ +vlib/v/checker/tests/eq_ne_op_wrong_type_err.vv:10:16: error: infix expr: cannot use `int literal` (right expression) as `Aaa` + 8 | + 9 | fn main() { + 10 | println(Aaa{} == 10) + | ~~ + 11 | println(10 == Aaa{}) + 12 | println(Aaa{} != 10) +vlib/v/checker/tests/eq_ne_op_wrong_type_err.vv:11:13: error: infix expr: cannot use `Aaa` (right expression) as `int literal` + 9 | fn main() { + 10 | println(Aaa{} == 10) + 11 | println(10 == Aaa{}) + | ~~ + 12 | println(Aaa{} != 10) + 13 | println(10 != Aaa{}) +vlib/v/checker/tests/eq_ne_op_wrong_type_err.vv:12:16: error: infix expr: cannot use `int literal` (right expression) as `Aaa` + 10 | println(Aaa{} == 10) + 11 | println(10 == Aaa{}) + 12 | println(Aaa{} != 10) + | ~~ + 13 | println(10 != Aaa{}) + 14 | +vlib/v/checker/tests/eq_ne_op_wrong_type_err.vv:13:13: error: infix expr: cannot use `Aaa` (right expression) as `int literal` + 11 | println(10 == Aaa{}) + 12 | println(Aaa{} != 10) + 13 | println(10 != Aaa{}) + | ~~ + 14 | + 15 | println(Aaa{0} == AAaa{0}) +vlib/v/checker/tests/eq_ne_op_wrong_type_err.vv:15:17: error: possible type mismatch of compared values of `==` operation + 13 | println(10 != Aaa{}) + 14 | + 15 | println(Aaa{0} == AAaa{0}) + | ~~ + 16 | println(AAaa{0} == Aaa{0}) + 17 | println(AAaa{1} != Aaa{1}) +vlib/v/checker/tests/eq_ne_op_wrong_type_err.vv:16:18: error: possible type mismatch of compared values of `==` operation + 14 | + 15 | println(Aaa{0} == AAaa{0}) + 16 | println(AAaa{0} == Aaa{0}) + | ~~ + 17 | println(AAaa{1} != Aaa{1}) + 18 | println(Aaa{1} != AAaa{1}) +vlib/v/checker/tests/eq_ne_op_wrong_type_err.vv:17:18: error: possible type mismatch of compared values of `!=` operation + 15 | println(Aaa{0} == AAaa{0}) + 16 | println(AAaa{0} == Aaa{0}) + 17 | println(AAaa{1} != Aaa{1}) + | ~~ + 18 | println(Aaa{1} != AAaa{1}) + 19 | +vlib/v/checker/tests/eq_ne_op_wrong_type_err.vv:18:17: error: possible type mismatch of compared values of `!=` operation + 16 | println(AAaa{0} == Aaa{0}) + 17 | println(AAaa{1} != Aaa{1}) + 18 | println(Aaa{1} != AAaa{1}) + | ~~ + 19 | + 20 | arr := Arr([0]) +vlib/v/checker/tests/eq_ne_op_wrong_type_err.vv:21:14: error: possible type mismatch of compared values of `==` operation + 19 | + 20 | arr := Arr([0]) + 21 | println(arr == [0]) + | ~~ + 22 | println([1] == arr) + 23 | println(arr != [0]) +vlib/v/checker/tests/eq_ne_op_wrong_type_err.vv:22:14: error: possible type mismatch of compared values of `==` operation + 20 | arr := Arr([0]) + 21 | println(arr == [0]) + 22 | println([1] == arr) + | ~~ + 23 | println(arr != [0]) + 24 | println([1] != arr) +vlib/v/checker/tests/eq_ne_op_wrong_type_err.vv:23:14: error: possible type mismatch of compared values of `!=` operation + 21 | println(arr == [0]) + 22 | println([1] == arr) + 23 | println(arr != [0]) + | ~~ + 24 | println([1] != arr) + 25 | } +vlib/v/checker/tests/eq_ne_op_wrong_type_err.vv:24:14: error: possible type mismatch of compared values of `!=` operation + 22 | println([1] == arr) + 23 | println(arr != [0]) + 24 | println([1] != arr) + | ~~ + 25 | } diff --git a/vlib/v/checker/tests/eq_ne_op_wrong_type_err.vv b/vlib/v/checker/tests/eq_ne_op_wrong_type_err.vv new file mode 100644 index 0000000000..a32f38df56 --- /dev/null +++ b/vlib/v/checker/tests/eq_ne_op_wrong_type_err.vv @@ -0,0 +1,25 @@ +struct Aaa { + i int +} + +type AAaa = Aaa + +type Arr = []int + +fn main() { + println(Aaa{} == 10) + println(10 == Aaa{}) + println(Aaa{} != 10) + println(10 != Aaa{}) + + println(Aaa{0} == AAaa{0}) + println(AAaa{0} == Aaa{0}) + println(AAaa{1} != Aaa{1}) + println(Aaa{1} != AAaa{1}) + + arr := Arr([0]) + println(arr == [0]) + println([1] == arr) + println(arr != [0]) + println([1] != arr) +}