diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-01-17 10:50:06 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-01-17 10:50:06 +0000 |
commit | 07dca1123dfaec527c6ba3164125309f5c4cc3db (patch) | |
tree | 36e66272f2da663034059df83f299e20c84d27de | |
parent | 90b8a31b83b1aafc3fb555ba4307527f9258f46d (diff) | |
parent | 3a859e587f3bcf9f49293bd1f2b2d19cdfd2be4b (diff) |
Merge #2867
2867: Nest attrs into exprs in function args r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
4 files changed, 59 insertions, 55 deletions
diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index a31a7a08d..06c92645e 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs | |||
@@ -19,6 +19,26 @@ pub(super) fn expr(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) { | |||
19 | expr_bp(p, r, 1) | 19 | expr_bp(p, r, 1) |
20 | } | 20 | } |
21 | 21 | ||
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 | |||
22 | pub(super) fn expr_stmt(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) { | 42 | pub(super) fn expr_stmt(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) { |
23 | let r = Restrictions { forbid_structs: false, prefer_stmt: true }; | 43 | let r = Restrictions { forbid_structs: false, prefer_stmt: true }; |
24 | expr_bp(p, r, 1) | 44 | expr_bp(p, r, 1) |
@@ -544,12 +564,9 @@ fn arg_list(p: &mut Parser) { | |||
544 | // fn main() { | 564 | // fn main() { |
545 | // foo(#[attr] 92) | 565 | // foo(#[attr] 92) |
546 | // } | 566 | // } |
547 | attributes::outer_attributes(p); | 567 | if !expr_with_attrs(p) { |
548 | if !p.at_ts(EXPR_FIRST) { | ||
549 | p.error("expected expression"); | ||
550 | break; | 568 | break; |
551 | } | 569 | } |
552 | expr(p); | ||
553 | if !p.at(T![')']) && !p.expect(T![,]) { | 570 | if !p.at(T![')']) && !p.expect(T![,]) { |
554 | break; | 571 | break; |
555 | } | 572 | } |
diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index a98a2a3ef..2cc321473 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs | |||
@@ -191,19 +191,8 @@ fn array_expr(p: &mut Parser) -> CompletedMarker { | |||
191 | 191 | ||
192 | // test array_attrs | 192 | // test array_attrs |
193 | // const A: &[i64] = &[1, #[cfg(test)] 2]; | 193 | // const A: &[i64] = &[1, #[cfg(test)] 2]; |
194 | let m = p.start(); | 194 | if !expr_with_attrs(p) { |
195 | let has_attrs = p.at(T![#]); | 195 | break; |
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), | ||
207 | } | 196 | } |
208 | 197 | ||
209 | if n_exprs == 1 && p.eat(T![;]) { | 198 | if n_exprs == 1 && p.eat(T![;]) { |
diff --git a/crates/ra_syntax/test_data/parser/err/0022_bad_exprs.txt b/crates/ra_syntax/test_data/parser/err/0022_bad_exprs.txt index 310a82464..cb45eb2fc 100644 --- a/crates/ra_syntax/test_data/parser/err/0022_bad_exprs.txt +++ b/crates/ra_syntax/test_data/parser/err/0022_bad_exprs.txt | |||
@@ -12,8 +12,8 @@ SOURCE_FILE@[0; 112) | |||
12 | BLOCK@[7; 33) | 12 | BLOCK@[7; 33) |
13 | L_CURLY@[7; 8) "{" | 13 | L_CURLY@[7; 8) "{" |
14 | WHITESPACE@[8; 9) " " | 14 | WHITESPACE@[8; 9) " " |
15 | EXPR_STMT@[9; 26) | 15 | EXPR_STMT@[9; 17) |
16 | ARRAY_EXPR@[9; 26) | 16 | ARRAY_EXPR@[9; 17) |
17 | L_BRACK@[9; 10) "[" | 17 | L_BRACK@[9; 10) "[" |
18 | LITERAL@[10; 11) | 18 | LITERAL@[10; 11) |
19 | INT_NUMBER@[10; 11) "1" | 19 | INT_NUMBER@[10; 11) "1" |
@@ -25,10 +25,13 @@ SOURCE_FILE@[0; 112) | |||
25 | WHITESPACE@[15; 16) " " | 25 | WHITESPACE@[15; 16) " " |
26 | ERROR@[16; 17) | 26 | ERROR@[16; 17) |
27 | AT@[16; 17) "@" | 27 | AT@[16; 17) "@" |
28 | EXPR_STMT@[17; 18) | ||
29 | ERROR@[17; 18) | ||
28 | COMMA@[17; 18) "," | 30 | COMMA@[17; 18) "," |
29 | WHITESPACE@[18; 19) " " | 31 | WHITESPACE@[18; 19) " " |
30 | ERROR@[19; 25) | 32 | STRUCT_DEF@[19; 26) |
31 | STRUCT_KW@[19; 25) "struct" | 33 | STRUCT_KW@[19; 25) "struct" |
34 | ERROR@[25; 26) | ||
32 | COMMA@[25; 26) "," | 35 | COMMA@[25; 26) "," |
33 | WHITESPACE@[26; 27) " " | 36 | WHITESPACE@[26; 27) " " |
34 | LET_STMT@[27; 31) | 37 | LET_STMT@[27; 31) |
@@ -51,14 +54,14 @@ SOURCE_FILE@[0; 112) | |||
51 | BLOCK@[41; 68) | 54 | BLOCK@[41; 68) |
52 | L_CURLY@[41; 42) "{" | 55 | L_CURLY@[41; 42) "{" |
53 | WHITESPACE@[42; 43) " " | 56 | WHITESPACE@[42; 43) " " |
54 | EXPR_STMT@[43; 52) | 57 | EXPR_STMT@[43; 54) |
55 | CALL_EXPR@[43; 52) | 58 | CALL_EXPR@[43; 54) |
56 | PATH_EXPR@[43; 46) | 59 | PATH_EXPR@[43; 46) |
57 | PATH@[43; 46) | 60 | PATH@[43; 46) |
58 | PATH_SEGMENT@[43; 46) | 61 | PATH_SEGMENT@[43; 46) |
59 | NAME_REF@[43; 46) | 62 | NAME_REF@[43; 46) |
60 | IDENT@[43; 46) "foo" | 63 | IDENT@[43; 46) "foo" |
61 | ARG_LIST@[46; 52) | 64 | ARG_LIST@[46; 54) |
62 | L_PAREN@[46; 47) "(" | 65 | L_PAREN@[46; 47) "(" |
63 | LITERAL@[47; 48) | 66 | LITERAL@[47; 48) |
64 | INT_NUMBER@[47; 48) "1" | 67 | INT_NUMBER@[47; 48) "1" |
@@ -67,10 +70,9 @@ SOURCE_FILE@[0; 112) | |||
67 | LITERAL@[50; 51) | 70 | LITERAL@[50; 51) |
68 | INT_NUMBER@[50; 51) "2" | 71 | INT_NUMBER@[50; 51) "2" |
69 | COMMA@[51; 52) "," | 72 | COMMA@[51; 52) "," |
70 | WHITESPACE@[52; 53) " " | 73 | WHITESPACE@[52; 53) " " |
71 | EXPR_STMT@[53; 54) | 74 | ERROR@[53; 54) |
72 | ERROR@[53; 54) | 75 | AT@[53; 54) "@" |
73 | AT@[53; 54) "@" | ||
74 | EXPR_STMT@[54; 55) | 76 | EXPR_STMT@[54; 55) |
75 | ERROR@[54; 55) | 77 | ERROR@[54; 55) |
76 | COMMA@[54; 55) "," | 78 | COMMA@[54; 55) "," |
@@ -101,8 +103,8 @@ SOURCE_FILE@[0; 112) | |||
101 | BLOCK@[76; 111) | 103 | BLOCK@[76; 111) |
102 | L_CURLY@[76; 77) "{" | 104 | L_CURLY@[76; 77) "{" |
103 | WHITESPACE@[77; 78) " " | 105 | WHITESPACE@[77; 78) " " |
104 | EXPR_STMT@[78; 91) | 106 | EXPR_STMT@[78; 93) |
105 | METHOD_CALL_EXPR@[78; 91) | 107 | METHOD_CALL_EXPR@[78; 93) |
106 | PATH_EXPR@[78; 81) | 108 | PATH_EXPR@[78; 81) |
107 | PATH@[78; 81) | 109 | PATH@[78; 81) |
108 | PATH_SEGMENT@[78; 81) | 110 | PATH_SEGMENT@[78; 81) |
@@ -111,7 +113,7 @@ SOURCE_FILE@[0; 112) | |||
111 | DOT@[81; 82) "." | 113 | DOT@[81; 82) "." |
112 | NAME_REF@[82; 85) | 114 | NAME_REF@[82; 85) |
113 | IDENT@[82; 85) "bar" | 115 | IDENT@[82; 85) "bar" |
114 | ARG_LIST@[85; 91) | 116 | ARG_LIST@[85; 93) |
115 | L_PAREN@[85; 86) "(" | 117 | L_PAREN@[85; 86) "(" |
116 | LITERAL@[86; 87) | 118 | LITERAL@[86; 87) |
117 | INT_NUMBER@[86; 87) "1" | 119 | INT_NUMBER@[86; 87) "1" |
@@ -120,10 +122,9 @@ SOURCE_FILE@[0; 112) | |||
120 | LITERAL@[89; 90) | 122 | LITERAL@[89; 90) |
121 | INT_NUMBER@[89; 90) "2" | 123 | INT_NUMBER@[89; 90) "2" |
122 | COMMA@[90; 91) "," | 124 | COMMA@[90; 91) "," |
123 | WHITESPACE@[91; 92) " " | 125 | WHITESPACE@[91; 92) " " |
124 | EXPR_STMT@[92; 93) | 126 | ERROR@[92; 93) |
125 | ERROR@[92; 93) | 127 | AT@[92; 93) "@" |
126 | AT@[92; 93) "@" | ||
127 | EXPR_STMT@[93; 94) | 128 | EXPR_STMT@[93; 94) |
128 | ERROR@[93; 94) | 129 | ERROR@[93; 94) |
129 | COMMA@[93; 94) "," | 130 | COMMA@[93; 94) "," |
@@ -148,15 +149,14 @@ SOURCE_FILE@[0; 112) | |||
148 | R_CURLY@[110; 111) "}" | 149 | R_CURLY@[110; 111) "}" |
149 | WHITESPACE@[111; 112) "\n" | 150 | WHITESPACE@[111; 112) "\n" |
150 | error 16: expected expression | 151 | error 16: expected expression |
151 | error 19: expected expression | 152 | error 17: expected R_BRACK |
152 | error 26: expected expression | 153 | error 17: expected SEMI |
153 | error 26: expected COMMA | 154 | error 17: expected expression |
154 | error 26: expected R_BRACK | 155 | error 18: expected SEMI |
155 | error 26: expected SEMI | 156 | error 25: expected a name |
157 | error 26: expected `;`, `{`, or `(` | ||
156 | error 30: expected pattern | 158 | error 30: expected pattern |
157 | error 31: expected SEMI | 159 | error 31: expected SEMI |
158 | error 52: expected expression | ||
159 | error 52: expected SEMI | ||
160 | error 53: expected expression | 160 | error 53: expected expression |
161 | error 54: expected SEMI | 161 | error 54: expected SEMI |
162 | error 54: expected expression | 162 | error 54: expected expression |
@@ -168,8 +168,6 @@ error 61: expected SEMI | |||
168 | error 65: expected pattern | 168 | error 65: expected pattern |
169 | error 65: expected SEMI | 169 | error 65: expected SEMI |
170 | error 65: expected expression | 170 | error 65: expected expression |
171 | error 91: expected expression | ||
172 | error 91: expected SEMI | ||
173 | error 92: expected expression | 171 | error 92: expected expression |
174 | error 93: expected SEMI | 172 | error 93: expected SEMI |
175 | error 93: expected expression | 173 | error 93: expected expression |
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_with_attr.txt b/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_with_attr.txt index 6b80ca8d0..8092d7009 100644 --- a/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_with_attr.txt +++ b/crates/ra_syntax/test_data/parser/inline/ok/0152_arg_with_attr.txt | |||
@@ -20,16 +20,16 @@ SOURCE_FILE@[0; 34) | |||
20 | IDENT@[16; 19) "foo" | 20 | IDENT@[16; 19) "foo" |
21 | ARG_LIST@[19; 31) | 21 | ARG_LIST@[19; 31) |
22 | L_PAREN@[19; 20) "(" | 22 | L_PAREN@[19; 20) "(" |
23 | ATTR@[20; 27) | 23 | LITERAL@[20; 30) |
24 | POUND@[20; 21) "#" | 24 | ATTR@[20; 27) |
25 | L_BRACK@[21; 22) "[" | 25 | POUND@[20; 21) "#" |
26 | PATH@[22; 26) | 26 | L_BRACK@[21; 22) "[" |
27 | PATH_SEGMENT@[22; 26) | 27 | PATH@[22; 26) |
28 | NAME_REF@[22; 26) | 28 | PATH_SEGMENT@[22; 26) |
29 | IDENT@[22; 26) "attr" | 29 | NAME_REF@[22; 26) |
30 | R_BRACK@[26; 27) "]" | 30 | IDENT@[22; 26) "attr" |
31 | WHITESPACE@[27; 28) " " | 31 | R_BRACK@[26; 27) "]" |
32 | LITERAL@[28; 30) | 32 | WHITESPACE@[27; 28) " " |
33 | INT_NUMBER@[28; 30) "92" | 33 | INT_NUMBER@[28; 30) "92" |
34 | R_PAREN@[30; 31) ")" | 34 | R_PAREN@[30; 31) ")" |
35 | WHITESPACE@[31; 32) "\n" | 35 | WHITESPACE@[31; 32) "\n" |