checker: make `[] == ArrayAlias([])` an error (#8562)

pull/8591/head
zakuro 2021-02-06 00:49:40 +09:00 committed by GitHub
parent e78c30d181
commit a94228bb16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 111 additions and 3 deletions

View File

@ -777,9 +777,9 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type {
match infix_expr.op { match infix_expr.op {
// .eq, .ne, .gt, .lt, .ge, .le, .and, .logical_or, .dot, .key_as, .right_shift {} // .eq, .ne, .gt, .lt, .ge, .le, .and, .logical_or, .dot, .key_as, .right_shift {}
.eq, .ne { .eq, .ne {
is_alias_eq_struct := left.kind == .alias && right.kind == .struct_ is_mismatch := (left.kind == .alias && right.kind in [.struct_, .array])
is_struct_eq_alias := left.kind == .struct_ && right.kind == .alias || (right.kind == .alias && left.kind in [.struct_, .array])
if is_alias_eq_struct || is_struct_eq_alias { if is_mismatch {
c.error('possible type mismatch of compared values of `$infix_expr.op` operation', c.error('possible type mismatch of compared values of `$infix_expr.op` operation',
infix_expr.pos) infix_expr.pos)
} }

View File

@ -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 | }

View File

@ -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)
}