Skip to content

Commit 53cfd4a

Browse files
committed
Add optional thin-arrow ret-type for fn-item
1 parent 4bf516e commit 53cfd4a

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

crates/parser/src/grammar.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,18 @@ fn opt_ret_type(p: &mut Parser<'_>) -> bool {
303303
}
304304
}
305305

306+
fn opt_no_arrow_ret_type(p: &mut Parser<'_>) -> bool {
307+
if p.at_ts(PATH_NAME_REF_KINDS) {
308+
let m = p.start();
309+
p.error("missing thin-arrow `->`");
310+
types::type_no_bounds(p);
311+
m.complete(p, RET_TYPE);
312+
true
313+
} else {
314+
false
315+
}
316+
}
317+
306318
fn name_r(p: &mut Parser<'_>, recovery: TokenSet) {
307319
if p.at(IDENT) {
308320
let m = p.start();

crates/parser/src/grammar/items.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,12 @@ fn fn_(p: &mut Parser<'_>, m: Marker) {
422422
// test function_ret_type
423423
// fn foo() {}
424424
// fn bar() -> () {}
425-
opt_ret_type(p);
425+
if !opt_ret_type(p) {
426+
// test_err function_ret_type_missing_arrow
427+
// fn foo() usize {}
428+
// fn bar() super::Foo {}
429+
opt_no_arrow_ret_type(p);
430+
}
426431

427432
// test_err fn_ret_recovery
428433
// fn foo() -> A>]) { let x = 1; }

crates/parser/test_data/generated/runner.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,10 @@ mod err {
783783
run_and_expect_errors("test_data/parser/inline/err/fn_ret_recovery.rs");
784784
}
785785
#[test]
786+
fn function_ret_type_missing_arrow() {
787+
run_and_expect_errors("test_data/parser/inline/err/function_ret_type_missing_arrow.rs");
788+
}
789+
#[test]
786790
fn gen_fn() {
787791
run_and_expect_errors_with_edition(
788792
"test_data/parser/inline/err/gen_fn.rs",
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
SOURCE_FILE
2+
FN
3+
FN_KW "fn"
4+
WHITESPACE " "
5+
NAME
6+
IDENT "foo"
7+
PARAM_LIST
8+
L_PAREN "("
9+
R_PAREN ")"
10+
WHITESPACE " "
11+
RET_TYPE
12+
PATH_TYPE
13+
PATH
14+
PATH_SEGMENT
15+
NAME_REF
16+
IDENT "usize"
17+
WHITESPACE " "
18+
BLOCK_EXPR
19+
STMT_LIST
20+
L_CURLY "{"
21+
R_CURLY "}"
22+
WHITESPACE "\n"
23+
FN
24+
FN_KW "fn"
25+
WHITESPACE " "
26+
NAME
27+
IDENT "bar"
28+
PARAM_LIST
29+
L_PAREN "("
30+
R_PAREN ")"
31+
WHITESPACE " "
32+
RET_TYPE
33+
PATH_TYPE
34+
PATH
35+
PATH
36+
PATH_SEGMENT
37+
NAME_REF
38+
SUPER_KW "super"
39+
COLON2 "::"
40+
PATH_SEGMENT
41+
NAME_REF
42+
IDENT "Foo"
43+
WHITESPACE " "
44+
BLOCK_EXPR
45+
STMT_LIST
46+
L_CURLY "{"
47+
R_CURLY "}"
48+
WHITESPACE "\n"
49+
error 9: missing thin-arrow `->`
50+
error 27: missing thin-arrow `->`
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fn foo() usize {}
2+
fn bar() super::Foo {}

0 commit comments

Comments
 (0)