checker: add checks and test for ~ operator

pull/5360/head
Swastik Baranwal 2020-06-12 15:35:20 +05:30 committed by GitHub
parent b4ad174b7f
commit 56ae3797dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 0 deletions

View File

@ -1952,6 +1952,9 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
if it.op == .mul && right_type.is_ptr() {
return right_type.deref()
}
if it.op == .bit_not && !right_type.is_int(){
c.error('operator ~ only defined on int types', it.pos)
}
if it.op == .not && right_type != table.bool_type_idx {
c.error('! operator can only be used with bool types', it.pos)
}

View File

@ -0,0 +1,26 @@
vlib/v/checker/tests/invert_other_types_bits_error.v:2:13: error: operator ~ only defined on int types
1 | fn main() {
2 | println(~3.0)
| ^
3 | println(~10.5)
4 | println(~'2')
vlib/v/checker/tests/invert_other_types_bits_error.v:3:13: error: operator ~ only defined on int types
1 | fn main() {
2 | println(~3.0)
3 | println(~10.5)
| ^
4 | println(~'2')
5 | println(~[2, 4, 6])
vlib/v/checker/tests/invert_other_types_bits_error.v:4:13: error: operator ~ only defined on int types
2 | println(~3.0)
3 | println(~10.5)
4 | println(~'2')
| ^
5 | println(~[2, 4, 6])
6 | }
vlib/v/checker/tests/invert_other_types_bits_error.v:5:13: error: operator ~ only defined on int types
3 | println(~10.5)
4 | println(~'2')
5 | println(~[2, 4, 6])
| ^
6 | }

View File

@ -0,0 +1,6 @@
fn main() {
println(~3.0)
println(~10.5)
println(~'2')
println(~[2, 4, 6])
}