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 ++++++++++ .../inline/err/0015_array_length_attributes.rs | 5 ++ .../inline/err/0015_array_length_attributes.txt | 60 ++++++++++++++++++++++ .../ok/0135_first_array_member_attributes.rs | 5 ++ .../ok/0135_first_array_member_attributes.txt | 51 ++++++++++++++++++ .../ok/0136_subsequent_array_member_attributes.rs | 5 ++ .../ok/0136_subsequent_array_member_attributes.txt | 51 ++++++++++++++++++ 7 files changed, 205 insertions(+) create mode 100644 crates/ra_syntax/tests/data/parser/inline/err/0015_array_length_attributes.rs create mode 100644 crates/ra_syntax/tests/data/parser/inline/err/0015_array_length_attributes.txt create mode 100644 crates/ra_syntax/tests/data/parser/inline/ok/0135_first_array_member_attributes.rs create mode 100644 crates/ra_syntax/tests/data/parser/inline/ok/0135_first_array_member_attributes.txt create mode 100644 crates/ra_syntax/tests/data/parser/inline/ok/0136_subsequent_array_member_attributes.rs create mode 100644 crates/ra_syntax/tests/data/parser/inline/ok/0136_subsequent_array_member_attributes.txt 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; diff --git a/crates/ra_syntax/tests/data/parser/inline/err/0015_array_length_attributes.rs b/crates/ra_syntax/tests/data/parser/inline/err/0015_array_length_attributes.rs new file mode 100644 index 000000000..ba630981d --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/inline/err/0015_array_length_attributes.rs @@ -0,0 +1,5 @@ +pub const A: &[i64] = &[ + #[cfg(test)] + 1; + 2, +]; diff --git a/crates/ra_syntax/tests/data/parser/inline/err/0015_array_length_attributes.txt b/crates/ra_syntax/tests/data/parser/inline/err/0015_array_length_attributes.txt new file mode 100644 index 000000000..5f0e3f22c --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/inline/err/0015_array_length_attributes.txt @@ -0,0 +1,60 @@ +SOURCE_FILE@[0; 53) + CONST_DEF@[0; 48) + VISIBILITY@[0; 3) + PUB_KW@[0; 3) "pub" + WHITESPACE@[3; 4) " " + CONST_KW@[4; 9) "const" + WHITESPACE@[9; 10) " " + NAME@[10; 11) + IDENT@[10; 11) "A" + COLON@[11; 12) ":" + WHITESPACE@[12; 13) " " + REFERENCE_TYPE@[13; 19) + AMP@[13; 14) "&" + SLICE_TYPE@[14; 19) + L_BRACK@[14; 15) "[" + PATH_TYPE@[15; 18) + PATH@[15; 18) + PATH_SEGMENT@[15; 18) + NAME_REF@[15; 18) + IDENT@[15; 18) "i64" + R_BRACK@[18; 19) "]" + WHITESPACE@[19; 20) " " + EQ@[20; 21) "=" + WHITESPACE@[21; 22) " " + REF_EXPR@[22; 48) + AMP@[22; 23) "&" + ARRAY_EXPR@[23; 48) + L_BRACK@[23; 24) "[" + WHITESPACE@[24; 27) "\n " + ATTR@[27; 39) + POUND@[27; 28) "#" + TOKEN_TREE@[28; 39) + L_BRACK@[28; 29) "[" + IDENT@[29; 32) "cfg" + TOKEN_TREE@[32; 38) + L_PAREN@[32; 33) "(" + IDENT@[33; 37) "test" + R_PAREN@[37; 38) ")" + R_BRACK@[38; 39) "]" + WHITESPACE@[39; 42) "\n " + LITERAL@[42; 43) + INT_NUMBER@[42; 43) "1" + SEMI@[43; 44) ";" + WHITESPACE@[44; 47) "\n " + LITERAL@[47; 48) + INT_NUMBER@[47; 48) "2" + ERROR@[48; 49) + COMMA@[48; 49) "," + WHITESPACE@[49; 50) "\n" + ERROR@[50; 51) + R_BRACK@[50; 51) "]" + ERROR@[51; 52) + SEMI@[51; 52) ";" + WHITESPACE@[52; 53) "\n" +error 44: removing an expression is not supported in this position +error 48: expected R_BRACK +error 48: expected SEMI +error 48: expected an item +error 50: expected an item +error 51: expected an item diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0135_first_array_member_attributes.rs b/crates/ra_syntax/tests/data/parser/inline/ok/0135_first_array_member_attributes.rs new file mode 100644 index 000000000..f59e13771 --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0135_first_array_member_attributes.rs @@ -0,0 +1,5 @@ +pub const A: &[i64] = &[ + #[cfg(test)] + 1, + 2, +]; diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0135_first_array_member_attributes.txt b/crates/ra_syntax/tests/data/parser/inline/ok/0135_first_array_member_attributes.txt new file mode 100644 index 000000000..eac4f6f30 --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0135_first_array_member_attributes.txt @@ -0,0 +1,51 @@ +SOURCE_FILE@[0; 56) + CONST_DEF@[0; 55) + VISIBILITY@[0; 3) + PUB_KW@[0; 3) "pub" + WHITESPACE@[3; 4) " " + CONST_KW@[4; 9) "const" + WHITESPACE@[9; 10) " " + NAME@[10; 11) + IDENT@[10; 11) "A" + COLON@[11; 12) ":" + WHITESPACE@[12; 13) " " + REFERENCE_TYPE@[13; 19) + AMP@[13; 14) "&" + SLICE_TYPE@[14; 19) + L_BRACK@[14; 15) "[" + PATH_TYPE@[15; 18) + PATH@[15; 18) + PATH_SEGMENT@[15; 18) + NAME_REF@[15; 18) + IDENT@[15; 18) "i64" + R_BRACK@[18; 19) "]" + WHITESPACE@[19; 20) " " + EQ@[20; 21) "=" + WHITESPACE@[21; 22) " " + REF_EXPR@[22; 54) + AMP@[22; 23) "&" + ARRAY_EXPR@[23; 54) + L_BRACK@[23; 24) "[" + WHITESPACE@[24; 28) "\n " + ATTR@[28; 40) + POUND@[28; 29) "#" + TOKEN_TREE@[29; 40) + L_BRACK@[29; 30) "[" + IDENT@[30; 33) "cfg" + TOKEN_TREE@[33; 39) + L_PAREN@[33; 34) "(" + IDENT@[34; 38) "test" + R_PAREN@[38; 39) ")" + R_BRACK@[39; 40) "]" + WHITESPACE@[40; 44) "\n " + LITERAL@[44; 45) + INT_NUMBER@[44; 45) "1" + COMMA@[45; 46) "," + WHITESPACE@[46; 50) "\n " + LITERAL@[50; 51) + INT_NUMBER@[50; 51) "2" + COMMA@[51; 52) "," + WHITESPACE@[52; 53) "\n" + R_BRACK@[53; 54) "]" + SEMI@[54; 55) ";" + WHITESPACE@[55; 56) "\n" diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0136_subsequent_array_member_attributes.rs b/crates/ra_syntax/tests/data/parser/inline/ok/0136_subsequent_array_member_attributes.rs new file mode 100644 index 000000000..1324acb3f --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0136_subsequent_array_member_attributes.rs @@ -0,0 +1,5 @@ +pub const A: &[i64] = &[ + 1, + #[cfg(test)] + 2, +]; diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0136_subsequent_array_member_attributes.txt b/crates/ra_syntax/tests/data/parser/inline/ok/0136_subsequent_array_member_attributes.txt new file mode 100644 index 000000000..6fa1b42d9 --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0136_subsequent_array_member_attributes.txt @@ -0,0 +1,51 @@ +SOURCE_FILE@[0; 56) + CONST_DEF@[0; 55) + VISIBILITY@[0; 3) + PUB_KW@[0; 3) "pub" + WHITESPACE@[3; 4) " " + CONST_KW@[4; 9) "const" + WHITESPACE@[9; 10) " " + NAME@[10; 11) + IDENT@[10; 11) "A" + COLON@[11; 12) ":" + WHITESPACE@[12; 13) " " + REFERENCE_TYPE@[13; 19) + AMP@[13; 14) "&" + SLICE_TYPE@[14; 19) + L_BRACK@[14; 15) "[" + PATH_TYPE@[15; 18) + PATH@[15; 18) + PATH_SEGMENT@[15; 18) + NAME_REF@[15; 18) + IDENT@[15; 18) "i64" + R_BRACK@[18; 19) "]" + WHITESPACE@[19; 20) " " + EQ@[20; 21) "=" + WHITESPACE@[21; 22) " " + REF_EXPR@[22; 54) + AMP@[22; 23) "&" + ARRAY_EXPR@[23; 54) + L_BRACK@[23; 24) "[" + WHITESPACE@[24; 28) "\n " + LITERAL@[28; 29) + INT_NUMBER@[28; 29) "1" + COMMA@[29; 30) "," + WHITESPACE@[30; 34) "\n " + ATTR@[34; 46) + POUND@[34; 35) "#" + TOKEN_TREE@[35; 46) + L_BRACK@[35; 36) "[" + IDENT@[36; 39) "cfg" + TOKEN_TREE@[39; 45) + L_PAREN@[39; 40) "(" + IDENT@[40; 44) "test" + R_PAREN@[44; 45) ")" + R_BRACK@[45; 46) "]" + WHITESPACE@[46; 50) "\n " + LITERAL@[50; 51) + INT_NUMBER@[50; 51) "2" + COMMA@[51; 52) "," + WHITESPACE@[52; 53) "\n" + R_BRACK@[53; 54) "]" + SEMI@[54; 55) ";" + WHITESPACE@[55; 56) "\n" -- cgit v1.2.3