Skip to content

Commit a00b4c2

Browse files
committed
Add ty_fn_ptr function to create function pointer type
1 parent 0ac5c8a commit a00b4c2

File tree

1 file changed

+41
-1
lines changed
  • src/tools/rust-analyzer/crates/syntax/src/ast

1 file changed

+41
-1
lines changed

src/tools/rust-analyzer/crates/syntax/src/ast/make.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ use parser::{Edition, T};
1515
use rowan::NodeOrToken;
1616
use stdx::{format_to, format_to_acc, never};
1717

18-
use crate::{ast, utils::is_raw_identifier, AstNode, SourceFile, SyntaxKind, SyntaxToken};
18+
use crate::{
19+
ast::{self, Param},
20+
utils::is_raw_identifier,
21+
AstNode, SourceFile, SyntaxKind, SyntaxToken,
22+
};
1923

2024
/// While the parent module defines basic atomic "constructors", the `ext`
2125
/// module defines shortcuts for common things.
@@ -198,6 +202,38 @@ pub fn ty_alias(
198202
ast_from_text(&s)
199203
}
200204

205+
pub fn ty_fn_ptr<I: Iterator<Item = Param>>(
206+
for_lifetime_list: Option<ast::GenericParamList>,
207+
is_unsafe: bool,
208+
abi: Option<ast::Abi>,
209+
params: I,
210+
ret_type: Option<ast::RetType>,
211+
) -> ast::FnPtrType {
212+
let mut s = String::from("type __ = ");
213+
214+
if let Some(list) = for_lifetime_list {
215+
format_to!(s, "for{} ", list);
216+
}
217+
218+
if is_unsafe {
219+
s.push_str("unsafe ");
220+
}
221+
222+
if let Some(abi) = abi {
223+
format_to!(s, "{} ", abi)
224+
}
225+
226+
s.push_str("fn");
227+
228+
format_to!(s, "({})", params.map(|p| p.to_string()).join(", "));
229+
230+
if let Some(ret_type) = ret_type {
231+
format_to!(s, " {}", ret_type);
232+
}
233+
234+
ast_from_text(&s)
235+
}
236+
201237
pub fn assoc_item_list() -> ast::AssocItemList {
202238
ast_from_text("impl C for D {}")
203239
}
@@ -862,6 +898,10 @@ pub fn item_const(
862898
ast_from_text(&format!("{visibility} const {name}: {ty} = {expr};"))
863899
}
864900

901+
pub fn unnamed_param(ty: ast::Type) -> ast::Param {
902+
ast_from_text(&format!("fn f({ty}) {{ }}"))
903+
}
904+
865905
pub fn param(pat: ast::Pat, ty: ast::Type) -> ast::Param {
866906
ast_from_text(&format!("fn f({pat}: {ty}) {{ }}"))
867907
}

0 commit comments

Comments
 (0)