Skip to content

Commit ab04a58

Browse files
authored
parser: add a more precise and helpful error message for int! and int? result type syntax (#20061)
1 parent 64733ed commit ab04a58

File tree

5 files changed

+23
-0
lines changed

5 files changed

+23
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vlib/v/checker/tests/wrong_option_type.vv:1:10: error: wrong syntax, it must be ?int, not int?
2+
1 | fn foo() int?{
3+
| ~~~
4+
2 | return 0
5+
3 | }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn foo() int?{
2+
return 0
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vlib/v/checker/tests/wrong_result_type.vv:1:10: error: wrong syntax, it must be !int, not int!
2+
1 | fn foo() int!{
3+
| ~~~
4+
2 | return 0
5+
3 | }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn foo() int!{
2+
return 0
3+
}

vlib/v/parser/fn.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,14 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
440440
return_type = p.parse_type()
441441
p.inside_fn_return = false
442442
return_type_pos = return_type_pos.extend(p.prev_tok.pos())
443+
444+
if p.tok.kind in [.question, .not] {
445+
ret_type_sym := p.table.sym(return_type)
446+
p.error_with_pos('wrong syntax, it must be ${p.tok.kind}${ret_type_sym.name}, not ${ret_type_sym.name}${p.tok.kind}',
447+
return_type_pos)
448+
}
443449
}
450+
444451
if p.tok.kind == .comma {
445452
mr_pos := return_type_pos.extend(p.peek_tok.pos())
446453
p.error_with_pos('multiple return types in function declaration must use parentheses, e.g. (int, string)',

0 commit comments

Comments
 (0)