about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGiga Bowser <45986823+Giga-Bowser@users.noreply.github.com>2024-10-24 17:46:14 -0400
committerGiga Bowser <45986823+Giga-Bowser@users.noreply.github.com>2024-10-24 17:46:14 -0400
commita00b4c2a529089b9eeeba140c9a82b872025d801 (patch)
treee0e5e2a40d7897b5d75ed38afabb630469236760
parent0ac5c8a8425c151bd1f68764bd728cf6b83b1b89 (diff)
downloadrust-a00b4c2a529089b9eeeba140c9a82b872025d801.tar.gz
rust-a00b4c2a529089b9eeeba140c9a82b872025d801.zip
Add `ty_fn_ptr` function to create function pointer type
-rw-r--r--src/tools/rust-analyzer/crates/syntax/src/ast/make.rs42
1 files changed, 41 insertions, 1 deletions
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 fcdc97ce327..2ec83d23b27 100644
--- a/src/tools/rust-analyzer/crates/syntax/src/ast/make.rs
+++ b/src/tools/rust-analyzer/crates/syntax/src/ast/make.rs
@@ -15,7 +15,11 @@ use parser::{Edition, T};
 use rowan::NodeOrToken;
 use stdx::{format_to, format_to_acc, never};
 
-use crate::{ast, utils::is_raw_identifier, AstNode, SourceFile, SyntaxKind, SyntaxToken};
+use crate::{
+    ast::{self, Param},
+    utils::is_raw_identifier,
+    AstNode, SourceFile, SyntaxKind, SyntaxToken,
+};
 
 /// While the parent module defines basic atomic "constructors", the `ext`
 /// module defines shortcuts for common things.
@@ -198,6 +202,38 @@ pub fn ty_alias(
     ast_from_text(&s)
 }
 
+pub fn ty_fn_ptr<I: Iterator<Item = Param>>(
+    for_lifetime_list: Option<ast::GenericParamList>,
+    is_unsafe: bool,
+    abi: Option<ast::Abi>,
+    params: I,
+    ret_type: Option<ast::RetType>,
+) -> ast::FnPtrType {
+    let mut s = String::from("type __ = ");
+
+    if let Some(list) = for_lifetime_list {
+        format_to!(s, "for{} ", list);
+    }
+
+    if is_unsafe {
+        s.push_str("unsafe ");
+    }
+
+    if let Some(abi) = abi {
+        format_to!(s, "{} ", abi)
+    }
+
+    s.push_str("fn");
+
+    format_to!(s, "({})", params.map(|p| p.to_string()).join(", "));
+
+    if let Some(ret_type) = ret_type {
+        format_to!(s, " {}", ret_type);
+    }
+
+    ast_from_text(&s)
+}
+
 pub fn assoc_item_list() -> ast::AssocItemList {
     ast_from_text("impl C for D {}")
 }
@@ -862,6 +898,10 @@ pub fn item_const(
     ast_from_text(&format!("{visibility} const {name}: {ty} = {expr};"))
 }
 
+pub fn unnamed_param(ty: ast::Type) -> ast::Param {
+    ast_from_text(&format!("fn f({ty}) {{ }}"))
+}
+
 pub fn param(pat: ast::Pat, ty: ast::Type) -> ast::Param {
     ast_from_text(&format!("fn f({pat}: {ty}) {{ }}"))
 }