Skip to content

Commit e614356

Browse files
Merge #2827
2827: Fix array element attribute position r=matklad a=edwin0cheng This PR fixed a bug which an ATTR node insert in the wrong place in array element. ~~And introduce `precede_next` for allow outer attributes to insert into a parsed `expr`.~~ related #2783 Co-authored-by: Edwin Cheng <[email protected]>
2 parents f4eeff2 + a766883 commit e614356

File tree

5 files changed

+60
-36
lines changed

5 files changed

+60
-36
lines changed

crates/ra_parser/src/grammar/attributes.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ pub(super) fn inner_attributes(p: &mut Parser) {
88
}
99
}
1010

11+
pub(super) fn with_outer_attributes(
12+
p: &mut Parser,
13+
f: impl Fn(&mut Parser) -> Option<CompletedMarker>,
14+
) -> bool {
15+
let am = p.start();
16+
let has_attrs = p.at(T![#]);
17+
attributes::outer_attributes(p);
18+
let cm = f(p);
19+
let success = cm.is_some();
20+
21+
match (has_attrs, cm) {
22+
(true, Some(cm)) => {
23+
let kind = cm.kind();
24+
cm.undo_completion(p).abandon(p);
25+
am.complete(p, kind);
26+
}
27+
_ => am.abandon(p),
28+
}
29+
30+
success
31+
}
32+
1133
pub(super) fn outer_attributes(p: &mut Parser) {
1234
while p.at(T![#]) {
1335
attribute(p, false)

crates/ra_parser/src/grammar/expressions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ pub(super) enum StmtWithSemi {
1414

1515
const EXPR_FIRST: TokenSet = LHS_FIRST;
1616

17-
pub(super) fn expr(p: &mut Parser) -> BlockLike {
17+
pub(super) fn expr(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) {
1818
let r = Restrictions { forbid_structs: false, prefer_stmt: false };
19-
expr_bp(p, r, 1).1
19+
expr_bp(p, r, 1)
2020
}
2121

2222
pub(super) fn expr_stmt(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) {

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,8 @@ fn array_expr(p: &mut Parser) -> CompletedMarker {
192192
// 1,
193193
// 2,
194194
// ];
195-
attributes::outer_attributes(p);
195+
attributes::with_outer_attributes(p, |p| expr(p).0);
196196

197-
expr(p);
198197
if p.eat(T![;]) {
199198
expr(p);
200199
p.expect(T![']']);
@@ -212,12 +211,15 @@ fn array_expr(p: &mut Parser) -> CompletedMarker {
212211
// #[cfg(test)]
213212
// 2,
214213
// ];
215-
attributes::outer_attributes(p);
216-
if !p.at_ts(EXPR_FIRST) {
217-
p.error("expected expression");
214+
if !attributes::with_outer_attributes(p, |p| {
215+
if !p.at_ts(EXPR_FIRST) {
216+
p.error("expected expression");
217+
return None;
218+
}
219+
expr(p).0
220+
}) {
218221
break;
219222
}
220-
expr(p);
221223
}
222224
p.expect(T![']']);
223225
m.complete(p, ARRAY_EXPR)

crates/ra_syntax/test_data/parser/inline/ok/0135_first_array_member_attributes.txt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ SOURCE_FILE@[0; 56)
2727
ARRAY_EXPR@[23; 54)
2828
L_BRACK@[23; 24) "["
2929
WHITESPACE@[24; 28) "\n "
30-
ATTR@[28; 40)
31-
POUND@[28; 29) "#"
32-
L_BRACK@[29; 30) "["
33-
PATH@[30; 33)
34-
PATH_SEGMENT@[30; 33)
35-
NAME_REF@[30; 33)
36-
IDENT@[30; 33) "cfg"
37-
TOKEN_TREE@[33; 39)
38-
L_PAREN@[33; 34) "("
39-
IDENT@[34; 38) "test"
40-
R_PAREN@[38; 39) ")"
41-
R_BRACK@[39; 40) "]"
42-
WHITESPACE@[40; 44) "\n "
43-
LITERAL@[44; 45)
30+
LITERAL@[28; 45)
31+
ATTR@[28; 40)
32+
POUND@[28; 29) "#"
33+
L_BRACK@[29; 30) "["
34+
PATH@[30; 33)
35+
PATH_SEGMENT@[30; 33)
36+
NAME_REF@[30; 33)
37+
IDENT@[30; 33) "cfg"
38+
TOKEN_TREE@[33; 39)
39+
L_PAREN@[33; 34) "("
40+
IDENT@[34; 38) "test"
41+
R_PAREN@[38; 39) ")"
42+
R_BRACK@[39; 40) "]"
43+
WHITESPACE@[40; 44) "\n "
4444
INT_NUMBER@[44; 45) "1"
4545
COMMA@[45; 46) ","
4646
WHITESPACE@[46; 50) "\n "

crates/ra_syntax/test_data/parser/inline/ok/0136_subsequent_array_member_attributes.txt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@ SOURCE_FILE@[0; 56)
3131
INT_NUMBER@[28; 29) "1"
3232
COMMA@[29; 30) ","
3333
WHITESPACE@[30; 34) "\n "
34-
ATTR@[34; 46)
35-
POUND@[34; 35) "#"
36-
L_BRACK@[35; 36) "["
37-
PATH@[36; 39)
38-
PATH_SEGMENT@[36; 39)
39-
NAME_REF@[36; 39)
40-
IDENT@[36; 39) "cfg"
41-
TOKEN_TREE@[39; 45)
42-
L_PAREN@[39; 40) "("
43-
IDENT@[40; 44) "test"
44-
R_PAREN@[44; 45) ")"
45-
R_BRACK@[45; 46) "]"
46-
WHITESPACE@[46; 50) "\n "
47-
LITERAL@[50; 51)
34+
LITERAL@[34; 51)
35+
ATTR@[34; 46)
36+
POUND@[34; 35) "#"
37+
L_BRACK@[35; 36) "["
38+
PATH@[36; 39)
39+
PATH_SEGMENT@[36; 39)
40+
NAME_REF@[36; 39)
41+
IDENT@[36; 39) "cfg"
42+
TOKEN_TREE@[39; 45)
43+
L_PAREN@[39; 40) "("
44+
IDENT@[40; 44) "test"
45+
R_PAREN@[44; 45) ")"
46+
R_BRACK@[45; 46) "]"
47+
WHITESPACE@[46; 50) "\n "
4848
INT_NUMBER@[50; 51) "2"
4949
COMMA@[51; 52) ","
5050
WHITESPACE@[52; 53) "\n"

0 commit comments

Comments
 (0)