From bba7cfee0ba2d839aa32871437d13227985a497e Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 28 Feb 2023 21:08:18 -0300 Subject: [PATCH] checker: check or-block used on non-option value (#17444) --- vlib/v/checker/checker.v | 17 +++++++++++++---- .../v/checker/tests/or_block_non_option_err.out | 7 +++++++ vlib/v/checker/tests/or_block_non_option_err.vv | 5 +++++ 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 vlib/v/checker/tests/or_block_non_option_err.out create mode 100644 vlib/v/checker/tests/or_block_non_option_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 5dcc6abffa..b79a445038 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3134,8 +3134,12 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type { info := node.info as ast.IdentVar // Got a var with type T, return current generic type if node.or_expr.kind != .absent { - if !info.typ.has_flag(.option) && node.or_expr.kind == .propagate_option { - c.error('cannot use `?` on non-option variable', node.pos) + if !info.typ.has_flag(.option) { + if node.or_expr.kind == .propagate_option { + c.error('cannot use `?` on non-option variable', node.pos) + } else if node.or_expr.kind == .block { + c.error('cannot use `or {}` block on non-option variable', node.pos) + } } unwrapped_typ := info.typ.clear_flag(.option).clear_flag(.result) c.expected_or_type = unwrapped_typ @@ -3228,8 +3232,13 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type { if node.or_expr.kind == .absent { return typ.clear_flag(.result) } - if !typ.has_flag(.option) && node.or_expr.kind == .propagate_option { - c.error('cannot use `?` on non-option variable', node.pos) + if !typ.has_flag(.option) { + if node.or_expr.kind == .propagate_option { + c.error('cannot use `?` on non-option variable', node.pos) + } else if node.or_expr.kind == .block { + c.error('cannot use `or {}` block on non-option variable', + node.pos) + } } unwrapped_typ := typ.clear_flag(.option).clear_flag(.result) c.expected_or_type = unwrapped_typ diff --git a/vlib/v/checker/tests/or_block_non_option_err.out b/vlib/v/checker/tests/or_block_non_option_err.out new file mode 100644 index 0000000000..c5da150e7e --- /dev/null +++ b/vlib/v/checker/tests/or_block_non_option_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/or_block_non_option_err.vv:3:5: error: cannot use `or {}` block on non-option variable + 1 | name := 100 + 2 | + 3 | _ = name or { + | ~~~~ + 4 | 100 + 5 | } diff --git a/vlib/v/checker/tests/or_block_non_option_err.vv b/vlib/v/checker/tests/or_block_non_option_err.vv new file mode 100644 index 0000000000..f895507f17 --- /dev/null +++ b/vlib/v/checker/tests/or_block_non_option_err.vv @@ -0,0 +1,5 @@ +name := 100 + +_ = name or { + 100 +} \ No newline at end of file