Skip to content

Commit 07dca11

Browse files
bors[bot]matklad
andauthored
Merge #2867
2867: Nest attrs into exprs in function args r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 90b8a31 + 3a859e5 commit 07dca11

File tree

4 files changed

+59
-55
lines changed

4 files changed

+59
-55
lines changed

crates/ra_parser/src/grammar/expressions.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ pub(super) fn expr(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) {
1919
expr_bp(p, r, 1)
2020
}
2121

22+
pub(super) fn expr_with_attrs(p: &mut Parser) -> bool {
23+
let m = p.start();
24+
let has_attrs = p.at(T![#]);
25+
attributes::outer_attributes(p);
26+
27+
let (cm, _block_like) = expr(p);
28+
let success = cm.is_some();
29+
30+
match (has_attrs, cm) {
31+
(true, Some(cm)) => {
32+
let kind = cm.kind();
33+
cm.undo_completion(p).abandon(p);
34+
m.complete(p, kind);
35+
}
36+
_ => m.abandon(p),
37+
}
38+
39+
success
40+
}
41+
2242
pub(super) fn expr_stmt(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) {
2343
let r = Restrictions { forbid_structs: false, prefer_stmt: true };
2444
expr_bp(p, r, 1)
@@ -544,12 +564,9 @@ fn arg_list(p: &mut Parser) {
544564
// fn main() {
545565
// foo(#[attr] 92)
546566
// }
547-
attributes::outer_attributes(p);
548-
if !p.at_ts(EXPR_FIRST) {
549-
p.error("expected expression");
567+
if !expr_with_attrs(p) {
550568
break;
551569
}
552-
expr(p);
553570
if !p.at(T![')']) && !p.expect(T![,]) {
554571
break;
555572
}

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,8 @@ fn array_expr(p: &mut Parser) -> CompletedMarker {
191191

192192
// test array_attrs
193193
// const A: &[i64] = &[1, #[cfg(test)] 2];
194-
let m = p.start();
195-
let has_attrs = p.at(T![#]);
196-
attributes::outer_attributes(p);
197-
198-
let cm = expr(p).0;
199-
200-
match (has_attrs, cm) {
201-
(true, Some(cm)) => {
202-
let kind = cm.kind();
203-
cm.undo_completion(p).abandon(p);
204-
m.complete(p, kind);
205-
}
206-
_ => m.abandon(p),
194+
if !expr_with_attrs(p) {
195+
break;
207196
}
208197

209198
if n_exprs == 1 && p.eat(T![;]) {

crates/ra_syntax/test_data/parser/err/0022_bad_exprs.txt

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ SOURCE_FILE@[0; 112)
1212
BLOCK@[7; 33)
1313
L_CURLY@[7; 8) "{"
1414
WHITESPACE@[8; 9) " "
15-
EXPR_STMT@[9; 26)
16-
ARRAY_EXPR@[9; 26)
15+
EXPR_STMT@[9; 17)
16+
ARRAY_EXPR@[9; 17)
1717
L_BRACK@[9; 10) "["
1818
LITERAL@[10; 11)
1919
INT_NUMBER@[10; 11) "1"
@@ -25,10 +25,13 @@ SOURCE_FILE@[0; 112)
2525
WHITESPACE@[15; 16) " "
2626
ERROR@[16; 17)
2727
AT@[16; 17) "@"
28+
EXPR_STMT@[17; 18)
29+
ERROR@[17; 18)
2830
COMMA@[17; 18) ","
29-
WHITESPACE@[18; 19) " "
30-
ERROR@[19; 25)
31-
STRUCT_KW@[19; 25) "struct"
31+
WHITESPACE@[18; 19) " "
32+
STRUCT_DEF@[19; 26)
33+
STRUCT_KW@[19; 25) "struct"
34+
ERROR@[25; 26)
3235
COMMA@[25; 26) ","
3336
WHITESPACE@[26; 27) " "
3437
LET_STMT@[27; 31)
@@ -51,14 +54,14 @@ SOURCE_FILE@[0; 112)
5154
BLOCK@[41; 68)
5255
L_CURLY@[41; 42) "{"
5356
WHITESPACE@[42; 43) " "
54-
EXPR_STMT@[43; 52)
55-
CALL_EXPR@[43; 52)
57+
EXPR_STMT@[43; 54)
58+
CALL_EXPR@[43; 54)
5659
PATH_EXPR@[43; 46)
5760
PATH@[43; 46)
5861
PATH_SEGMENT@[43; 46)
5962
NAME_REF@[43; 46)
6063
IDENT@[43; 46) "foo"
61-
ARG_LIST@[46; 52)
64+
ARG_LIST@[46; 54)
6265
L_PAREN@[46; 47) "("
6366
LITERAL@[47; 48)
6467
INT_NUMBER@[47; 48) "1"
@@ -67,10 +70,9 @@ SOURCE_FILE@[0; 112)
6770
LITERAL@[50; 51)
6871
INT_NUMBER@[50; 51) "2"
6972
COMMA@[51; 52) ","
70-
WHITESPACE@[52; 53) " "
71-
EXPR_STMT@[53; 54)
72-
ERROR@[53; 54)
73-
AT@[53; 54) "@"
73+
WHITESPACE@[52; 53) " "
74+
ERROR@[53; 54)
75+
AT@[53; 54) "@"
7476
EXPR_STMT@[54; 55)
7577
ERROR@[54; 55)
7678
COMMA@[54; 55) ","
@@ -101,8 +103,8 @@ SOURCE_FILE@[0; 112)
101103
BLOCK@[76; 111)
102104
L_CURLY@[76; 77) "{"
103105
WHITESPACE@[77; 78) " "
104-
EXPR_STMT@[78; 91)
105-
METHOD_CALL_EXPR@[78; 91)
106+
EXPR_STMT@[78; 93)
107+
METHOD_CALL_EXPR@[78; 93)
106108
PATH_EXPR@[78; 81)
107109
PATH@[78; 81)
108110
PATH_SEGMENT@[78; 81)
@@ -111,7 +113,7 @@ SOURCE_FILE@[0; 112)
111113
DOT@[81; 82) "."
112114
NAME_REF@[82; 85)
113115
IDENT@[82; 85) "bar"
114-
ARG_LIST@[85; 91)
116+
ARG_LIST@[85; 93)
115117
L_PAREN@[85; 86) "("
116118
LITERAL@[86; 87)
117119
INT_NUMBER@[86; 87) "1"
@@ -120,10 +122,9 @@ SOURCE_FILE@[0; 112)
120122
LITERAL@[89; 90)
121123
INT_NUMBER@[89; 90) "2"
122124
COMMA@[90; 91) ","
123-
WHITESPACE@[91; 92) " "
124-
EXPR_STMT@[92; 93)
125-
ERROR@[92; 93)
126-
AT@[92; 93) "@"
125+
WHITESPACE@[91; 92) " "
126+
ERROR@[92; 93)
127+
AT@[92; 93) "@"
127128
EXPR_STMT@[93; 94)
128129
ERROR@[93; 94)
129130
COMMA@[93; 94) ","
@@ -148,15 +149,14 @@ SOURCE_FILE@[0; 112)
148149
R_CURLY@[110; 111) "}"
149150
WHITESPACE@[111; 112) "\n"
150151
error 16: expected expression
151-
error 19: expected expression
152-
error 26: expected expression
153-
error 26: expected COMMA
154-
error 26: expected R_BRACK
155-
error 26: expected SEMI
152+
error 17: expected R_BRACK
153+
error 17: expected SEMI
154+
error 17: expected expression
155+
error 18: expected SEMI
156+
error 25: expected a name
157+
error 26: expected `;`, `{`, or `(`
156158
error 30: expected pattern
157159
error 31: expected SEMI
158-
error 52: expected expression
159-
error 52: expected SEMI
160160
error 53: expected expression
161161
error 54: expected SEMI
162162
error 54: expected expression
@@ -168,8 +168,6 @@ error 61: expected SEMI
168168
error 65: expected pattern
169169
error 65: expected SEMI
170170
error 65: expected expression
171-
error 91: expected expression
172-
error 91: expected SEMI
173171
error 92: expected expression
174172
error 93: expected SEMI
175173
error 93: expected expression

crates/ra_syntax/test_data/parser/inline/ok/0152_arg_with_attr.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ SOURCE_FILE@[0; 34)
2020
IDENT@[16; 19) "foo"
2121
ARG_LIST@[19; 31)
2222
L_PAREN@[19; 20) "("
23-
ATTR@[20; 27)
24-
POUND@[20; 21) "#"
25-
L_BRACK@[21; 22) "["
26-
PATH@[22; 26)
27-
PATH_SEGMENT@[22; 26)
28-
NAME_REF@[22; 26)
29-
IDENT@[22; 26) "attr"
30-
R_BRACK@[26; 27) "]"
31-
WHITESPACE@[27; 28) " "
32-
LITERAL@[28; 30)
23+
LITERAL@[20; 30)
24+
ATTR@[20; 27)
25+
POUND@[20; 21) "#"
26+
L_BRACK@[21; 22) "["
27+
PATH@[22; 26)
28+
PATH_SEGMENT@[22; 26)
29+
NAME_REF@[22; 26)
30+
IDENT@[22; 26) "attr"
31+
R_BRACK@[26; 27) "]"
32+
WHITESPACE@[27; 28) " "
3333
INT_NUMBER@[28; 30) "92"
3434
R_PAREN@[30; 31) ")"
3535
WHITESPACE@[31; 32) "\n"

0 commit comments

Comments
 (0)