diff --git a/vlib/os/inode.v b/vlib/os/inode.v index 4145ff8a2b..88ff85e1bb 100644 --- a/vlib/os/inode.v +++ b/vlib/os/inode.v @@ -58,38 +58,38 @@ pub fn inode(path string) FileMode { return FileMode{ typ: typ owner: FilePermission{ - read: bool(attr.st_mode & u32(C.S_IREAD)) - write: bool(attr.st_mode & u32(C.S_IWRITE)) - execute: bool(attr.st_mode & u32(C.S_IEXEC)) + read: (attr.st_mode & u32(C.S_IREAD)) != 0 + write: (attr.st_mode & u32(C.S_IWRITE)) != 0 + execute: (attr.st_mode & u32(C.S_IEXEC)) != 0 } group: FilePermission{ - read: bool(attr.st_mode & u32(C.S_IREAD)) - write: bool(attr.st_mode & u32(C.S_IWRITE)) - execute: bool(attr.st_mode & u32(C.S_IEXEC)) + read: (attr.st_mode & u32(C.S_IREAD)) != 0 + write: (attr.st_mode & u32(C.S_IWRITE)) != 0 + execute: (attr.st_mode & u32(C.S_IEXEC)) != 0 } others: FilePermission{ - read: bool(attr.st_mode & u32(C.S_IREAD)) - write: bool(attr.st_mode & u32(C.S_IWRITE)) - execute: bool(attr.st_mode & u32(C.S_IEXEC)) + read: (attr.st_mode & u32(C.S_IREAD)) != 0 + write: (attr.st_mode & u32(C.S_IWRITE)) != 0 + execute: (attr.st_mode & u32(C.S_IEXEC)) != 0 } } } $else { return FileMode{ typ: typ owner: FilePermission{ - read: bool(attr.st_mode & u32(C.S_IRUSR)) - write: bool(attr.st_mode & u32(C.S_IWUSR)) - execute: bool(attr.st_mode & u32(C.S_IXUSR)) + read: (attr.st_mode & u32(C.S_IRUSR)) != 0 + write: (attr.st_mode & u32(C.S_IWUSR)) != 0 + execute: (attr.st_mode & u32(C.S_IXUSR)) != 0 } group: FilePermission{ - read: bool(attr.st_mode & u32(C.S_IRGRP)) - write: bool(attr.st_mode & u32(C.S_IWGRP)) - execute: bool(attr.st_mode & u32(C.S_IXGRP)) + read: (attr.st_mode & u32(C.S_IRGRP)) != 0 + write: (attr.st_mode & u32(C.S_IWGRP)) != 0 + execute: (attr.st_mode & u32(C.S_IXGRP)) != 0 } others: FilePermission{ - read: bool(attr.st_mode & u32(C.S_IROTH)) - write: bool(attr.st_mode & u32(C.S_IWOTH)) - execute: bool(attr.st_mode & u32(C.S_IXOTH)) + read: (attr.st_mode & u32(C.S_IROTH)) != 0 + write: (attr.st_mode & u32(C.S_IWOTH)) != 0 + execute: (attr.st_mode & u32(C.S_IXOTH)) != 0 } } } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index cea93491c7..473513cb80 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2356,6 +2356,8 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type { type_name := c.table.type_to_str(node.expr_type) c.error('cannot cast `$type_name` to struct', node.pos) } + } else if node.typ == table.bool_type { + c.error('cannot cast to bool - use e.g. `some_int != 0` instead', node.pos) } if node.has_arg { c.expr(node.arg) diff --git a/vlib/v/checker/tests/cast_err.out b/vlib/v/checker/tests/cast_err.out new file mode 100644 index 0000000000..ab2794b24c --- /dev/null +++ b/vlib/v/checker/tests/cast_err.out @@ -0,0 +1,20 @@ +vlib/v/checker/tests/cast_err.v:3:11: error: cannot cast to bool - use e.g. `some_int != 0` instead + 1 | fn test_bool_cast() { + 2 | v := 3 + 3 | _ = bool(v) + | ^ + 4 | _ = bool(&v) + 5 | _ = bool([2]) +vlib/v/checker/tests/cast_err.v:4:11: error: cannot cast to bool - use e.g. `some_int != 0` instead + 2 | v := 3 + 3 | _ = bool(v) + 4 | _ = bool(&v) + | ^ + 5 | _ = bool([2]) + 6 | } +vlib/v/checker/tests/cast_err.v:5:11: error: cannot cast to bool - use e.g. `some_int != 0` instead + 3 | _ = bool(v) + 4 | _ = bool(&v) + 5 | _ = bool([2]) + | ~~~ + 6 | } diff --git a/vlib/v/checker/tests/cast_err.vv b/vlib/v/checker/tests/cast_err.vv new file mode 100644 index 0000000000..de0faa1132 --- /dev/null +++ b/vlib/v/checker/tests/cast_err.vv @@ -0,0 +1,6 @@ +fn test_bool_cast() { + v := 3 + _ = bool(v) + _ = bool(&v) + _ = bool([2]) +} diff --git a/vlib/v/tests/conversions_test.v b/vlib/v/tests/conversions_test.v new file mode 100644 index 0000000000..10b6d1961d --- /dev/null +++ b/vlib/v/tests/conversions_test.v @@ -0,0 +1,16 @@ +fn test_conv_to_bool() { + v := 0 + mut b := v != 0 + assert !b + b = u64(&v) != 0 + assert b + // check true -> 1 + assert int(b) == 1 + + // branchless tests (can be important for manual optimization) + arr := [7,8]!! + e := arr[int(b)] + assert e == 8 + b = e < 0 + assert arr[int(b)] == 7 +}