aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_parser/src/grammar/expressions
diff options
context:
space:
mode:
authorRyan Cumming <[email protected]>2019-06-30 08:59:26 +0100
committerRyan Cumming <[email protected]>2019-06-30 09:36:54 +0100
commitb01496538c938d6a0c904512a38e4325cc960334 (patch)
treea6588dc1af1094aadac540bfe90acced3fb08fea /crates/ra_parser/src/grammar/expressions
parent27df89f47d5f0a6e8e62d517d98dda854efabc34 (diff)
Support attributes on array members
Array members are allow to have attributes such as `#[cfg]`. This is a bit tricky as we don't know if the first expression is an initializer or a member until we encounter a `;`. This reuses a trick from `stmt` where we remember if we saw an attribute and then raise an error if the first expression ends up being an initializer. This isn't perfect as the error isn't correctly located on the attribute or initializer; it ends up immediately after the `;`.
Diffstat (limited to 'crates/ra_parser/src/grammar/expressions')
-rw-r--r--crates/ra_parser/src/grammar/expressions/atom.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs
index 41be283d0..5e51d667e 100644
--- a/crates/ra_parser/src/grammar/expressions/atom.rs
+++ b/crates/ra_parser/src/grammar/expressions/atom.rs
@@ -170,8 +170,28 @@ fn array_expr(p: &mut Parser) -> CompletedMarker {
170 if p.eat(T![']']) { 170 if p.eat(T![']']) {
171 return m.complete(p, ARRAY_EXPR); 171 return m.complete(p, ARRAY_EXPR);
172 } 172 }
173
174 // test first_array_member_attributes
175 // pub const A: &[i64] = &[
176 // #[cfg(test)]
177 // 1,
178 // 2,
179 // ];
180 let first_member_has_attrs = p.at(T![#]);
181 attributes::outer_attributes(p);
182
173 expr(p); 183 expr(p);
174 if p.eat(T![;]) { 184 if p.eat(T![;]) {
185 if first_member_has_attrs {
186 // test_err array_length_attributes
187 // pub const A: &[i64] = &[
188 // #[cfg(test)]
189 // 1;
190 // 2,
191 // ];
192 p.error("removing an expression is not supported in this position");
193 }
194
175 expr(p); 195 expr(p);
176 p.expect(T![']']); 196 p.expect(T![']']);
177 return m.complete(p, ARRAY_EXPR); 197 return m.complete(p, ARRAY_EXPR);
@@ -181,6 +201,14 @@ fn array_expr(p: &mut Parser) -> CompletedMarker {
181 if p.at(T![']']) { 201 if p.at(T![']']) {
182 break; 202 break;
183 } 203 }
204
205 // test subsequent_array_member_attributes
206 // pub const A: &[i64] = &[
207 // 1,
208 // #[cfg(test)]
209 // 2,
210 // ];
211 attributes::outer_attributes(p);
184 if !p.at_ts(EXPR_FIRST) { 212 if !p.at_ts(EXPR_FIRST) {
185 p.error("expected expression"); 213 p.error("expected expression");
186 break; 214 break;