diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index c375a5df52..f848f12de7 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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) } diff --git a/vlib/v/checker/tests/invert_other_types_bits_error.out b/vlib/v/checker/tests/invert_other_types_bits_error.out new file mode 100644 index 0000000000..19e3c84970 --- /dev/null +++ b/vlib/v/checker/tests/invert_other_types_bits_error.out @@ -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 | } diff --git a/vlib/v/checker/tests/invert_other_types_bits_error.vv b/vlib/v/checker/tests/invert_other_types_bits_error.vv new file mode 100644 index 0000000000..ad74f6449a --- /dev/null +++ b/vlib/v/checker/tests/invert_other_types_bits_error.vv @@ -0,0 +1,6 @@ +fn main() { + println(~3.0) + println(~10.5) + println(~'2') + println(~[2, 4, 6]) +}