|
1 | 1 | use rustc_data_structures::fx::FxHashMap; |
2 | 2 | use rustc_hir as hir; |
3 | | -use rustc_hir::def_id::DefId; |
4 | | -use rustc_middle::ty::{self, TyCtxt}; |
| 3 | +use rustc_hir::def_id::{DefId, LocalDefId}; |
| 4 | +use rustc_hir::definitions::DefPathData; |
| 5 | +use rustc_hir::intravisit::{self, Visitor}; |
| 6 | +use rustc_middle::ty::{self, DefIdTree, TyCtxt}; |
5 | 7 |
|
6 | 8 | pub fn provide(providers: &mut ty::query::Providers) { |
7 | 9 | *providers = ty::query::Providers { |
8 | 10 | associated_item, |
9 | 11 | associated_item_def_ids, |
10 | 12 | associated_items, |
| 13 | + associated_items_for_impl_trait_in_trait, |
11 | 14 | impl_item_implementor_ids, |
12 | 15 | ..*providers |
13 | 16 | }; |
@@ -112,3 +115,34 @@ fn associated_item_from_impl_item_ref(impl_item_ref: &hir::ImplItemRef) -> ty::A |
112 | 115 | fn_has_self_parameter: has_self, |
113 | 116 | } |
114 | 117 | } |
| 118 | + |
| 119 | +fn associated_items_for_impl_trait_in_trait(tcx: TyCtxt<'_>, fn_def_id: DefId) -> &'_ [DefId] { |
| 120 | + struct RPITVisitor { |
| 121 | + rpits: Vec<LocalDefId>, |
| 122 | + } |
| 123 | + |
| 124 | + impl<'v> Visitor<'v> for RPITVisitor { |
| 125 | + fn visit_ty(&mut self, ty: &'v hir::Ty<'v>) { |
| 126 | + if let hir::TyKind::OpaqueDef(item_id, _, _) = ty.kind { |
| 127 | + self.rpits.push(item_id.owner_id.def_id) |
| 128 | + } |
| 129 | + intravisit::walk_ty(self, ty) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + let mut visitor = RPITVisitor { rpits: Vec::new() }; |
| 134 | + |
| 135 | + if let Some(output) = tcx.hir().get_fn_output(fn_def_id.expect_local()) { |
| 136 | + visitor.visit_fn_ret_ty(output); |
| 137 | + |
| 138 | + let trait_def_id = tcx.parent(fn_def_id).expect_local(); |
| 139 | + |
| 140 | + tcx.arena.alloc_from_iter(visitor.rpits.iter().map(|_opaque_ty_def_id| { |
| 141 | + let trait_assoc_ty = |
| 142 | + tcx.at(output.span()).create_def(trait_def_id, DefPathData::ImplTraitAssocTy); |
| 143 | + trait_assoc_ty.def_id().to_def_id() |
| 144 | + })) |
| 145 | + } else { |
| 146 | + &[] |
| 147 | + } |
| 148 | +} |
0 commit comments