Auto merge of #17941 - ChayimFriedman2:pre-closure-to-fn, r=Veykril

Preliminary work for #17940

I split the PR as requested, and made small commits.
This commit is contained in:
bors 2024-08-26 08:09:15 +00:00
commit 0ad26e6025
26 changed files with 816 additions and 167 deletions

View file

@ -8,7 +8,7 @@ use crate::{
ted, AstToken, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken,
};
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct IndentLevel(pub u8);
impl From<u8> for IndentLevel {

View file

@ -486,6 +486,8 @@ impl Fn {
#[inline]
pub fn fn_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![fn]) }
#[inline]
pub fn gen_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![gen]) }
#[inline]
pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) }
}

View file

@ -1035,6 +1035,7 @@ pub fn fn_(
is_async: bool,
is_const: bool,
is_unsafe: bool,
is_gen: bool,
) -> ast::Fn {
let type_params = match type_params {
Some(type_params) => format!("{type_params}"),
@ -1056,9 +1057,10 @@ pub fn fn_(
let async_literal = if is_async { "async " } else { "" };
let const_literal = if is_const { "const " } else { "" };
let unsafe_literal = if is_unsafe { "unsafe " } else { "" };
let gen_literal = if is_gen { "gen " } else { "" };
ast_from_text(&format!(
"{visibility}{async_literal}{const_literal}{unsafe_literal}fn {fn_name}{type_params}{params} {ret_type}{where_clause}{body}",
"{visibility}{const_literal}{async_literal}{gen_literal}{unsafe_literal}fn {fn_name}{type_params}{params} {ret_type}{where_clause}{body}",
))
}
pub fn struct_(

View file

@ -17,7 +17,7 @@ use crate::{
ted, NodeOrToken, SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
};
use super::{RangeItem, RangeOp};
use super::{GenericParam, RangeItem, RangeOp};
impl ast::Lifetime {
pub fn text(&self) -> TokenText<'_> {
@ -822,6 +822,15 @@ pub enum TypeOrConstParam {
Const(ast::ConstParam),
}
impl From<TypeOrConstParam> for GenericParam {
fn from(value: TypeOrConstParam) -> Self {
match value {
TypeOrConstParam::Type(it) => GenericParam::TypeParam(it),
TypeOrConstParam::Const(it) => GenericParam::ConstParam(it),
}
}
}
impl TypeOrConstParam {
pub fn name(&self) -> Option<ast::Name> {
match self {

View file

@ -6,9 +6,9 @@ use parser::Edition;
use crate::{ast, AstNode};
pub fn parse_expr_from_str(s: &str) -> Option<ast::Expr> {
pub fn parse_expr_from_str(s: &str, edition: Edition) -> Option<ast::Expr> {
let s = s.trim();
let file = ast::SourceFile::parse(&format!("const _: () = {s};"), Edition::CURRENT);
let file = ast::SourceFile::parse(&format!("const _: () = {s};"), edition);
let expr = file.syntax_node().descendants().find_map(ast::Expr::cast)?;
if expr.syntax().text() != s {
return None;