Skip to content

Commit 7eb790a

Browse files
authored
Merge pull request #22170 from ChayimFriedman2/return-attr
fix: Parse `return #[attr] expr`
2 parents 91be466 + 965512e commit 7eb790a

5 files changed

Lines changed: 45 additions & 4 deletions

File tree

crates/parser/src/grammar/expressions.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
mod atom;
22

3-
use crate::grammar::attributes::ATTRIBUTE_FIRST;
4-
53
use super::*;
64

75
pub(super) use atom::{EXPR_RECOVERY_SET, LITERAL_FIRST, literal, parse_asm_expr};
@@ -324,7 +322,7 @@ fn expr_bp(
324322
}
325323

326324
const LHS_FIRST: TokenSet =
327-
atom::ATOM_EXPR_FIRST.union(TokenSet::new(&[T![&], T![*], T![!], T![.], T![-], T![_]]));
325+
atom::ATOM_EXPR_FIRST.union(TokenSet::new(&[T![&], T![*], T![!], T![.], T![-], T![_], T![#]]));
328326

329327
fn lhs(p: &mut Parser<'_>, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> {
330328
let m;
@@ -653,7 +651,7 @@ fn arg_list(p: &mut Parser<'_>) {
653651
T![')'],
654652
T![,],
655653
|| "expected expression".into(),
656-
EXPR_FIRST.union(ATTRIBUTE_FIRST),
654+
EXPR_FIRST,
657655
|p| expr(p).is_some(),
658656
);
659657
m.complete(p, ARG_LIST);

crates/parser/src/grammar/expressions/atom.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,10 @@ fn return_expr(p: &mut Parser<'_>) -> CompletedMarker {
888888
let m = p.start();
889889
p.bump(T![return]);
890890
if p.at_ts(EXPR_FIRST) {
891+
// test return_attr
892+
// fn foo() {
893+
// return #[attr] 1;
894+
// }
891895
expr(p);
892896
}
893897
m.complete(p, RETURN_EXPR)

crates/parser/test_data/generated/runner.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,8 @@ mod ok {
584584
run_and_expect_no_errors("test_data/parser/inline/ok/reference_type.rs");
585585
}
586586
#[test]
587+
fn return_attr() { run_and_expect_no_errors("test_data/parser/inline/ok/return_attr.rs"); }
588+
#[test]
587589
fn return_expr() { run_and_expect_no_errors("test_data/parser/inline/ok/return_expr.rs"); }
588590
#[test]
589591
fn return_type_syntax_in_path() {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
BLOCK_EXPR
12+
STMT_LIST
13+
L_CURLY "{"
14+
WHITESPACE "\n "
15+
EXPR_STMT
16+
RETURN_EXPR
17+
RETURN_KW "return"
18+
WHITESPACE " "
19+
LITERAL
20+
ATTR
21+
POUND "#"
22+
L_BRACK "["
23+
PATH_META
24+
PATH
25+
PATH_SEGMENT
26+
NAME_REF
27+
IDENT "attr"
28+
R_BRACK "]"
29+
WHITESPACE " "
30+
INT_NUMBER "1"
31+
SEMICOLON ";"
32+
WHITESPACE "\n"
33+
R_CURLY "}"
34+
WHITESPACE "\n"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn foo() {
2+
return #[attr] 1;
3+
}

0 commit comments

Comments
 (0)