checker: fix optional or_block {none} (#7095)

pull/7104/head
yuyi 2020-12-03 08:03:17 +08:00 committed by GitHub
parent c1b25dd61d
commit 30da85a4d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 1 deletions

View File

@ -24,7 +24,7 @@ pub fn (mut c Checker) check_basic(got table.Type, expected table.Type) bool {
return true return true
} }
if got_idx == table.none_type_idx && expected.has_flag(.optional) { 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 // allow pointers to be initialized with 0. TODO: use none instead
if exp_is_ptr && got_idx == table.int_type_idx { if exp_is_ptr && got_idx == table.int_type_idx {

View File

@ -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 |

View File

@ -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)
}