Replace FnLikeNode by FnKind.

This commit is contained in:
Camille GILLOT 2021-10-19 23:31:51 +02:00
parent 05eb6f36f1
commit 6e98688e68
11 changed files with 47 additions and 102 deletions

View file

@ -1,6 +1,7 @@
use crate::def::{CtorKind, DefKind, Res};
use crate::def_id::DefId;
crate use crate::hir_id::{HirId, ItemLocalId};
use crate::intravisit::FnKind;
use crate::LangItem;
use rustc_ast::util::parser::ExprPrecedence;
@ -3258,6 +3259,32 @@ impl<'hir> Node<'hir> {
_ => None,
}
}
pub fn fn_kind(self) -> Option<FnKind<'hir>> {
match self {
Node::Item(i) => match i.kind {
ItemKind::Fn(ref sig, ref generics, _) => {
Some(FnKind::ItemFn(i.ident, generics, sig.header, &i.vis))
}
_ => None,
},
Node::TraitItem(ti) => match ti.kind {
TraitItemKind::Fn(ref sig, TraitFn::Provided(_)) => {
Some(FnKind::Method(ti.ident, sig, None))
}
_ => None,
},
Node::ImplItem(ii) => match ii.kind {
ImplItemKind::Fn(ref sig, _) => Some(FnKind::Method(ii.ident, sig, Some(&ii.vis))),
_ => None,
},
Node::Expr(e) => match e.kind {
ExprKind::Closure(..) => Some(FnKind::Closure),
_ => None,
},
_ => None,
}
}
}
// Some nodes are used a lot. Make sure they don't unintentionally get bigger.

View file

@ -117,6 +117,14 @@ impl<'a> FnKind<'a> {
FnKind::Closure => None,
}
}
pub fn constness(self) -> Constness {
self.header().map_or(Constness::NotConst, |header| header.constness)
}
pub fn asyncness(self) -> IsAsync {
self.header().map_or(IsAsync::NotAsync, |header| header.asyncness)
}
}
/// An abstract representation of the HIR `rustc_middle::hir::map::Map`.