Skip to content

Commit cb161e7

Browse files
committed
Move some field extraction logic onto a method on Node
1 parent 30ff127 commit cb161e7

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

compiler/rustc_hir/src/hir.rs

+23
Original file line numberDiff line numberDiff line change
@@ -3743,6 +3743,29 @@ impl<'hir> Node<'hir> {
37433743
}
37443744
}
37453745

3746+
/// Get the type for constants, assoc types, type aliases and statics.
3747+
pub fn ty(self) -> Option<&'hir Ty<'hir>> {
3748+
match self {
3749+
Node::Item(it) => match it.kind {
3750+
ItemKind::TyAlias(ty, _) | ItemKind::Static(ty, _, _) | ItemKind::Const(ty, _) => {
3751+
Some(ty)
3752+
}
3753+
_ => None,
3754+
},
3755+
Node::TraitItem(it) => match it.kind {
3756+
TraitItemKind::Const(ty, _) => Some(ty),
3757+
TraitItemKind::Type(_, ty) => ty,
3758+
_ => None,
3759+
},
3760+
Node::ImplItem(it) => match it.kind {
3761+
ImplItemKind::Const(ty, _) => Some(ty),
3762+
ImplItemKind::Type(ty) => Some(ty),
3763+
_ => None,
3764+
},
3765+
_ => None,
3766+
}
3767+
}
3768+
37463769
pub fn alias_ty(self) -> Option<&'hir Ty<'hir>> {
37473770
match self {
37483771
Node::Item(Item { kind: ItemKind::TyAlias(ty, ..), .. }) => Some(ty),

compiler/rustc_ty_utils/src/opaque_types.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,9 @@ fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [
177177
}
178178
}
179179
DefKind::AssocTy | DefKind::AssocConst => {
180-
let span = match tcx.hir().get_by_def_id(item) {
181-
rustc_hir::Node::ImplItem(it) => match it.kind {
182-
rustc_hir::ImplItemKind::Const(ty, _) => ty.span,
183-
rustc_hir::ImplItemKind::Type(ty) => ty.span,
184-
other => span_bug!(tcx.def_span(item), "{other:#?}"),
185-
},
186-
other => span_bug!(tcx.def_span(item), "{other:#?}"),
180+
let span = match tcx.hir().get_by_def_id(item).ty() {
181+
Some(ty) => ty.span,
182+
_ => tcx.def_span(item),
187183
};
188184
collector.visit_spanned(span, tcx.type_of(item).subst_identity());
189185
}

0 commit comments

Comments
 (0)