Skip to content

Commit

Permalink
parser: add a more precise and helpful error message for int! and `…
Browse files Browse the repository at this point in the history
…int?` result type syntax (#20061)
  • Loading branch information
felipensp committed Dec 2, 2023
1 parent 64733ed commit ab04a58
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions vlib/v/checker/tests/wrong_option_type.out
@@ -0,0 +1,5 @@
vlib/v/checker/tests/wrong_option_type.vv:1:10: error: wrong syntax, it must be ?int, not int?
1 | fn foo() int?{
| ~~~
2 | return 0
3 | }
3 changes: 3 additions & 0 deletions vlib/v/checker/tests/wrong_option_type.vv
@@ -0,0 +1,3 @@
fn foo() int?{
return 0
}
5 changes: 5 additions & 0 deletions vlib/v/checker/tests/wrong_result_type.out
@@ -0,0 +1,5 @@
vlib/v/checker/tests/wrong_result_type.vv:1:10: error: wrong syntax, it must be !int, not int!
1 | fn foo() int!{
| ~~~
2 | return 0
3 | }
3 changes: 3 additions & 0 deletions vlib/v/checker/tests/wrong_result_type.vv
@@ -0,0 +1,3 @@
fn foo() int!{
return 0
}
7 changes: 7 additions & 0 deletions vlib/v/parser/fn.v
Expand Up @@ -440,7 +440,14 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
return_type = p.parse_type()
p.inside_fn_return = false
return_type_pos = return_type_pos.extend(p.prev_tok.pos())

if p.tok.kind in [.question, .not] {
ret_type_sym := p.table.sym(return_type)
p.error_with_pos('wrong syntax, it must be ${p.tok.kind}${ret_type_sym.name}, not ${ret_type_sym.name}${p.tok.kind}',
return_type_pos)
}
}

if p.tok.kind == .comma {
mr_pos := return_type_pos.extend(p.peek_tok.pos())
p.error_with_pos('multiple return types in function declaration must use parentheses, e.g. (int, string)',
Expand Down

0 comments on commit ab04a58

Please sign in to comment.