Skip to content

Commit

Permalink
checker: fix missing option and result wrong type return type definit…
Browse files Browse the repository at this point in the history
…ion (#21626)
  • Loading branch information
felipensp authored Jun 2, 2024
1 parent 1096173 commit 6d2c3a9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions vlib/v/checker/tests/option_and_result_err.out
Original file line number Diff line number Diff line change
@@ -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 | }
3 changes: 3 additions & 0 deletions vlib/v/checker/tests/option_and_result_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn foo() !?i32 {
return 6
}
13 changes: 13 additions & 0 deletions vlib/v/parser/parse_type.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/parser/tests/option_result_err.out
Original file line number Diff line number Diff line change
@@ -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 | }
4 changes: 2 additions & 2 deletions vlib/v/parser/tests/result_option_err.out
Original file line number Diff line number Diff line change
@@ -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 | }

0 comments on commit 6d2c3a9

Please sign in to comment.