From b01496538c938d6a0c904512a38e4325cc960334 Mon Sep 17 00:00:00 2001 From: Ryan Cumming Date: Sun, 30 Jun 2019 17:59:26 +1000 Subject: 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 `;`. --- crates/ra_parser/src/grammar/expressions/atom.rs | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'crates/ra_parser/src') 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 { if p.eat(T![']']) { return m.complete(p, ARRAY_EXPR); } + + // test first_array_member_attributes + // pub const A: &[i64] = &[ + // #[cfg(test)] + // 1, + // 2, + // ]; + let first_member_has_attrs = p.at(T![#]); + attributes::outer_attributes(p); + expr(p); if p.eat(T![;]) { + if first_member_has_attrs { + // test_err array_length_attributes + // pub const A: &[i64] = &[ + // #[cfg(test)] + // 1; + // 2, + // ]; + p.error("removing an expression is not supported in this position"); + } + expr(p); p.expect(T![']']); return m.complete(p, ARRAY_EXPR); @@ -181,6 +201,14 @@ fn array_expr(p: &mut Parser) -> CompletedMarker { if p.at(T![']']) { break; } + + // test subsequent_array_member_attributes + // pub const A: &[i64] = &[ + // 1, + // #[cfg(test)] + // 2, + // ]; + attributes::outer_attributes(p); if !p.at_ts(EXPR_FIRST) { p.error("expected expression"); break; -- cgit v1.2.3 From 2959aa446e452d2176f9db453e52047453a3904e Mon Sep 17 00:00:00 2001 From: Ryan Cumming Date: Sun, 30 Jun 2019 19:55:50 +1000 Subject: Remove parse error on array initializer attributes This is actually allowed by the `rustc` parser but most attributes will fail later due to attributes on expressions being experimental. --- crates/ra_parser/src/grammar/expressions/atom.rs | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'crates/ra_parser/src') diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index 5e51d667e..6bda04141 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs @@ -177,21 +177,10 @@ fn array_expr(p: &mut Parser) -> CompletedMarker { // 1, // 2, // ]; - let first_member_has_attrs = p.at(T![#]); attributes::outer_attributes(p); expr(p); if p.eat(T![;]) { - if first_member_has_attrs { - // test_err array_length_attributes - // pub const A: &[i64] = &[ - // #[cfg(test)] - // 1; - // 2, - // ]; - p.error("removing an expression is not supported in this position"); - } - expr(p); p.expect(T![']']); return m.complete(p, ARRAY_EXPR); -- cgit v1.2.3