summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/crates/syntax/src
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/rust-analyzer/crates/syntax/src')
-rw-r--r--src/tools/rust-analyzer/crates/syntax/src/ast.rs30
-rw-r--r--src/tools/rust-analyzer/crates/syntax/src/ast/generated/nodes.rs1
-rw-r--r--src/tools/rust-analyzer/crates/syntax/src/ast/make.rs21
-rw-r--r--src/tools/rust-analyzer/crates/syntax/src/lib.rs2
-rw-r--r--src/tools/rust-analyzer/crates/syntax/src/syntax_node.rs1
-rw-r--r--src/tools/rust-analyzer/crates/syntax/src/utils.rs7
6 files changed, 56 insertions, 6 deletions
diff --git a/src/tools/rust-analyzer/crates/syntax/src/ast.rs b/src/tools/rust-analyzer/crates/syntax/src/ast.rs
index 10c045758..385a4e0a3 100644
--- a/src/tools/rust-analyzer/crates/syntax/src/ast.rs
+++ b/src/tools/rust-analyzer/crates/syntax/src/ast.rs
@@ -13,6 +13,8 @@ pub mod prec;
use std::marker::PhantomData;
+use itertools::Either;
+
use crate::{
syntax_node::{SyntaxNode, SyntaxNodeChildren, SyntaxToken},
SyntaxKind,
@@ -98,6 +100,34 @@ impl<N: AstNode> Iterator for AstChildren<N> {
}
}
+impl<L, R> AstNode for Either<L, R>
+where
+ L: AstNode,
+ R: AstNode,
+{
+ fn can_cast(kind: SyntaxKind) -> bool
+ where
+ Self: Sized,
+ {
+ L::can_cast(kind) || R::can_cast(kind)
+ }
+
+ fn cast(syntax: SyntaxNode) -> Option<Self>
+ where
+ Self: Sized,
+ {
+ if L::can_cast(syntax.kind()) {
+ L::cast(syntax).map(Either::Left)
+ } else {
+ R::cast(syntax).map(Either::Right)
+ }
+ }
+
+ fn syntax(&self) -> &SyntaxNode {
+ self.as_ref().either(L::syntax, R::syntax)
+ }
+}
+
mod support {
use super::{AstChildren, AstNode, SyntaxKind, SyntaxNode, SyntaxToken};
diff --git a/src/tools/rust-analyzer/crates/syntax/src/ast/generated/nodes.rs b/src/tools/rust-analyzer/crates/syntax/src/ast/generated/nodes.rs
index a214a5e44..642a3bfc3 100644
--- a/src/tools/rust-analyzer/crates/syntax/src/ast/generated/nodes.rs
+++ b/src/tools/rust-analyzer/crates/syntax/src/ast/generated/nodes.rs
@@ -842,6 +842,7 @@ impl ast::HasAttrs for ClosureExpr {}
impl ClosureExpr {
pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) }
pub fn generic_param_list(&self) -> Option<GenericParamList> { support::child(&self.syntax) }
+ pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) }
pub fn static_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![static]) }
pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) }
pub fn move_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![move]) }
diff --git a/src/tools/rust-analyzer/crates/syntax/src/ast/make.rs b/src/tools/rust-analyzer/crates/syntax/src/ast/make.rs
index d5b329698..5aebe4cd9 100644
--- a/src/tools/rust-analyzer/crates/syntax/src/ast/make.rs
+++ b/src/tools/rust-analyzer/crates/syntax/src/ast/make.rs
@@ -12,7 +12,7 @@
use itertools::Itertools;
use stdx::{format_to, never};
-use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxToken};
+use crate::{ast, utils::is_raw_identifier, AstNode, SourceFile, SyntaxKind, SyntaxToken};
/// While the parent module defines basic atomic "constructors", the `ext`
/// module defines shortcuts for common things.
@@ -111,8 +111,7 @@ pub fn name_ref(name_ref: &str) -> ast::NameRef {
ast_from_text(&format!("fn f() {{ {raw_escape}{name_ref}; }}"))
}
fn raw_ident_esc(ident: &str) -> &'static str {
- let is_keyword = parser::SyntaxKind::from_keyword(ident).is_some();
- if is_keyword && !matches!(ident, "self" | "crate" | "super" | "Self") {
+ if is_raw_identifier(ident) {
"r#"
} else {
""
@@ -520,6 +519,15 @@ pub fn literal_pat(lit: &str) -> ast::LiteralPat {
}
}
+pub fn slice_pat(pats: impl IntoIterator<Item = ast::Pat>) -> ast::SlicePat {
+ let pats_str = pats.into_iter().join(", ");
+ return from_text(&format!("[{pats_str}]"));
+
+ fn from_text(text: &str) -> ast::SlicePat {
+ ast_from_text(&format!("fn f() {{ match () {{{text} => ()}} }}"))
+ }
+}
+
/// Creates a tuple of patterns from an iterator of patterns.
///
/// Invariant: `pats` must be length > 0
@@ -814,6 +822,7 @@ pub fn fn_(
visibility: Option<ast::Visibility>,
fn_name: ast::Name,
type_params: Option<ast::GenericParamList>,
+ where_clause: Option<ast::WhereClause>,
params: ast::ParamList,
body: ast::BlockExpr,
ret_type: Option<ast::RetType>,
@@ -823,6 +832,10 @@ pub fn fn_(
Some(type_params) => format!("{type_params}"),
None => "".into(),
};
+ let where_clause = match where_clause {
+ Some(it) => format!("{it} "),
+ None => "".into(),
+ };
let ret_type = match ret_type {
Some(ret_type) => format!("{ret_type} "),
None => "".into(),
@@ -835,7 +848,7 @@ pub fn fn_(
let async_literal = if is_async { "async " } else { "" };
ast_from_text(&format!(
- "{visibility}{async_literal}fn {fn_name}{type_params}{params} {ret_type}{body}",
+ "{visibility}{async_literal}fn {fn_name}{type_params}{params} {ret_type}{where_clause}{body}",
))
}
diff --git a/src/tools/rust-analyzer/crates/syntax/src/lib.rs b/src/tools/rust-analyzer/crates/syntax/src/lib.rs
index 84c66b27e..6f57cbad6 100644
--- a/src/tools/rust-analyzer/crates/syntax/src/lib.rs
+++ b/src/tools/rust-analyzer/crates/syntax/src/lib.rs
@@ -186,7 +186,7 @@ impl SourceFile {
/// ```
#[macro_export]
macro_rules! match_ast {
- (match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
+ (match $node:ident { $($tt:tt)* }) => { $crate::match_ast!(match ($node) { $($tt)* }) };
(match ($node:expr) {
$( $( $path:ident )::+ ($it:pat) => $res:expr, )*
diff --git a/src/tools/rust-analyzer/crates/syntax/src/syntax_node.rs b/src/tools/rust-analyzer/crates/syntax/src/syntax_node.rs
index a08c01597..2e9e0bc22 100644
--- a/src/tools/rust-analyzer/crates/syntax/src/syntax_node.rs
+++ b/src/tools/rust-analyzer/crates/syntax/src/syntax_node.rs
@@ -48,6 +48,7 @@ impl SyntaxTreeBuilder {
pub fn finish(self) -> Parse<SyntaxNode> {
let (green, errors) = self.finish_raw();
// Disable block validation, see https://github.com/rust-lang/rust-analyzer/pull/10357
+ #[allow(clippy::overly_complex_bool_expr)]
if cfg!(debug_assertions) && false {
let node = SyntaxNode::new_root(green.clone());
crate::validation::validate_block_structure(&node);
diff --git a/src/tools/rust-analyzer/crates/syntax/src/utils.rs b/src/tools/rust-analyzer/crates/syntax/src/utils.rs
index f4c02518b..25f34ea9d 100644
--- a/src/tools/rust-analyzer/crates/syntax/src/utils.rs
+++ b/src/tools/rust-analyzer/crates/syntax/src/utils.rs
@@ -2,7 +2,7 @@
use itertools::Itertools;
-use crate::{ast, match_ast, AstNode};
+use crate::{ast, match_ast, AstNode, SyntaxKind};
pub fn path_to_string_stripping_turbo_fish(path: &ast::Path) -> String {
path.syntax()
@@ -23,6 +23,11 @@ pub fn path_to_string_stripping_turbo_fish(path: &ast::Path) -> String {
.join("::")
}
+pub fn is_raw_identifier(name: &str) -> bool {
+ let is_keyword = SyntaxKind::from_keyword(name).is_some();
+ is_keyword && !matches!(name, "self" | "crate" | "super" | "Self")
+}
+
#[cfg(test)]
mod tests {
use super::path_to_string_stripping_turbo_fish;