From cb161e77ba7492524e17e077085f1fab7e0dd9b8 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 19 Jun 2023 09:51:28 +0000 Subject: [PATCH] Move some field extraction logic onto a method on `Node` --- compiler/rustc_hir/src/hir.rs | 23 +++++++++++++++++++++ compiler/rustc_ty_utils/src/opaque_types.rs | 10 +++------ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 70fc66947df..5e5001bc8b4 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -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), diff --git a/compiler/rustc_ty_utils/src/opaque_types.rs b/compiler/rustc_ty_utils/src/opaque_types.rs index ab97406452e..5fb5c4ad144 100644 --- a/compiler/rustc_ty_utils/src/opaque_types.rs +++ b/compiler/rustc_ty_utils/src/opaque_types.rs @@ -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()); }