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

checker: check fn type mismatch of return result type fn (#16269)

This commit is contained in:
yuyi 2022-10-31 20:05:43 +08:00 committed by GitHub
parent c3e209a634
commit 339bd0c4b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

View File

@ -381,6 +381,9 @@ pub fn (mut c Checker) check_matching_function_symbols(got_type_sym &ast.TypeSym
if got_fn.return_type.has_flag(.optional) != exp_fn.return_type.has_flag(.optional) { if got_fn.return_type.has_flag(.optional) != exp_fn.return_type.has_flag(.optional) {
return false return false
} }
if got_fn.return_type.has_flag(.result) != exp_fn.return_type.has_flag(.result) {
return false
}
if !c.check_basic(got_fn.return_type, exp_fn.return_type) { if !c.check_basic(got_fn.return_type, exp_fn.return_type) {
return false return false
} }

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/return_result_fn_mismatch.vv:15:10: error: cannot use `fn ()` as `fn () !` in argument 1 to `do_test`
13 | foo() or { return }
14 | }
15 | do_test(test)
| ~~~~
16 | }

View File

@ -0,0 +1,16 @@
type AnonOptionalFun = fn () !
// do_test is used to test functionality
pub fn do_test(cb AnonOptionalFun) {
cb() or { panic(err) }
}
fn foo () ! {
}
fn main() {
test := fn () {
foo() or { return }
}
do_test(test)
}