From b6560e3ebb80a7a96b3c3598ecba232f799fe93f Mon Sep 17 00:00:00 2001 From: adamrk Date: Tue, 28 Apr 2020 10:23:45 +0200 Subject: Treat comments beginning with four slashes as regular line comments --- crates/ra_syntax/src/ast.rs | 15 +++++++++++++++ crates/ra_syntax/src/ast/tokens.rs | 1 + 2 files changed, 16 insertions(+) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 7fca5661e..a716e525b 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -242,6 +242,21 @@ fn test_comments_preserve_trailing_whitespace() { ); } +#[test] +fn test_four_slash_line_comment() { + let file = SourceFile::parse( + r#" + //// too many slashes to be a doc comment + /// doc comment + mod foo {} + "#, + ) + .ok() + .unwrap(); + let module = file.syntax().descendants().find_map(Module::cast).unwrap(); + assert_eq!("doc comment", module.doc_comment_text().unwrap()); +} + #[test] fn test_where_predicates() { fn assert_bound(text: &str, bound: Option) { diff --git a/crates/ra_syntax/src/ast/tokens.rs b/crates/ra_syntax/src/ast/tokens.rs index 3865729b8..481813e38 100644 --- a/crates/ra_syntax/src/ast/tokens.rs +++ b/crates/ra_syntax/src/ast/tokens.rs @@ -48,6 +48,7 @@ pub enum CommentPlacement { const COMMENT_PREFIX_TO_KIND: &[(&str, CommentKind)] = { use {CommentPlacement::*, CommentShape::*}; &[ + ("////", CommentKind { shape: Line, doc: None }), ("///", CommentKind { shape: Line, doc: Some(Outer) }), ("//!", CommentKind { shape: Line, doc: Some(Inner) }), ("/**", CommentKind { shape: Block, doc: Some(Outer) }), -- cgit v1.2.3 From 0bd7d81805df16c8d1f200b13e485f6dda22f104 Mon Sep 17 00:00:00 2001 From: adamrk Date: Tue, 28 Apr 2020 21:13:37 +0200 Subject: Fix comment prefix method for four slash comments --- crates/ra_syntax/src/ast/tokens.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/tokens.rs b/crates/ra_syntax/src/ast/tokens.rs index 481813e38..74906d8a6 100644 --- a/crates/ra_syntax/src/ast/tokens.rs +++ b/crates/ra_syntax/src/ast/tokens.rs @@ -13,7 +13,12 @@ impl Comment { } pub fn prefix(&self) -> &'static str { - prefix_by_kind(self.kind()) + for (prefix, k) in COMMENT_PREFIX_TO_KIND.iter() { + if *k == self.kind() && self.text().starts_with(prefix) { + return prefix; + } + } + unreachable!() } } @@ -70,15 +75,6 @@ fn kind_by_prefix(text: &str) -> CommentKind { panic!("bad comment text: {:?}", text) } -fn prefix_by_kind(kind: CommentKind) -> &'static str { - for (prefix, k) in COMMENT_PREFIX_TO_KIND.iter() { - if *k == kind { - return prefix; - } - } - unreachable!() -} - impl Whitespace { pub fn spans_multiple_lines(&self) -> bool { let text = self.text(); -- cgit v1.2.3 From b4dd4752570d5f1ba24f7ffda69be4b1c935cd04 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 29 Apr 2020 14:49:54 +0200 Subject: More principled approach for finding From trait --- crates/ra_syntax/src/ast/make.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index ee0f5cc40..492088353 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -22,8 +22,7 @@ pub fn path_unqualified(segment: ast::PathSegment) -> ast::Path { pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path { path_from_text(&format!("{}::{}", qual, segment)) } - -pub fn path_from_text(text: &str) -> ast::Path { +fn path_from_text(text: &str) -> ast::Path { ast_from_text(text) } -- cgit v1.2.3 From 0af727da91e7ff3c8ed5518cb7e005e8d4f939b0 Mon Sep 17 00:00:00 2001 From: John Renner Date: Mon, 27 Apr 2020 10:02:47 -0700 Subject: Validate the location of `crate` in paths --- crates/ra_syntax/src/ast/generated/nodes.rs | 1 + crates/ra_syntax/src/validation.rs | 39 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs index 2cb3ad011..3b5e05af9 100644 --- a/crates/ra_syntax/src/ast/generated/nodes.rs +++ b/crates/ra_syntax/src/ast/generated/nodes.rs @@ -1249,6 +1249,7 @@ pub struct PathSegment { } impl PathSegment { pub fn coloncolon_token(&self) -> Option { support::token(&self.syntax, T![::]) } + pub fn crate_token(&self) -> Option { support::token(&self.syntax, T![crate]) } pub fn l_angle_token(&self) -> Option { support::token(&self.syntax, T![<]) } pub fn name_ref(&self) -> Option { support::child(&self.syntax) } pub fn type_arg_list(&self) -> Option { support::child(&self.syntax) } diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index 5e93895ec..a30bc97bb 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs @@ -96,6 +96,7 @@ pub(crate) fn validate(root: &SyntaxNode) -> Vec { ast::RecordField(it) => validate_numeric_name(it.name_ref(), &mut errors), ast::Visibility(it) => validate_visibility(it, &mut errors), ast::RangeExpr(it) => validate_range_expr(it, &mut errors), + ast::PathSegment(it) => validate_crate_keyword_in_path_segment(it, &mut errors), _ => (), } } @@ -222,3 +223,41 @@ fn validate_range_expr(expr: ast::RangeExpr, errors: &mut Vec) { )); } } + +fn validate_crate_keyword_in_path_segment( + segment: ast::PathSegment, + errors: &mut Vec, +) { + const ERR_MSG: &str = "The `crate` keyword is only allowed as the first segment of a path"; + + let crate_token = match segment.crate_token() { + None => return, + Some(it) => it, + }; + + // Disallow both ::crate and foo::crate + let path = segment.parent_path(); + if segment.coloncolon_token().is_some() || path.qualifier().is_some() { + errors.push(SyntaxError::new(ERR_MSG, crate_token.text_range())); + return; + } + + // We now know that the path variable describes a complete path. + // For expressions and types, validation is complete, but we still have + // to handle UseItems like this: + // use foo:{crate}; + // so we crawl upwards looking for any preceding paths on `UseTree`s + for node in path.syntax().ancestors().skip(1) { + match_ast! { + match node { + ast::UseTree(it) => if let Some(tree_path) = it.path() { + if tree_path != path { + errors.push(SyntaxError::new(ERR_MSG, crate_token.text_range())); + } + }, + ast::UseTreeList(_it) => continue, + _ => return, + } + }; + } +} -- cgit v1.2.3 From 513a3615f6d462852c0135dc4ac30a2086e25c5a Mon Sep 17 00:00:00 2001 From: John Renner Date: Thu, 30 Apr 2020 10:41:24 -0700 Subject: Report invalid, nested, multi-segment crate-paths Specifically, things like: use foo::{crate::bar}; Are now being caught, when before we only caught: use foo::{crate}; --- crates/ra_syntax/src/validation.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index a30bc97bb..f0b3dec63 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs @@ -236,21 +236,40 @@ fn validate_crate_keyword_in_path_segment( }; // Disallow both ::crate and foo::crate - let path = segment.parent_path(); + let mut path = segment.parent_path(); if segment.coloncolon_token().is_some() || path.qualifier().is_some() { errors.push(SyntaxError::new(ERR_MSG, crate_token.text_range())); return; } - // We now know that the path variable describes a complete path. // For expressions and types, validation is complete, but we still have - // to handle UseItems like this: - // use foo:{crate}; - // so we crawl upwards looking for any preceding paths on `UseTree`s + // to handle invalid UseItems like this: + // + // use foo:{crate::bar::baz}; + // + // To handle this we must inspect the parent `UseItem`s and `UseTree`s + // but right now we're looking deep inside the nested `Path` nodes because + // `Path`s are left-associative: + // + // ((crate)::bar)::baz) + // ^ current value of path + // + // So we need to climb to the top + while let Some(parent) = path.parent_path() { + path = parent; + } + + // Now that we've found the whole path we need to see if there's a prefix + // somewhere in the UseTree hierarchy. This check is arbitrarily deep + // because rust allows arbitrary nesting like so: + // + // use {foo::{{{{crate::bar::baz}}}}}; for node in path.syntax().ancestors().skip(1) { match_ast! { match node { ast::UseTree(it) => if let Some(tree_path) = it.path() { + // Even a top-level path exists within a `UseTree` so we must explicitly + // allow our path but disallow anything else if tree_path != path { errors.push(SyntaxError::new(ERR_MSG, crate_token.text_range())); } -- cgit v1.2.3 From 15cfa9a808be820ceafc2e957ea8532e8ec68f00 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Apr 2020 21:36:31 +0200 Subject: Fix a bunch of false-positives in join-lines --- crates/ra_syntax/src/ast/expr_extensions.rs | 5 ++++- crates/ra_syntax/src/ast/generated/nodes.rs | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index 93aa3d45f..ecf74fd36 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -368,12 +368,15 @@ impl ast::BlockExpr { /// const FOO: () = { stand_alone }; /// ``` pub fn is_standalone(&self) -> bool { + if self.unsafe_token().is_some() || self.async_token().is_some() { + return false; + } let kind = match self.syntax().parent() { None => return true, Some(it) => it.kind(), }; match kind { - FN_DEF | MATCH_ARM | IF_EXPR | WHILE_EXPR | LOOP_EXPR | TRY_BLOCK_EXPR => false, + FN_DEF | IF_EXPR | WHILE_EXPR | LOOP_EXPR | TRY_BLOCK_EXPR => false, _ => true, } } diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs index 3b5e05af9..d2253d4af 100644 --- a/crates/ra_syntax/src/ast/generated/nodes.rs +++ b/crates/ra_syntax/src/ast/generated/nodes.rs @@ -554,6 +554,7 @@ impl ast::AttrsOwner for BlockExpr {} impl BlockExpr { pub fn label(&self) -> Option