From 9ad7307e318c93758a93074e665b03b6ed5495de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Thu, 19 May 2022 15:04:44 +0200 Subject: [PATCH] checker: forbid optional variable (#14460) --- vlib/v/checker/checker.v | 4 +++ .../v/checker/tests/optional_variable_err.out | 5 ++++ vlib/v/checker/tests/optional_variable_err.vv | 3 +++ vlib/v/gen/c/cgen.v | 4 --- vlib/v/tests/optional_assign_test.v | 25 ------------------- 5 files changed, 12 insertions(+), 29 deletions(-) create mode 100644 vlib/v/checker/tests/optional_variable_err.out create mode 100644 vlib/v/checker/tests/optional_variable_err.vv delete mode 100644 vlib/v/tests/optional_assign_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 308e89b371..b074530dcd 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2312,6 +2312,10 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { mut to_sym := c.table.sym(to_type) // type to be used as cast mut final_to_sym := c.table.final_sym(to_type) + if to_type.has_flag(.optional) { + c.error('casting to optional type is forbidden', node.pos) + } + if (to_sym.is_number() && from_sym.name == 'JS.Number') || (to_sym.is_number() && from_sym.name == 'JS.BigInt') || (to_sym.is_string() && from_sym.name == 'JS.String') diff --git a/vlib/v/checker/tests/optional_variable_err.out b/vlib/v/checker/tests/optional_variable_err.out new file mode 100644 index 0000000000..997774b028 --- /dev/null +++ b/vlib/v/checker/tests/optional_variable_err.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/optional_variable_err.vv:2:7: error: casting to optional type is forbidden + 1 | fn main() { + 2 | _ := ?bool(false) + | ~~~~~~~~~~~~ + 3 | } diff --git a/vlib/v/checker/tests/optional_variable_err.vv b/vlib/v/checker/tests/optional_variable_err.vv new file mode 100644 index 0000000000..1ce984c915 --- /dev/null +++ b/vlib/v/checker/tests/optional_variable_err.vv @@ -0,0 +1,3 @@ +fn main() { + _ := ?bool(false) +} diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index f9ebef0ac9..2ed72513ce 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2243,10 +2243,6 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ g.write('*') } } - if expected_type.has_flag(.optional) && expr is ast.None { - g.gen_optional_error(expected_type, expr) - return - } if expr is ast.IntegerLiteral { if expected_type in [ast.u64_type, ast.u32_type, ast.u16_type] && expr.val[0] != `-` { g.expr(expr) diff --git a/vlib/v/tests/optional_assign_test.v b/vlib/v/tests/optional_assign_test.v deleted file mode 100644 index d191f7b8f6..0000000000 --- a/vlib/v/tests/optional_assign_test.v +++ /dev/null @@ -1,25 +0,0 @@ -type Foo = int | string - -fn test_optional_none_assign() { - x := ?Foo(none) - // assert x.err == none - // assert !x.has_value -} - -fn test_optional_none_assign_nonsumtype() { - x := ?int(none) - // assert x.err == none - // assert !x.has_value -} - -// TODO: make this working next -/* -fn test_optional_value_assign() { - x := ?Foo('test') -} - -fn test_optional_none_reassign() { - mut x := ?Foo('test') - x = none -} -*/