From acc85be5ae9fb311447aa3350e08fb6dd452a374 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Sat, 17 Oct 2020 22:32:30 +0530 Subject: [PATCH] checker: disallow type casting to `none` (#6635) --- vlib/v/checker/checker.v | 3 + vlib/v/checker/tests/none_type_cast_err.out | 75 +++++++++++++++++++++ vlib/v/checker/tests/none_type_cast_err.vv | 13 ++++ 3 files changed, 91 insertions(+) create mode 100644 vlib/v/checker/tests/none_type_cast_err.out create mode 100644 vlib/v/checker/tests/none_type_cast_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index f6b3bdd173..606c5f8547 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2670,6 +2670,9 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type { } } else if node.typ == table.bool_type { c.error('cannot cast to bool - use e.g. `some_int != 0` instead', node.pos) + } else if node.expr_type == table.none_type { + type_name := c.table.type_to_str(node.typ) + c.error('cannot cast `none` to `$type_name`', node.pos) } if node.has_arg { c.expr(node.arg) diff --git a/vlib/v/checker/tests/none_type_cast_err.out b/vlib/v/checker/tests/none_type_cast_err.out new file mode 100644 index 0000000000..2d25d700d7 --- /dev/null +++ b/vlib/v/checker/tests/none_type_cast_err.out @@ -0,0 +1,75 @@ +vlib/v/checker/tests/none_type_cast_err.vv:2:14: error: cannot cast `none` to `string` + 1 | fn main() { + 2 | _ := string(none) + | ~~~~ + 3 | _ := int(none) + 4 | _ := i8(none) +vlib/v/checker/tests/none_type_cast_err.vv:3:11: error: cannot cast `none` to `int` + 1 | fn main() { + 2 | _ := string(none) + 3 | _ := int(none) + | ~~~~ + 4 | _ := i8(none) + 5 | _ := i16(none) +vlib/v/checker/tests/none_type_cast_err.vv:4:10: error: cannot cast `none` to `i8` + 2 | _ := string(none) + 3 | _ := int(none) + 4 | _ := i8(none) + | ~~~~ + 5 | _ := i16(none) + 6 | _ := i64(none) +vlib/v/checker/tests/none_type_cast_err.vv:5:11: error: cannot cast `none` to `i16` + 3 | _ := int(none) + 4 | _ := i8(none) + 5 | _ := i16(none) + | ~~~~ + 6 | _ := i64(none) + 7 | _ := u16(none) +vlib/v/checker/tests/none_type_cast_err.vv:6:11: error: cannot cast `none` to `i64` + 4 | _ := i8(none) + 5 | _ := i16(none) + 6 | _ := i64(none) + | ~~~~ + 7 | _ := u16(none) + 8 | _ := u32(none) +vlib/v/checker/tests/none_type_cast_err.vv:7:11: error: cannot cast `none` to `u16` + 5 | _ := i16(none) + 6 | _ := i64(none) + 7 | _ := u16(none) + | ~~~~ + 8 | _ := u32(none) + 9 | _ := u64(none) +vlib/v/checker/tests/none_type_cast_err.vv:8:11: error: cannot cast `none` to `u32` + 6 | _ := i64(none) + 7 | _ := u16(none) + 8 | _ := u32(none) + | ~~~~ + 9 | _ := u64(none) + 10 | _ := rune(none) +vlib/v/checker/tests/none_type_cast_err.vv:9:11: error: cannot cast `none` to `u64` + 7 | _ := u16(none) + 8 | _ := u32(none) + 9 | _ := u64(none) + | ~~~~ + 10 | _ := rune(none) + 11 | _ := f32(none) +vlib/v/checker/tests/none_type_cast_err.vv:10:12: error: cannot cast `none` to `rune` + 8 | _ := u32(none) + 9 | _ := u64(none) + 10 | _ := rune(none) + | ~~~~ + 11 | _ := f32(none) + 12 | _ := f64(none) +vlib/v/checker/tests/none_type_cast_err.vv:11:11: error: cannot cast `none` to `f32` + 9 | _ := u64(none) + 10 | _ := rune(none) + 11 | _ := f32(none) + | ~~~~ + 12 | _ := f64(none) + 13 | } +vlib/v/checker/tests/none_type_cast_err.vv:12:11: error: cannot cast `none` to `f64` + 10 | _ := rune(none) + 11 | _ := f32(none) + 12 | _ := f64(none) + | ~~~~ + 13 | } diff --git a/vlib/v/checker/tests/none_type_cast_err.vv b/vlib/v/checker/tests/none_type_cast_err.vv new file mode 100644 index 0000000000..953b36965e --- /dev/null +++ b/vlib/v/checker/tests/none_type_cast_err.vv @@ -0,0 +1,13 @@ +fn main() { + _ := string(none) + _ := int(none) + _ := i8(none) + _ := i16(none) + _ := i64(none) + _ := u16(none) + _ := u32(none) + _ := u64(none) + _ := rune(none) + _ := f32(none) + _ := f64(none) +}