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

checker: fix returning error on reference results (#14313)

This commit is contained in:
Daniel Däschle 2022-05-05 16:02:49 +02:00 committed by GitHub
parent b6058bfd6e
commit 89fe82b732
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View File

@ -24,6 +24,7 @@ pub fn (mut c Checker) return_stmt(mut node ast.Return) {
return
}
exp_is_optional := expected_type.has_flag(.optional)
exp_is_result := expected_type.has_flag(.result)
mut expected_types := [expected_type]
if expected_type_sym.info is ast.MultiReturn {
expected_types = expected_type_sym.info.types
@ -72,11 +73,13 @@ pub fn (mut c Checker) return_stmt(mut node ast.Return) {
}
}
}
// allow `none` & `error` return types for function that returns optional
// allow `none` & `error` return types for function that returns option or result
option_type_idx := c.table.type_idxs['Option']
result_type_idx := c.table.type_idxs['_result']
got_types_0_idx := got_types[0].idx()
if exp_is_optional
&& got_types_0_idx in [ast.none_type_idx, ast.error_type_idx, option_type_idx] {
if (exp_is_optional
&& got_types_0_idx in [ast.none_type_idx, ast.error_type_idx, option_type_idx])
|| (exp_is_result && got_types_0_idx in [ast.error_type_idx, result_type_idx]) {
if got_types_0_idx == ast.none_type_idx && expected_type == ast.ovoid_type {
c.error('returning `none` in functions, that have a `?` result type is not allowed anymore, either `return error(message)` or just `return` instead',
node.pos)

View File

@ -73,3 +73,11 @@ fn unsafe_return_error() !int {
fn test_unsafe_return_error() {
unsafe_return_error() or { assert err.msg() == 'abc' }
}
fn return_reference_type(path string) !&string {
if path.len == 0 {
return error('vfopen called with ""')
}
str := ''
return &str
}