checker: fix missing option and result wrong type return type definition (#21626)

This commit is contained in:
Felipe Pena 2024-06-02 11:14:51 -03:00 committed by GitHub
parent 1096173bd3
commit 6d2c3a9caa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 25 additions and 4 deletions

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/option_and_result_err.vv:1:11: error: the type must be Option or Result
1 | fn foo() !?i32 {
| ^
2 | return 6
3 | }

View File

@ -0,0 +1,3 @@
fn foo() !?i32 {
return 6
}

View File

@ -465,9 +465,22 @@ fn (mut p Parser) parse_type() ast.Type {
if p.tok.kind == .question {
p.next()
is_option = true
if p.tok.kind == .not {
p.next()
is_result = true
}
} else if p.tok.kind == .not {
p.next()
is_result = true
if p.tok.kind == .question {
p.next()
is_option = true
}
}
if is_option && is_result {
p.error_with_pos('the type must be Option or Result', p.prev_tok.pos())
return 0
}
if is_option || is_result {

View File

@ -1,5 +1,5 @@
vlib/v/parser/tests/option_result_err.vv:2:2: error: invalid expression: unexpected keyword `return`
vlib/v/parser/tests/option_result_err.vv:1:11: error: the type must be Option or Result
1 | fn abc() ?!string {
| ^
2 | return ''
| ~~~~~~
3 | }

View File

@ -1,5 +1,5 @@
vlib/v/parser/tests/result_option_err.vv:2:2: error: invalid expression: unexpected keyword `return`
vlib/v/parser/tests/result_option_err.vv:1:11: error: the type must be Option or Result
1 | fn abc() ?!string {
| ^
2 | return ''
| ~~~~~~
3 | }