aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
authorDJMcNab <[email protected]>2019-01-28 20:03:56 +0000
committerDJMcNab <[email protected]>2019-01-28 20:03:56 +0000
commit00e6b5d26c82d5faff066c24418a0eb5741efcd1 (patch)
tree82ad741bab4f820850ba6f13e2b760cc840f43fb /crates/ra_syntax
parent137b1ccb715fe795cdf6c528bef6f8df3387f158 (diff)
Parse and validate attributes in blocks
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/src/ast/generated.rs1
-rw-r--r--crates/ra_syntax/src/grammar.ron3
-rw-r--r--crates/ra_syntax/src/grammar/expressions.rs2
-rw-r--r--crates/ra_syntax/src/validation.rs2
-rw-r--r--crates/ra_syntax/src/validation/block.rs24
-rw-r--r--crates/ra_syntax/src/yellow/syntax_error.rs4
-rw-r--r--crates/ra_syntax/tests/data/parser/err/0031_block_inner_attrs.rs15
-rw-r--r--crates/ra_syntax/tests/data/parser/err/0031_block_inner_attrs.txt114
-rw-r--r--crates/ra_syntax/tests/data/parser/ok/0045_block_inner_attrs.rs20
-rw-r--r--crates/ra_syntax/tests/data/parser/ok/0045_block_inner_attrs.txt167
10 files changed, 352 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index 3ace6533c..ce559882b 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -272,6 +272,7 @@ impl ToOwned for Block {
272} 272}
273 273
274 274
275impl ast::AttrsOwner for Block {}
275impl Block { 276impl Block {
276 pub fn statements(&self) -> impl Iterator<Item = &Stmt> { 277 pub fn statements(&self) -> impl Iterator<Item = &Stmt> {
277 super::children(self) 278 super::children(self)
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron
index 85fc79038..f4841241f 100644
--- a/crates/ra_syntax/src/grammar.ron
+++ b/crates/ra_syntax/src/grammar.ron
@@ -571,6 +571,9 @@ Grammar(
571 options: [ "Expr" ], 571 options: [ "Expr" ],
572 collections: [ 572 collections: [
573 ["statements", "Stmt"], 573 ["statements", "Stmt"],
574 ],
575 traits: [
576 "AttrsOwner",
574 ] 577 ]
575 ), 578 ),
576 "ParamList": ( 579 "ParamList": (
diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs
index d27eb8b7e..6b88c5685 100644
--- a/crates/ra_syntax/src/grammar/expressions.rs
+++ b/crates/ra_syntax/src/grammar/expressions.rs
@@ -42,6 +42,8 @@ pub(crate) fn block(p: &mut Parser) {
42 } 42 }
43 let m = p.start(); 43 let m = p.start();
44 p.bump(); 44 p.bump();
45 // This is checked by a validator
46 attributes::inner_attributes(p);
45 47
46 while !p.at(EOF) && !p.at(R_CURLY) { 48 while !p.at(EOF) && !p.at(R_CURLY) {
47 match p.current() { 49 match p.current() {
diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs
index 73e1d20b9..ac6cc3dd6 100644
--- a/crates/ra_syntax/src/validation.rs
+++ b/crates/ra_syntax/src/validation.rs
@@ -2,6 +2,7 @@ mod byte;
2mod byte_string; 2mod byte_string;
3mod char; 3mod char;
4mod string; 4mod string;
5mod block;
5 6
6use crate::{ 7use crate::{
7 SourceFile, yellow::SyntaxError, AstNode, 8 SourceFile, yellow::SyntaxError, AstNode,
@@ -17,6 +18,7 @@ pub(crate) fn validate(file: &SourceFile) -> Vec<SyntaxError> {
17 .visit::<ast::ByteString, _>(self::byte_string::validate_byte_string_node) 18 .visit::<ast::ByteString, _>(self::byte_string::validate_byte_string_node)
18 .visit::<ast::Char, _>(self::char::validate_char_node) 19 .visit::<ast::Char, _>(self::char::validate_char_node)
19 .visit::<ast::String, _>(self::string::validate_string_node) 20 .visit::<ast::String, _>(self::string::validate_string_node)
21 .visit::<ast::Block, _>(self::block::validate_block_node)
20 .accept(node); 22 .accept(node);
21 } 23 }
22 errors 24 errors
diff --git a/crates/ra_syntax/src/validation/block.rs b/crates/ra_syntax/src/validation/block.rs
new file mode 100644
index 000000000..9e1949124
--- /dev/null
+++ b/crates/ra_syntax/src/validation/block.rs
@@ -0,0 +1,24 @@
1use crate::{SyntaxKind::*,
2 ast::{self, AttrsOwner, AstNode},
3 yellow::{
4 SyntaxError,
5 SyntaxErrorKind::*,
6 },
7};
8
9pub(crate) fn validate_block_node(node: &ast::Block, errors: &mut Vec<SyntaxError>) {
10 if let Some(parent) = node.syntax().parent() {
11 match parent.kind() {
12 FN_DEF => return,
13 BLOCK_EXPR => match parent.parent().map(|v| v.kind()) {
14 Some(EXPR_STMT) | Some(BLOCK) => return,
15 _ => {}
16 },
17 _ => {}
18 }
19 }
20 errors.extend(
21 node.attrs()
22 .map(|attr| SyntaxError::new(InvalidBlockAttr, attr.syntax().range())),
23 )
24}
diff --git a/crates/ra_syntax/src/yellow/syntax_error.rs b/crates/ra_syntax/src/yellow/syntax_error.rs
index 534f3511e..c52c44cc3 100644
--- a/crates/ra_syntax/src/yellow/syntax_error.rs
+++ b/crates/ra_syntax/src/yellow/syntax_error.rs
@@ -94,6 +94,7 @@ pub enum SyntaxErrorKind {
94 UnicodeEscapeOutOfRange, 94 UnicodeEscapeOutOfRange,
95 UnclosedString, 95 UnclosedString,
96 InvalidSuffix, 96 InvalidSuffix,
97 InvalidBlockAttr,
97} 98}
98 99
99#[derive(Debug, Clone, PartialEq, Eq, Hash)] 100#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -136,6 +137,9 @@ impl fmt::Display for SyntaxErrorKind {
136 UnicodeEscapeOutOfRange => write!(f, "Unicode escape code should be at most 0x10FFFF"), 137 UnicodeEscapeOutOfRange => write!(f, "Unicode escape code should be at most 0x10FFFF"),
137 UnclosedString => write!(f, "Unclosed string literal"), 138 UnclosedString => write!(f, "Unclosed string literal"),
138 InvalidSuffix => write!(f, "Invalid literal suffix"), 139 InvalidSuffix => write!(f, "Invalid literal suffix"),
140 InvalidBlockAttr => {
141 write!(f, "A block in this position cannot accept inner attributes")
142 }
139 ParseError(msg) => write!(f, "{}", msg.0), 143 ParseError(msg) => write!(f, "{}", msg.0),
140 } 144 }
141 } 145 }
diff --git a/crates/ra_syntax/tests/data/parser/err/0031_block_inner_attrs.rs b/crates/ra_syntax/tests/data/parser/err/0031_block_inner_attrs.rs
new file mode 100644
index 000000000..6a04f2d0a
--- /dev/null
+++ b/crates/ra_syntax/tests/data/parser/err/0031_block_inner_attrs.rs
@@ -0,0 +1,15 @@
1fn block() {
2 let inner = {
3 #![doc("Inner attributes not allowed here")]
4 //! Nor are ModuleDoc comments
5 };
6 if true {
7 #![doc("Nor here")]
8 #![doc("We error on each attr")]
9 //! Nor are ModuleDoc comments
10 }
11 while true {
12 #![doc("Nor here")]
13 //! Nor are ModuleDoc comments
14 }
15}
diff --git a/crates/ra_syntax/tests/data/parser/err/0031_block_inner_attrs.txt b/crates/ra_syntax/tests/data/parser/err/0031_block_inner_attrs.txt
new file mode 100644
index 000000000..086aa79ac
--- /dev/null
+++ b/crates/ra_syntax/tests/data/parser/err/0031_block_inner_attrs.txt
@@ -0,0 +1,114 @@
1SOURCE_FILE@[0; 350)
2 FN_DEF@[0; 349)
3 FN_KW@[0; 2)
4 WHITESPACE@[2; 3)
5 NAME@[3; 8)
6 IDENT@[3; 8) "block"
7 PARAM_LIST@[8; 10)
8 L_PAREN@[8; 9)
9 R_PAREN@[9; 10)
10 WHITESPACE@[10; 11)
11 BLOCK@[11; 349)
12 L_CURLY@[11; 12)
13 WHITESPACE@[12; 17)
14 LET_STMT@[17; 129)
15 LET_KW@[17; 20)
16 WHITESPACE@[20; 21)
17 BIND_PAT@[21; 26)
18 NAME@[21; 26)
19 IDENT@[21; 26) "inner"
20 WHITESPACE@[26; 27)
21 EQ@[27; 28)
22 WHITESPACE@[28; 29)
23 BLOCK_EXPR@[29; 128)
24 BLOCK@[29; 128)
25 L_CURLY@[29; 30)
26 WHITESPACE@[30; 39)
27 err: `A block in this position cannot accept inner attributes`
28 ATTR@[39; 83)
29 POUND@[39; 40)
30 EXCL@[40; 41)
31 TOKEN_TREE@[41; 83)
32 L_BRACK@[41; 42)
33 IDENT@[42; 45) "doc"
34 TOKEN_TREE@[45; 82)
35 L_PAREN@[45; 46)
36 STRING@[46; 81)
37 R_PAREN@[81; 82)
38 R_BRACK@[82; 83)
39 WHITESPACE@[83; 92)
40 COMMENT@[92; 122)
41 WHITESPACE@[122; 127)
42 R_CURLY@[127; 128)
43 SEMI@[128; 129)
44 WHITESPACE@[129; 134)
45 EXPR_STMT@[134; 257)
46 IF_EXPR@[134; 257)
47 IF_KW@[134; 136)
48 WHITESPACE@[136; 137)
49 CONDITION@[137; 141)
50 LITERAL@[137; 141)
51 TRUE_KW@[137; 141)
52 WHITESPACE@[141; 142)
53 BLOCK@[142; 257)
54 L_CURLY@[142; 143)
55 WHITESPACE@[143; 152)
56 err: `A block in this position cannot accept inner attributes`
57 ATTR@[152; 171)
58 POUND@[152; 153)
59 EXCL@[153; 154)
60 TOKEN_TREE@[154; 171)
61 L_BRACK@[154; 155)
62 IDENT@[155; 158) "doc"
63 TOKEN_TREE@[158; 170)
64 L_PAREN@[158; 159)
65 STRING@[159; 169)
66 R_PAREN@[169; 170)
67 R_BRACK@[170; 171)
68 WHITESPACE@[171; 180)
69 err: `A block in this position cannot accept inner attributes`
70 ATTR@[180; 212)
71 POUND@[180; 181)
72 EXCL@[181; 182)
73 TOKEN_TREE@[182; 212)
74 L_BRACK@[182; 183)
75 IDENT@[183; 186) "doc"
76 TOKEN_TREE@[186; 211)
77 L_PAREN@[186; 187)
78 STRING@[187; 210)
79 R_PAREN@[210; 211)
80 R_BRACK@[211; 212)
81 WHITESPACE@[212; 221)
82 COMMENT@[221; 251)
83 WHITESPACE@[251; 256)
84 R_CURLY@[256; 257)
85 WHITESPACE@[257; 262)
86 WHILE_EXPR@[262; 347)
87 WHILE_KW@[262; 267)
88 WHITESPACE@[267; 268)
89 CONDITION@[268; 272)
90 LITERAL@[268; 272)
91 TRUE_KW@[268; 272)
92 WHITESPACE@[272; 273)
93 BLOCK@[273; 347)
94 L_CURLY@[273; 274)
95 WHITESPACE@[274; 283)
96 err: `A block in this position cannot accept inner attributes`
97 ATTR@[283; 302)
98 POUND@[283; 284)
99 EXCL@[284; 285)
100 TOKEN_TREE@[285; 302)
101 L_BRACK@[285; 286)
102 IDENT@[286; 289) "doc"
103 TOKEN_TREE@[289; 301)
104 L_PAREN@[289; 290)
105 STRING@[290; 300)
106 R_PAREN@[300; 301)
107 R_BRACK@[301; 302)
108 WHITESPACE@[302; 311)
109 COMMENT@[311; 341)
110 WHITESPACE@[341; 346)
111 R_CURLY@[346; 347)
112 WHITESPACE@[347; 348)
113 R_CURLY@[348; 349)
114 WHITESPACE@[349; 350)
diff --git a/crates/ra_syntax/tests/data/parser/ok/0045_block_inner_attrs.rs b/crates/ra_syntax/tests/data/parser/ok/0045_block_inner_attrs.rs
new file mode 100644
index 000000000..88df8138e
--- /dev/null
+++ b/crates/ra_syntax/tests/data/parser/ok/0045_block_inner_attrs.rs
@@ -0,0 +1,20 @@
1fn block() {
2 #![doc("Inner attributes allowed here")]
3 //! As are ModuleDoc style comments
4 {
5 #![doc("Inner attributes are allowed in blocks used as statements")]
6 #![doc("Being validated is not affected by duplcates")]
7 //! As are ModuleDoc style comments
8 };
9 {
10 #![doc("Inner attributes are allowed in blocks when they are the last statement of another block")]
11 //! As are ModuleDoc style comments
12 }
13}
14
15// https://github.com/rust-analyzer/rust-analyzer/issues/689
16impl Whatever {
17 fn salsa_event(&self, event_fn: impl Fn() -> Event<Self>) {
18 #![allow(unused_variables)] // this is `inner_attr` of the block
19 }
20}
diff --git a/crates/ra_syntax/tests/data/parser/ok/0045_block_inner_attrs.txt b/crates/ra_syntax/tests/data/parser/ok/0045_block_inner_attrs.txt
new file mode 100644
index 000000000..a1ba645ef
--- /dev/null
+++ b/crates/ra_syntax/tests/data/parser/ok/0045_block_inner_attrs.txt
@@ -0,0 +1,167 @@
1SOURCE_FILE@[0; 686)
2 FN_DEF@[0; 461)
3 FN_KW@[0; 2)
4 WHITESPACE@[2; 3)
5 NAME@[3; 8)
6 IDENT@[3; 8) "block"
7 PARAM_LIST@[8; 10)
8 L_PAREN@[8; 9)
9 R_PAREN@[9; 10)
10 WHITESPACE@[10; 11)
11 BLOCK@[11; 461)
12 L_CURLY@[11; 12)
13 WHITESPACE@[12; 17)
14 ATTR@[17; 57)
15 POUND@[17; 18)
16 EXCL@[18; 19)
17 TOKEN_TREE@[19; 57)
18 L_BRACK@[19; 20)
19 IDENT@[20; 23) "doc"
20 TOKEN_TREE@[23; 56)
21 L_PAREN@[23; 24)
22 STRING@[24; 55)
23 R_PAREN@[55; 56)
24 R_BRACK@[56; 57)
25 WHITESPACE@[57; 62)
26 COMMENT@[62; 97)
27 WHITESPACE@[97; 102)
28 EXPR_STMT@[102; 295)
29 BLOCK_EXPR@[102; 294)
30 BLOCK@[102; 294)
31 L_CURLY@[102; 103)
32 WHITESPACE@[103; 112)
33 ATTR@[112; 180)
34 POUND@[112; 113)
35 EXCL@[113; 114)
36 TOKEN_TREE@[114; 180)
37 L_BRACK@[114; 115)
38 IDENT@[115; 118) "doc"
39 TOKEN_TREE@[118; 179)
40 L_PAREN@[118; 119)
41 STRING@[119; 178)
42 R_PAREN@[178; 179)
43 R_BRACK@[179; 180)
44 WHITESPACE@[180; 189)
45 ATTR@[189; 244)
46 POUND@[189; 190)
47 EXCL@[190; 191)
48 TOKEN_TREE@[191; 244)
49 L_BRACK@[191; 192)
50 IDENT@[192; 195) "doc"
51 TOKEN_TREE@[195; 243)
52 L_PAREN@[195; 196)
53 STRING@[196; 242)
54 R_PAREN@[242; 243)
55 R_BRACK@[243; 244)
56 WHITESPACE@[244; 253)
57 COMMENT@[253; 288)
58 WHITESPACE@[288; 293)
59 R_CURLY@[293; 294)
60 SEMI@[294; 295)
61 WHITESPACE@[295; 300)
62 BLOCK_EXPR@[300; 459)
63 BLOCK@[300; 459)
64 L_CURLY@[300; 301)
65 WHITESPACE@[301; 310)
66 ATTR@[310; 409)
67 POUND@[310; 311)
68 EXCL@[311; 312)
69 TOKEN_TREE@[312; 409)
70 L_BRACK@[312; 313)
71 IDENT@[313; 316) "doc"
72 TOKEN_TREE@[316; 408)
73 L_PAREN@[316; 317)
74 STRING@[317; 407)
75 R_PAREN@[407; 408)
76 R_BRACK@[408; 409)
77 WHITESPACE@[409; 418)
78 COMMENT@[418; 453)
79 WHITESPACE@[453; 458)
80 R_CURLY@[458; 459)
81 WHITESPACE@[459; 460)
82 R_CURLY@[460; 461)
83 WHITESPACE@[461; 463)
84 COMMENT@[463; 523)
85 WHITESPACE@[523; 524)
86 IMPL_BLOCK@[524; 685)
87 IMPL_KW@[524; 528)
88 WHITESPACE@[528; 529)
89 PATH_TYPE@[529; 537)
90 PATH@[529; 537)
91 PATH_SEGMENT@[529; 537)
92 NAME_REF@[529; 537)
93 IDENT@[529; 537) "Whatever"
94 WHITESPACE@[537; 538)
95 ITEM_LIST@[538; 685)
96 L_CURLY@[538; 539)
97 WHITESPACE@[539; 544)
98 FN_DEF@[544; 683)
99 FN_KW@[544; 546)
100 WHITESPACE@[546; 547)
101 NAME@[547; 558)
102 IDENT@[547; 558) "salsa_event"
103 PARAM_LIST@[558; 601)
104 L_PAREN@[558; 559)
105 SELF_PARAM@[559; 564)
106 AMP@[559; 560)
107 SELF_KW@[560; 564)
108 COMMA@[564; 565)
109 WHITESPACE@[565; 566)
110 PARAM@[566; 600)
111 BIND_PAT@[566; 574)
112 NAME@[566; 574)
113 IDENT@[566; 574) "event_fn"
114 COLON@[574; 575)
115 WHITESPACE@[575; 576)
116 IMPL_TRAIT_TYPE@[576; 600)
117 IMPL_KW@[576; 580)
118 WHITESPACE@[580; 581)
119 PATH_TYPE@[581; 600)
120 PATH@[581; 600)
121 PATH_SEGMENT@[581; 600)
122 NAME_REF@[581; 583)
123 IDENT@[581; 583) "Fn"
124 PARAM_LIST@[583; 585)
125 L_PAREN@[583; 584)
126 R_PAREN@[584; 585)
127 WHITESPACE@[585; 586)
128 RET_TYPE@[586; 600)
129 THIN_ARROW@[586; 588)
130 WHITESPACE@[588; 589)
131 PATH_TYPE@[589; 600)
132 PATH@[589; 600)
133 PATH_SEGMENT@[589; 600)
134 NAME_REF@[589; 594)
135 IDENT@[589; 594) "Event"
136 TYPE_ARG_LIST@[594; 600)
137 L_ANGLE@[594; 595)
138 TYPE_ARG@[595; 599)
139 PATH_TYPE@[595; 599)
140 PATH@[595; 599)
141 PATH_SEGMENT@[595; 599)
142 NAME_REF@[595; 599)
143 IDENT@[595; 599) "Self"
144 R_ANGLE@[599; 600)
145 R_PAREN@[600; 601)
146 WHITESPACE@[601; 602)
147 BLOCK@[602; 683)
148 L_CURLY@[602; 603)
149 WHITESPACE@[603; 612)
150 ATTR@[612; 639)
151 POUND@[612; 613)
152 EXCL@[613; 614)
153 TOKEN_TREE@[614; 639)
154 L_BRACK@[614; 615)
155 IDENT@[615; 620) "allow"
156 TOKEN_TREE@[620; 638)
157 L_PAREN@[620; 621)
158 IDENT@[621; 637) "unused_variables"
159 R_PAREN@[637; 638)
160 R_BRACK@[638; 639)
161 WHITESPACE@[639; 640)
162 COMMENT@[640; 677)
163 WHITESPACE@[677; 682)
164 R_CURLY@[682; 683)
165 WHITESPACE@[683; 684)
166 R_CURLY@[684; 685)
167 WHITESPACE@[685; 686)