From 30da85a4d539d306413e1b1988a77df40204c5ab Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 3 Dec 2020 08:03:17 +0800 Subject: [PATCH] checker: fix optional or_block {none} (#7095) --- vlib/v/checker/check_types.v | 2 +- .../tests/optional_or_block_none_err.out | 7 ++++++ .../tests/optional_or_block_none_err.vv | 22 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/optional_or_block_none_err.out create mode 100644 vlib/v/checker/tests/optional_or_block_none_err.vv diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 24c3487914..5877de6d54 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -24,7 +24,7 @@ pub fn (mut c Checker) check_basic(got table.Type, expected table.Type) bool { return true } if got_idx == table.none_type_idx && expected.has_flag(.optional) { - return true + return false } // allow pointers to be initialized with 0. TODO: use none instead if exp_is_ptr && got_idx == table.int_type_idx { diff --git a/vlib/v/checker/tests/optional_or_block_none_err.out b/vlib/v/checker/tests/optional_or_block_none_err.out new file mode 100644 index 0000000000..ed608c30dd --- /dev/null +++ b/vlib/v/checker/tests/optional_or_block_none_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/optional_or_block_none_err.vv:18:3: error: wrong return type `none` in the `or {}` block, expected `Animal` + 16 | fn main() { + 17 | mut dog := new_animal(9) or { + 18 | none + | ~~~~ + 19 | } + 20 | diff --git a/vlib/v/checker/tests/optional_or_block_none_err.vv b/vlib/v/checker/tests/optional_or_block_none_err.vv new file mode 100644 index 0000000000..c1970a306e --- /dev/null +++ b/vlib/v/checker/tests/optional_or_block_none_err.vv @@ -0,0 +1,22 @@ +module main + +struct Animal { + mut: + height byte +} + +fn new_animal(height byte) ?Animal { + if height < 10 { + return error('Too small to be an animal!') + } + + return Animal{ height: height } +} + +fn main() { + mut dog := new_animal(9) or { + none + } + + println(dog) +}