1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: optional return fix

This commit is contained in:
yuyi 2020-06-04 16:40:32 +08:00 committed by GitHub
parent 5ae8853648
commit 8a24d7d723
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View File

@ -1258,6 +1258,11 @@ pub fn (mut c Checker) return_stmt(mut return_stmt ast.Return) {
}
for i, exp_type in expected_types {
got_typ := got_types[i]
if got_typ.flag_is(.optional) &&
(!exp_type.flag_is(.optional) || c.table.type_to_str(got_typ) != c.table.type_to_str(exp_type)) {
pos := return_stmt.exprs[i].position()
c.error('cannot use `${c.table.type_to_str(got_typ)}` as type `${c.table.type_to_str(exp_type)}` in return argument', pos)
}
is_generic := exp_type == table.t_type
ok := if is_generic { c.check_types(got_typ, c.cur_generic_type) || got_typ == exp_type } else { c.check_types(got_typ,
exp_type) }

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/return_optional_err.v:4:13: error: cannot use `?string` as type `string` in return argument
2 |
3 | fn my_func() string {
4 | return os.read_file('/some/file/that/exists.txt') or {panic(err)}
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 | }
6 |

View File

@ -0,0 +1,10 @@
import os
fn my_func() string {
return os.read_file('/some/file/that/exists.txt') or {panic(err)}
}
fn main() {
aa := my_func()
println(aa)
}