1 //! FIXME: write short doc here
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,
10 use rustc_lexer::unescape::{
11 self, unescape_byte, unescape_byte_literal, unescape_char, unescape_literal, Mode,
13 use std::convert::TryFrom;
15 fn rustc_unescape_error_to_string(err: unescape::EscapeError) -> &'static str {
16 use unescape::EscapeError as EE;
19 let err_message = match err {
21 "Literal must not be empty"
23 EE::MoreThanOneChar => {
24 "Literal must be one character long"
27 "Character must be escaped: `\\`"
29 EE::InvalidEscape => {
32 EE::BareCarriageReturn | EE::BareCarriageReturnInRawString => {
33 "Character must be escaped: `\r`"
35 EE::EscapeOnlyChar => {
36 "Escape character `\\` must be escaped itself"
38 EE::TooShortHexEscape => {
39 "ASCII hex escape code must have exactly two digits"
41 EE::InvalidCharInHexEscape => {
42 "ASCII hex escape code must contain only hex characters"
44 EE::OutOfRangeHexEscape => {
45 "ASCII hex escape code must be at most 0x7F"
47 EE::NoBraceInUnicodeEscape => {
48 "Missing `{` to begin the unicode escape"
50 EE::InvalidCharInUnicodeEscape => {
51 "Unicode escape must contain only hex characters and underscores"
53 EE::EmptyUnicodeEscape => {
54 "Unicode escape must not be empty"
56 EE::UnclosedUnicodeEscape => {
57 "Missing `}` to terminate the unicode escape"
59 EE::LeadingUnderscoreUnicodeEscape => {
60 "Unicode escape code must not begin with an underscore"
62 EE::OverlongUnicodeEscape => {
63 "Unicode escape code must have at most 6 digits"
65 EE::LoneSurrogateUnicodeEscape => {
66 "Unicode escape code must not be a surrogate"
68 EE::OutOfRangeUnicodeEscape => {
69 "Unicode escape code must be at most 0x10FFFF"
71 EE::UnicodeEscapeInByte => {
72 "Byte literals must not contain unicode escapes"
74 EE::NonAsciiCharInByte | EE::NonAsciiCharInByteString => {
75 "Byte literals must not contain non-ASCII characters"
82 pub(crate) fn validate(root: &SyntaxNode) -> Vec<SyntaxError> {
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()`)
89 let mut errors = Vec::new();
90 for node in root.descendants() {
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),
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))
113 let token = literal.token();
114 let text = token.text().as_str();
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));
124 if let Some(Err(e)) = unquote(text, 2, '\'').map(unescape_byte) {
129 if let Some(Err(e)) = unquote(text, 1, '\'').map(unescape_char) {
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));
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));
155 pub(crate) fn validate_block_structure(root: &SyntaxNode) {
156 let mut stack = Vec::new();
157 for node in root.descendants() {
159 T!['{'] => stack.push(node),
161 if let Some(pair) = stack.pop() {
165 "\nunpaired curleys:\n{}\n{:#?}\n",
170 node.next_sibling().is_none() && pair.prev_sibling().is_none(),
171 "\nfloating curlys at {:?}\nfile:\n{}\nerror:\n{}\n",
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(),
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)
199 fn validate_visibility(vis: ast::Visibility, errors: &mut Vec<SyntaxError>) {
200 let parent = match vis.syntax().parent() {
204 match parent.kind() {
205 FN_DEF | CONST_DEF | TYPE_ALIAS_DEF => (),
209 let impl_def = match parent.parent().and_then(|it| it.parent()).and_then(ast::ImplDef::cast) {
213 if impl_def.target_trait().is_some() {
214 errors.push(SyntaxError::new("Unnecessary visibility qualifier", vis.syntax.text_range()));
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(),
227 fn validate_path_keywords(segment: ast::PathSegment, errors: &mut Vec<SyntaxError>) {
228 use ast::PathSegmentKind;
230 let path = segment.parent_path();
231 let is_path_start = segment.coloncolon_token().is_none() && path.qualifier().is_none();
233 if let Some(token) = segment.self_token() {
235 errors.push(SyntaxError::new(
236 "The `self` keyword is only allowed as the first segment of a path",
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",
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",
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",
269 fn use_prefix(mut path: ast::Path) -> Option<ast::Path> {
270 for node in path.syntax().ancestors().skip(1) {
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);
280 ast::UseTreeList(_it) => continue,
281 ast::Path(parent) => path = parent,
289 fn all_supers(path: &ast::Path) -> bool {
290 let segment = match path.segment() {
292 None => return false,
295 if segment.kind() != Some(PathSegmentKind::SuperKw) {
299 if let Some(ref subpath) = path.qualifier() {
300 return all_supers(subpath);