Move some field extraction logic onto a method on Node

This commit is contained in:
Oli Scherer 2023-06-19 09:51:28 +00:00
parent 30ff127036
commit cb161e77ba
2 changed files with 26 additions and 7 deletions

View File

@ -3743,6 +3743,29 @@ impl<'hir> Node<'hir> {
}
}
/// Get the type for constants, assoc types, type aliases and statics.
pub fn ty(self) -> Option<&'hir Ty<'hir>> {
match self {
Node::Item(it) => match it.kind {
ItemKind::TyAlias(ty, _) | ItemKind::Static(ty, _, _) | ItemKind::Const(ty, _) => {
Some(ty)
}
_ => None,
},
Node::TraitItem(it) => match it.kind {
TraitItemKind::Const(ty, _) => Some(ty),
TraitItemKind::Type(_, ty) => ty,
_ => None,
},
Node::ImplItem(it) => match it.kind {
ImplItemKind::Const(ty, _) => Some(ty),
ImplItemKind::Type(ty) => Some(ty),
_ => None,
},
_ => None,
}
}
pub fn alias_ty(self) -> Option<&'hir Ty<'hir>> {
match self {
Node::Item(Item { kind: ItemKind::TyAlias(ty, ..), .. }) => Some(ty),

View File

@ -177,13 +177,9 @@ fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [
}
}
DefKind::AssocTy | DefKind::AssocConst => {
let span = match tcx.hir().get_by_def_id(item) {
rustc_hir::Node::ImplItem(it) => match it.kind {
rustc_hir::ImplItemKind::Const(ty, _) => ty.span,
rustc_hir::ImplItemKind::Type(ty) => ty.span,
other => span_bug!(tcx.def_span(item), "{other:#?}"),
},
other => span_bug!(tcx.def_span(item), "{other:#?}"),
let span = match tcx.hir().get_by_def_id(item).ty() {
Some(ty) => ty.span,
_ => tcx.def_span(item),
};
collector.visit_spanned(span, tcx.type_of(item).subst_identity());
}