|
| 1 | +//! This module contains helpers for walking all types of |
| 2 | +//! a signature, while preserving spans as much as possible |
| 3 | +
|
| 4 | +use std::ops::ControlFlow; |
| 5 | + |
| 6 | +use rustc_hir::{def::DefKind, def_id::LocalDefId}; |
| 7 | +use rustc_middle::ty::{self, TyCtxt}; |
| 8 | +use rustc_span::Span; |
| 9 | +use rustc_type_ir::visit::TypeVisitable; |
| 10 | + |
| 11 | +pub trait SpannedTypeVisitor<'tcx> { |
| 12 | + type BreakTy = !; |
| 13 | + fn visit( |
| 14 | + &mut self, |
| 15 | + span: Span, |
| 16 | + value: impl TypeVisitable<TyCtxt<'tcx>>, |
| 17 | + ) -> ControlFlow<Self::BreakTy>; |
| 18 | +} |
| 19 | + |
| 20 | +pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>( |
| 21 | + tcx: TyCtxt<'tcx>, |
| 22 | + item: LocalDefId, |
| 23 | + visitor: &mut V, |
| 24 | +) -> ControlFlow<V::BreakTy> { |
| 25 | + let kind = tcx.def_kind(item); |
| 26 | + trace!(?kind); |
| 27 | + match kind { |
| 28 | + DefKind::Coroutine => { |
| 29 | + match tcx.type_of(item).instantiate_identity().kind() { |
| 30 | + ty::Coroutine(_, args, _) => visitor.visit(tcx.def_span(item), args.as_coroutine().sig())?, |
| 31 | + _ => bug!(), |
| 32 | + } |
| 33 | + for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) { |
| 34 | + visitor.visit(span, pred)?; |
| 35 | + } |
| 36 | + } |
| 37 | + // Walk over the signature of the function-like |
| 38 | + DefKind::Closure | DefKind::AssocFn | DefKind::Fn => { |
| 39 | + let ty_sig = match kind { |
| 40 | + DefKind::Closure => match tcx.type_of(item).instantiate_identity().kind() { |
| 41 | + ty::Closure(_, args) => args.as_closure().sig(), |
| 42 | + _ => bug!(), |
| 43 | + }, |
| 44 | + _ => tcx.fn_sig(item).instantiate_identity(), |
| 45 | + }; |
| 46 | + let hir_sig = tcx.hir().get_by_def_id(item).fn_decl().unwrap(); |
| 47 | + // Walk over the inputs and outputs manually in order to get good spans for them. |
| 48 | + visitor.visit(hir_sig.output.span(), ty_sig.output()); |
| 49 | + for (hir, ty) in hir_sig.inputs.iter().zip(ty_sig.inputs().iter()) { |
| 50 | + visitor.visit(hir.span, ty.map_bound(|x| *x))?; |
| 51 | + } |
| 52 | + for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) { |
| 53 | + visitor.visit(span, pred)?; |
| 54 | + } |
| 55 | + } |
| 56 | + // Walk over the type behind the alias |
| 57 | + DefKind::TyAlias {..} | DefKind::AssocTy | |
| 58 | + // Walk over the type of the item |
| 59 | + DefKind::Static(_) | DefKind::Const | DefKind::AssocConst | DefKind::AnonConst => { |
| 60 | + let span = match tcx.hir().get_by_def_id(item).ty() { |
| 61 | + Some(ty) => ty.span, |
| 62 | + _ => tcx.def_span(item), |
| 63 | + }; |
| 64 | + visitor.visit(span, tcx.type_of(item).instantiate_identity()); |
| 65 | + for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) { |
| 66 | + visitor.visit(span, pred)?; |
| 67 | + } |
| 68 | + } |
| 69 | + DefKind::OpaqueTy => { |
| 70 | + for (pred, span) in tcx.explicit_item_bounds(item).instantiate_identity_iter_copied() { |
| 71 | + visitor.visit(span, pred)?; |
| 72 | + } |
| 73 | + } |
| 74 | + // Look at field types |
| 75 | + DefKind::Struct | DefKind::Union | DefKind::Enum => { |
| 76 | + let span = tcx.def_ident_span(item).unwrap(); |
| 77 | + visitor.visit(span, tcx.type_of(item).instantiate_identity()); |
| 78 | + for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) { |
| 79 | + visitor.visit(span, pred)?; |
| 80 | + } |
| 81 | + } |
| 82 | + // Does not have a syntactical signature |
| 83 | + DefKind::InlineConst => {} |
| 84 | + DefKind::Impl { of_trait } => { |
| 85 | + if of_trait { |
| 86 | + let span = tcx.hir().get_by_def_id(item).expect_item().expect_impl().of_trait.unwrap().path.span; |
| 87 | + let args = &tcx.impl_trait_ref(item).unwrap().instantiate_identity().args[1..]; |
| 88 | + visitor.visit(span, args)?; |
| 89 | + } |
| 90 | + let span = match tcx.hir().get_by_def_id(item).ty() { |
| 91 | + Some(ty) => ty.span, |
| 92 | + _ => tcx.def_span(item), |
| 93 | + }; |
| 94 | + visitor.visit(span, tcx.type_of(item).instantiate_identity()); |
| 95 | + for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) { |
| 96 | + visitor.visit(span, pred)?; |
| 97 | + }} |
| 98 | + DefKind::Trait => { |
| 99 | + for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) { |
| 100 | + visitor.visit(span, pred)?; |
| 101 | + } |
| 102 | + } |
| 103 | + DefKind::TraitAlias => { |
| 104 | + for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) { |
| 105 | + visitor.visit(span, pred)?; |
| 106 | + } |
| 107 | + } |
| 108 | + | DefKind::Variant |
| 109 | + | DefKind::ForeignTy |
| 110 | + | DefKind::TyParam |
| 111 | + | DefKind::ConstParam |
| 112 | + | DefKind::Ctor(_, _) |
| 113 | + | DefKind::Field |
| 114 | + | DefKind::LifetimeParam => { |
| 115 | + span_bug!( |
| 116 | + tcx.def_span(item), |
| 117 | + "{kind:?} has not seen any uses of `walk_types` yet, ping oli-obk if you'd like any help" |
| 118 | + ) |
| 119 | + } |
| 120 | + // These don't have any types. |
| 121 | + | DefKind::ExternCrate |
| 122 | + | DefKind::ForeignMod |
| 123 | + | DefKind::Macro(_) |
| 124 | + | DefKind::GlobalAsm |
| 125 | + | DefKind::Mod |
| 126 | + | DefKind::Use => {} |
| 127 | + } |
| 128 | + ControlFlow::Continue(()) |
| 129 | +} |
0 commit comments