]> git.lizzy.rs Git - rust.git/blob - crates/ra_syntax/src/validation.rs
436ab033dc8b0fb2a4f1f09ca61068ee24579250
[rust.git] / crates / ra_syntax / src / validation.rs
1 //! FIXME: write short doc here
2
3 mod block;
4
5 use crate::{
6     ast, match_ast, AstNode, SyntaxError,
7     SyntaxKind::{BYTE, BYTE_STRING, CHAR, CONST_DEF, FN_DEF, INT_NUMBER, STRING, TYPE_ALIAS_DEF},
8     SyntaxNode, SyntaxToken, TextSize, T,
9 };
10 use rustc_lexer::unescape::{
11     self, unescape_byte, unescape_byte_literal, unescape_char, unescape_literal, Mode,
12 };
13 use std::convert::TryFrom;
14
15 fn rustc_unescape_error_to_string(err: unescape::EscapeError) -> &'static str {
16     use unescape::EscapeError as EE;
17
18     #[rustfmt::skip]
19     let err_message = match err {
20         EE::ZeroChars => {
21             "Literal must not be empty"
22         }
23         EE::MoreThanOneChar => {
24             "Literal must be one character long"
25         }
26         EE::LoneSlash => {
27             "Character must be escaped: `\\`"
28         }
29         EE::InvalidEscape => {
30             "Invalid escape"
31         }
32         EE::BareCarriageReturn | EE::BareCarriageReturnInRawString => {
33             "Character must be escaped: `\r`"
34         }
35         EE::EscapeOnlyChar => {
36             "Escape character `\\` must be escaped itself"
37         }
38         EE::TooShortHexEscape => {
39             "ASCII hex escape code must have exactly two digits"
40         }
41         EE::InvalidCharInHexEscape => {
42             "ASCII hex escape code must contain only hex characters"
43         }
44         EE::OutOfRangeHexEscape => {
45             "ASCII hex escape code must be at most 0x7F"
46         }
47         EE::NoBraceInUnicodeEscape => {
48             "Missing `{` to begin the unicode escape"
49         }
50         EE::InvalidCharInUnicodeEscape => {
51             "Unicode escape must contain only hex characters and underscores"
52         }
53         EE::EmptyUnicodeEscape => {
54             "Unicode escape must not be empty"
55         }
56         EE::UnclosedUnicodeEscape => {
57             "Missing `}` to terminate the unicode escape"
58         }
59         EE::LeadingUnderscoreUnicodeEscape => {
60             "Unicode escape code must not begin with an underscore"
61         }
62         EE::OverlongUnicodeEscape => {
63             "Unicode escape code must have at most 6 digits"
64         }
65         EE::LoneSurrogateUnicodeEscape => {
66             "Unicode escape code must not be a surrogate"
67         }
68         EE::OutOfRangeUnicodeEscape => {
69             "Unicode escape code must be at most 0x10FFFF"
70         }
71         EE::UnicodeEscapeInByte => {
72             "Byte literals must not contain unicode escapes"
73         }
74         EE::NonAsciiCharInByte | EE::NonAsciiCharInByteString => {
75             "Byte literals must not contain non-ASCII characters"
76         }
77     };
78
79     err_message
80 }
81
82 pub(crate) fn validate(root: &SyntaxNode) -> Vec<SyntaxError> {
83     // FIXME:
84     // * Add validation of character literal containing only a single char
85     // * Add validation of `crate` keyword not appearing in the middle of the symbol path
86     // * Add validation of doc comments are being attached to nodes
87     // * Remove validation of unterminated literals (it is already implemented in `tokenize()`)
88
89     let mut errors = Vec::new();
90     for node in root.descendants() {
91         match_ast! {
92             match node {
93                 ast::Literal(it) => validate_literal(it, &mut errors),
94                 ast::BlockExpr(it) => block::validate_block_expr(it, &mut errors),
95                 ast::FieldExpr(it) => validate_numeric_name(it.name_ref(), &mut errors),
96                 ast::RecordField(it) => validate_numeric_name(it.name_ref(), &mut errors),
97                 ast::Visibility(it) => validate_visibility(it, &mut errors),
98                 ast::RangeExpr(it) => validate_range_expr(it, &mut errors),
99                 ast::PathSegment(it) => validate_path_keywords(it, &mut errors),
100                 _ => (),
101             }
102         }
103     }
104     errors
105 }
106
107 fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) {
108     // FIXME: move this function to outer scope (https://github.com/rust-analyzer/rust-analyzer/pull/2834#discussion_r366196658)
109     fn unquote(text: &str, prefix_len: usize, end_delimiter: char) -> Option<&str> {
110         text.rfind(end_delimiter).and_then(|end| text.get(prefix_len..end))
111     }
112
113     let token = literal.token();
114     let text = token.text().as_str();
115
116     // FIXME: lift this lambda refactor to `fn` (https://github.com/rust-analyzer/rust-analyzer/pull/2834#discussion_r366199205)
117     let mut push_err = |prefix_len, (off, err): (usize, unescape::EscapeError)| {
118         let off = token.text_range().start() + TextSize::try_from(off + prefix_len).unwrap();
119         acc.push(SyntaxError::new_at_offset(rustc_unescape_error_to_string(err), off));
120     };
121
122     match token.kind() {
123         BYTE => {
124             if let Some(Err(e)) = unquote(text, 2, '\'').map(unescape_byte) {
125                 push_err(2, e);
126             }
127         }
128         CHAR => {
129             if let Some(Err(e)) = unquote(text, 1, '\'').map(unescape_char) {
130                 push_err(1, e);
131             }
132         }
133         BYTE_STRING => {
134             if let Some(without_quotes) = unquote(text, 2, '"') {
135                 unescape_byte_literal(without_quotes, Mode::ByteStr, &mut |range, char| {
136                     if let Err(err) = char {
137                         push_err(2, (range.start, err));
138                     }
139                 })
140             }
141         }
142         STRING => {
143             if let Some(without_quotes) = unquote(text, 1, '"') {
144                 unescape_literal(without_quotes, Mode::Str, &mut |range, char| {
145                     if let Err(err) = char {
146                         push_err(1, (range.start, err));
147                     }
148                 })
149             }
150         }
151         _ => (),
152     }
153 }
154
155 pub(crate) fn validate_block_structure(root: &SyntaxNode) {
156     let mut stack = Vec::new();
157     for node in root.descendants() {
158         match node.kind() {
159             T!['{'] => stack.push(node),
160             T!['}'] => {
161                 if let Some(pair) = stack.pop() {
162                     assert_eq!(
163                         node.parent(),
164                         pair.parent(),
165                         "\nunpaired curleys:\n{}\n{:#?}\n",
166                         root.text(),
167                         root,
168                     );
169                     assert!(
170                         node.next_sibling().is_none() && pair.prev_sibling().is_none(),
171                         "\nfloating curlys at {:?}\nfile:\n{}\nerror:\n{}\n",
172                         node,
173                         root.text(),
174                         node.text(),
175                     );
176                 }
177             }
178             _ => (),
179         }
180     }
181 }
182
183 fn validate_numeric_name(name_ref: Option<ast::NameRef>, errors: &mut Vec<SyntaxError>) {
184     if let Some(int_token) = int_token(name_ref) {
185         if int_token.text().chars().any(|c| !c.is_digit(10)) {
186             errors.push(SyntaxError::new(
187                 "Tuple (struct) field access is only allowed through \
188                 decimal integers with no underscores or suffix",
189                 int_token.text_range(),
190             ));
191         }
192     }
193
194     fn int_token(name_ref: Option<ast::NameRef>) -> Option<SyntaxToken> {
195         name_ref?.syntax().first_child_or_token()?.into_token().filter(|it| it.kind() == INT_NUMBER)
196     }
197 }
198
199 fn validate_visibility(vis: ast::Visibility, errors: &mut Vec<SyntaxError>) {
200     let parent = match vis.syntax().parent() {
201         Some(it) => it,
202         None => return,
203     };
204     match parent.kind() {
205         FN_DEF | CONST_DEF | TYPE_ALIAS_DEF => (),
206         _ => return,
207     }
208
209     let impl_def = match parent.parent().and_then(|it| it.parent()).and_then(ast::ImplDef::cast) {
210         Some(it) => it,
211         None => return,
212     };
213     if impl_def.target_trait().is_some() {
214         errors.push(SyntaxError::new("Unnecessary visibility qualifier", vis.syntax.text_range()));
215     }
216 }
217
218 fn validate_range_expr(expr: ast::RangeExpr, errors: &mut Vec<SyntaxError>) {
219     if expr.op_kind() == Some(ast::RangeOp::Inclusive) && expr.end().is_none() {
220         errors.push(SyntaxError::new(
221             "An inclusive range must have an end expression",
222             expr.syntax().text_range(),
223         ));
224     }
225 }
226
227 fn validate_path_keywords(segment: ast::PathSegment, errors: &mut Vec<SyntaxError>) {
228     use ast::PathSegmentKind;
229
230     let path = segment.parent_path();
231     let is_path_start = segment.coloncolon_token().is_none() && path.qualifier().is_none();
232
233     if let Some(token) = segment.self_token() {
234         if !is_path_start {
235             errors.push(SyntaxError::new(
236                 "The `self` keyword is only allowed as the first segment of a path",
237                 token.text_range(),
238             ));
239         }
240     } else if let Some(token) = segment.crate_token() {
241         if !is_path_start || use_prefix(path).is_some() {
242             errors.push(SyntaxError::new(
243                 "The `crate` keyword is only allowed as the first segment of a path",
244                 token.text_range(),
245             ));
246         }
247     } else if let Some(token) = segment.super_token() {
248         if !all_supers(&path) {
249             errors.push(SyntaxError::new(
250                 "The `super` keyword may only be preceded by other `super`s",
251                 token.text_range(),
252             ));
253             return;
254         }
255
256         let mut curr_path = path;
257         while let Some(prefix) = use_prefix(curr_path) {
258             if !all_supers(&prefix) {
259                 errors.push(SyntaxError::new(
260                     "The `super` keyword may only be preceded by other `super`s",
261                     token.text_range(),
262                 ));
263                 return;
264             }
265             curr_path = prefix;
266         }
267     }
268
269     fn use_prefix(mut path: ast::Path) -> Option<ast::Path> {
270         for node in path.syntax().ancestors().skip(1) {
271             match_ast! {
272                 match node {
273                     ast::UseTree(it) => if let Some(tree_path) = it.path() {
274                         // Even a top-level path exists within a `UseTree` so we must explicitly
275                         // allow our path but disallow anything else
276                         if tree_path != path {
277                             return Some(tree_path);
278                         }
279                     },
280                     ast::UseTreeList(_it) => continue,
281                     ast::Path(parent) => path = parent,
282                     _ => return None,
283                 }
284             };
285         }
286         return None;
287     }
288
289     fn all_supers(path: &ast::Path) -> bool {
290         let segment = match path.segment() {
291             Some(it) => it,
292             None => return false,
293         };
294
295         if segment.kind() != Some(PathSegmentKind::SuperKw) {
296             return false;
297         }
298
299         if let Some(ref subpath) = path.qualifier() {
300             return all_supers(subpath);
301         }
302
303         return true;
304     }
305 }