2021-11-18 15:44:45 -06:00
|
|
|
use rustc_hir as hir;
|
2022-12-16 14:15:34 -06:00
|
|
|
use rustc_hir::def::DefKind;
|
2023-02-21 04:35:19 -06:00
|
|
|
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
|
2022-10-26 14:38:32 -05:00
|
|
|
use rustc_hir::definitions::DefPathData;
|
|
|
|
use rustc_hir::intravisit::{self, Visitor};
|
2023-02-22 09:51:17 -06:00
|
|
|
use rustc_middle::ty::{self, ImplTraitInTraitData, InternalSubsts, TyCtxt};
|
2023-02-28 10:05:37 -06:00
|
|
|
use rustc_span::symbol::kw;
|
2021-11-18 15:44:45 -06:00
|
|
|
|
|
|
|
pub fn provide(providers: &mut ty::query::Providers) {
|
|
|
|
*providers = ty::query::Providers {
|
|
|
|
associated_item,
|
|
|
|
associated_item_def_ids,
|
|
|
|
associated_items,
|
2023-03-16 22:45:14 -05:00
|
|
|
associated_types_for_impl_traits_in_associated_fn,
|
|
|
|
associated_type_for_impl_trait_in_trait,
|
2021-11-18 16:29:07 -06:00
|
|
|
impl_item_implementor_ids,
|
2021-11-18 15:44:45 -06:00
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] {
|
|
|
|
let item = tcx.hir().expect_item(def_id.expect_local());
|
|
|
|
match item.kind {
|
2023-02-28 10:05:37 -06:00
|
|
|
hir::ItemKind::Trait(.., ref trait_item_refs) => {
|
2023-03-03 15:27:25 -06:00
|
|
|
if tcx.lower_impl_trait_in_trait_to_assoc_ty() {
|
2023-02-28 10:05:37 -06:00
|
|
|
// We collect RPITITs for each trait method's return type and create a
|
2023-03-16 22:45:14 -05:00
|
|
|
// corresponding associated item using associated_types_for_impl_traits_in_associated_fn
|
2023-02-28 10:05:37 -06:00
|
|
|
// query.
|
|
|
|
tcx.arena.alloc_from_iter(
|
|
|
|
trait_item_refs
|
|
|
|
.iter()
|
|
|
|
.map(|trait_item_ref| trait_item_ref.id.owner_id.to_def_id())
|
|
|
|
.chain(
|
|
|
|
trait_item_refs
|
|
|
|
.iter()
|
|
|
|
.filter(|trait_item_ref| {
|
|
|
|
matches!(trait_item_ref.kind, hir::AssocItemKind::Fn { .. })
|
|
|
|
})
|
|
|
|
.flat_map(|trait_item_ref| {
|
|
|
|
let trait_fn_def_id =
|
|
|
|
trait_item_ref.id.owner_id.def_id.to_def_id();
|
2023-03-16 22:45:14 -05:00
|
|
|
tcx.associated_types_for_impl_traits_in_associated_fn(
|
|
|
|
trait_fn_def_id,
|
|
|
|
)
|
2023-02-28 10:05:37 -06:00
|
|
|
})
|
|
|
|
.map(|def_id| *def_id),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
tcx.arena.alloc_from_iter(
|
|
|
|
trait_item_refs
|
|
|
|
.iter()
|
|
|
|
.map(|trait_item_ref| trait_item_ref.id.owner_id.to_def_id()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-03-02 13:20:48 -06:00
|
|
|
hir::ItemKind::Impl(ref impl_) => {
|
2023-03-03 15:27:25 -06:00
|
|
|
if tcx.lower_impl_trait_in_trait_to_assoc_ty() {
|
2023-03-02 13:20:48 -06:00
|
|
|
// We collect RPITITs for each trait method's return type, on the impl side too and
|
|
|
|
// create a corresponding associated item using
|
2023-03-16 22:45:14 -05:00
|
|
|
// associated_types_for_impl_traits_in_associated_fn query.
|
2023-03-02 13:20:48 -06:00
|
|
|
tcx.arena.alloc_from_iter(
|
|
|
|
impl_
|
|
|
|
.items
|
|
|
|
.iter()
|
|
|
|
.map(|impl_item_ref| impl_item_ref.id.owner_id.to_def_id())
|
|
|
|
.chain(impl_.of_trait.iter().flat_map(|_| {
|
|
|
|
impl_
|
|
|
|
.items
|
|
|
|
.iter()
|
|
|
|
.filter(|impl_item_ref| {
|
|
|
|
matches!(impl_item_ref.kind, hir::AssocItemKind::Fn { .. })
|
|
|
|
})
|
|
|
|
.flat_map(|impl_item_ref| {
|
|
|
|
let impl_fn_def_id =
|
|
|
|
impl_item_ref.id.owner_id.def_id.to_def_id();
|
2023-03-16 22:45:14 -05:00
|
|
|
tcx.associated_types_for_impl_traits_in_associated_fn(
|
|
|
|
impl_fn_def_id,
|
|
|
|
)
|
2023-03-02 13:20:48 -06:00
|
|
|
})
|
|
|
|
.map(|def_id| *def_id)
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
tcx.arena.alloc_from_iter(
|
|
|
|
impl_.items.iter().map(|impl_item_ref| impl_item_ref.id.owner_id.to_def_id()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-11-18 15:44:45 -06:00
|
|
|
_ => span_bug!(item.span, "associated_item_def_ids: not impl or trait"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 02:57:34 -06:00
|
|
|
fn associated_items(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItems {
|
2023-02-05 06:28:12 -06:00
|
|
|
if tcx.is_trait_alias(def_id) {
|
|
|
|
ty::AssocItems::new(Vec::new())
|
|
|
|
} else {
|
|
|
|
let items = tcx.associated_item_def_ids(def_id).iter().map(|did| tcx.associated_item(*did));
|
|
|
|
ty::AssocItems::new(items)
|
|
|
|
}
|
2021-11-18 15:44:45 -06:00
|
|
|
}
|
|
|
|
|
2023-02-21 04:35:19 -06:00
|
|
|
fn impl_item_implementor_ids(tcx: TyCtxt<'_>, impl_id: DefId) -> DefIdMap<DefId> {
|
2021-11-18 16:29:07 -06:00
|
|
|
tcx.associated_items(impl_id)
|
|
|
|
.in_definition_order()
|
|
|
|
.filter_map(|item| item.trait_item_def_id.map(|trait_item| (trait_item, item.def_id)))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2021-11-18 15:44:45 -06:00
|
|
|
fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem {
|
|
|
|
let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
|
2021-10-21 12:41:47 -05:00
|
|
|
let parent_def_id = tcx.hir().get_parent_item(id);
|
2022-09-20 00:11:23 -05:00
|
|
|
let parent_item = tcx.hir().expect_item(parent_def_id.def_id);
|
2021-11-18 15:44:45 -06:00
|
|
|
match parent_item.kind {
|
|
|
|
hir::ItemKind::Impl(ref impl_) => {
|
|
|
|
if let Some(impl_item_ref) =
|
2022-10-26 22:02:18 -05:00
|
|
|
impl_.items.iter().find(|i| i.id.owner_id.to_def_id() == def_id)
|
2021-11-18 15:44:45 -06:00
|
|
|
{
|
2022-03-12 17:52:25 -06:00
|
|
|
let assoc_item = associated_item_from_impl_item_ref(impl_item_ref);
|
2021-11-18 15:44:45 -06:00
|
|
|
debug_assert_eq!(assoc_item.def_id, def_id);
|
|
|
|
return assoc_item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hir::ItemKind::Trait(.., ref trait_item_refs) => {
|
|
|
|
if let Some(trait_item_ref) =
|
2022-10-26 22:02:18 -05:00
|
|
|
trait_item_refs.iter().find(|i| i.id.owner_id.to_def_id() == def_id)
|
2021-11-18 15:44:45 -06:00
|
|
|
{
|
2022-03-12 17:52:25 -06:00
|
|
|
let assoc_item = associated_item_from_trait_item_ref(trait_item_ref);
|
2021-11-18 15:44:45 -06:00
|
|
|
debug_assert_eq!(assoc_item.def_id, def_id);
|
|
|
|
return assoc_item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
span_bug!(
|
|
|
|
parent_item.span,
|
|
|
|
"unexpected parent of trait or impl item or item not found: {:?}",
|
|
|
|
parent_item.kind
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-12 17:52:25 -06:00
|
|
|
fn associated_item_from_trait_item_ref(trait_item_ref: &hir::TraitItemRef) -> ty::AssocItem {
|
2022-10-26 22:02:18 -05:00
|
|
|
let owner_id = trait_item_ref.id.owner_id;
|
2021-11-18 15:44:45 -06:00
|
|
|
let (kind, has_self) = match trait_item_ref.kind {
|
|
|
|
hir::AssocItemKind::Const => (ty::AssocKind::Const, false),
|
|
|
|
hir::AssocItemKind::Fn { has_self } => (ty::AssocKind::Fn, has_self),
|
|
|
|
hir::AssocItemKind::Type => (ty::AssocKind::Type, false),
|
|
|
|
};
|
|
|
|
|
|
|
|
ty::AssocItem {
|
2022-01-12 20:15:51 -06:00
|
|
|
name: trait_item_ref.ident.name,
|
2021-11-18 15:44:45 -06:00
|
|
|
kind,
|
2022-10-26 22:02:18 -05:00
|
|
|
def_id: owner_id.to_def_id(),
|
|
|
|
trait_item_def_id: Some(owner_id.to_def_id()),
|
2022-03-12 17:52:25 -06:00
|
|
|
container: ty::TraitContainer,
|
2021-11-18 15:44:45 -06:00
|
|
|
fn_has_self_parameter: has_self,
|
2023-03-12 15:32:50 -05:00
|
|
|
opt_rpitit_info: None,
|
2021-11-18 15:44:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-12 17:52:25 -06:00
|
|
|
fn associated_item_from_impl_item_ref(impl_item_ref: &hir::ImplItemRef) -> ty::AssocItem {
|
2022-10-26 22:02:18 -05:00
|
|
|
let def_id = impl_item_ref.id.owner_id;
|
2021-11-18 15:44:45 -06:00
|
|
|
let (kind, has_self) = match impl_item_ref.kind {
|
|
|
|
hir::AssocItemKind::Const => (ty::AssocKind::Const, false),
|
|
|
|
hir::AssocItemKind::Fn { has_self } => (ty::AssocKind::Fn, has_self),
|
|
|
|
hir::AssocItemKind::Type => (ty::AssocKind::Type, false),
|
|
|
|
};
|
|
|
|
|
|
|
|
ty::AssocItem {
|
2022-01-12 20:15:51 -06:00
|
|
|
name: impl_item_ref.ident.name,
|
2021-11-18 15:44:45 -06:00
|
|
|
kind,
|
|
|
|
def_id: def_id.to_def_id(),
|
2022-01-08 05:22:06 -06:00
|
|
|
trait_item_def_id: impl_item_ref.trait_item_def_id,
|
2022-03-12 17:52:25 -06:00
|
|
|
container: ty::ImplContainer,
|
2021-11-18 15:44:45 -06:00
|
|
|
fn_has_self_parameter: has_self,
|
2023-03-12 15:32:50 -05:00
|
|
|
opt_rpitit_info: None,
|
2021-11-18 15:44:45 -06:00
|
|
|
}
|
|
|
|
}
|
2022-10-26 14:38:32 -05:00
|
|
|
|
2023-03-16 22:45:14 -05:00
|
|
|
/// Given an `fn_def_id` of a trait or a trait implementation:
|
|
|
|
///
|
|
|
|
/// if `fn_def_id` is a function defined inside a trait, then it synthesizes
|
|
|
|
/// a new def id corresponding to a new associated type for each return-
|
|
|
|
/// position `impl Trait` in the signature.
|
|
|
|
///
|
|
|
|
/// if `fn_def_id` is a function inside of an impl, then for each synthetic
|
|
|
|
/// associated type generated for the corresponding trait function described
|
|
|
|
/// above, synthesize a corresponding associated type in the impl.
|
|
|
|
fn associated_types_for_impl_traits_in_associated_fn(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
fn_def_id: DefId,
|
|
|
|
) -> &'_ [DefId] {
|
2023-02-16 15:12:53 -06:00
|
|
|
let parent_def_id = tcx.parent(fn_def_id);
|
2022-10-26 14:38:32 -05:00
|
|
|
|
2023-02-16 15:12:53 -06:00
|
|
|
match tcx.def_kind(parent_def_id) {
|
|
|
|
DefKind::Trait => {
|
|
|
|
struct RPITVisitor {
|
|
|
|
rpits: Vec<LocalDefId>,
|
2022-10-26 14:38:32 -05:00
|
|
|
}
|
|
|
|
|
2023-02-16 15:12:53 -06:00
|
|
|
impl<'v> Visitor<'v> for RPITVisitor {
|
|
|
|
fn visit_ty(&mut self, ty: &'v hir::Ty<'v>) {
|
|
|
|
if let hir::TyKind::OpaqueDef(item_id, _, _) = ty.kind {
|
|
|
|
self.rpits.push(item_id.owner_id.def_id)
|
|
|
|
}
|
|
|
|
intravisit::walk_ty(self, ty)
|
|
|
|
}
|
|
|
|
}
|
2022-10-26 14:38:32 -05:00
|
|
|
|
2023-02-16 15:12:53 -06:00
|
|
|
let mut visitor = RPITVisitor { rpits: Vec::new() };
|
2022-10-26 14:38:32 -05:00
|
|
|
|
2023-02-16 15:12:53 -06:00
|
|
|
if let Some(output) = tcx.hir().get_fn_output(fn_def_id.expect_local()) {
|
|
|
|
visitor.visit_fn_ret_ty(output);
|
|
|
|
|
|
|
|
tcx.arena.alloc_from_iter(visitor.rpits.iter().map(|opaque_ty_def_id| {
|
2023-03-16 22:45:14 -05:00
|
|
|
tcx.associated_type_for_impl_trait_in_trait(opaque_ty_def_id).to_def_id()
|
2023-02-16 15:12:53 -06:00
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
&[]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DefKind::Impl { .. } => {
|
|
|
|
let Some(trait_fn_def_id) = tcx.associated_item(fn_def_id).trait_item_def_id else { return &[] };
|
|
|
|
|
|
|
|
tcx.arena.alloc_from_iter(
|
2023-03-16 22:45:14 -05:00
|
|
|
tcx.associated_types_for_impl_traits_in_associated_fn(trait_fn_def_id).iter().map(
|
2023-02-16 15:12:53 -06:00
|
|
|
move |trait_assoc_def_id| {
|
2023-03-16 22:45:14 -05:00
|
|
|
associated_type_for_impl_trait_in_impl(
|
2023-02-16 15:12:53 -06:00
|
|
|
tcx,
|
|
|
|
trait_assoc_def_id.expect_local(),
|
|
|
|
fn_def_id.expect_local(),
|
|
|
|
)
|
|
|
|
.to_def_id()
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
def_kind => bug!(
|
2023-03-16 22:45:14 -05:00
|
|
|
"associated_types_for_impl_traits_in_associated_fn: {:?} should be Trait or Impl but is {:?}",
|
2023-02-16 15:12:53 -06:00
|
|
|
parent_def_id,
|
|
|
|
def_kind
|
|
|
|
),
|
2022-10-26 14:38:32 -05:00
|
|
|
}
|
|
|
|
}
|
2022-12-16 14:15:34 -06:00
|
|
|
|
2023-03-16 22:45:14 -05:00
|
|
|
/// Given an `opaque_ty_def_id` corresponding to an `impl Trait` in an associated
|
|
|
|
/// function from a trait, synthesize an associated type for that `impl Trait`
|
|
|
|
/// that inherits properties that we infer from the method and the opaque type.
|
|
|
|
fn associated_type_for_impl_trait_in_trait(
|
2022-12-16 14:15:34 -06:00
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
opaque_ty_def_id: LocalDefId,
|
|
|
|
) -> LocalDefId {
|
2023-03-10 14:48:11 -06:00
|
|
|
let fn_def_id = tcx.impl_trait_in_trait_parent_fn(opaque_ty_def_id.to_def_id());
|
2022-12-16 14:15:34 -06:00
|
|
|
let trait_def_id = tcx.parent(fn_def_id);
|
|
|
|
assert_eq!(tcx.def_kind(trait_def_id), DefKind::Trait);
|
|
|
|
|
|
|
|
let span = tcx.def_span(opaque_ty_def_id);
|
|
|
|
let trait_assoc_ty =
|
|
|
|
tcx.at(span).create_def(trait_def_id.expect_local(), DefPathData::ImplTraitAssocTy);
|
2023-02-28 10:05:37 -06:00
|
|
|
|
|
|
|
let local_def_id = trait_assoc_ty.def_id();
|
|
|
|
let def_id = local_def_id.to_def_id();
|
|
|
|
|
|
|
|
trait_assoc_ty.opt_def_kind(Some(DefKind::AssocTy));
|
|
|
|
|
|
|
|
// There's no HIR associated with this new synthesized `def_id`, so feed
|
|
|
|
// `opt_local_def_id_to_hir_id` with `None`.
|
|
|
|
trait_assoc_ty.opt_local_def_id_to_hir_id(None);
|
|
|
|
|
|
|
|
// Copy span of the opaque.
|
|
|
|
trait_assoc_ty.def_ident_span(Some(span));
|
|
|
|
|
|
|
|
trait_assoc_ty.associated_item(ty::AssocItem {
|
|
|
|
name: kw::Empty,
|
|
|
|
kind: ty::AssocKind::Type,
|
|
|
|
def_id,
|
|
|
|
trait_item_def_id: None,
|
|
|
|
container: ty::TraitContainer,
|
|
|
|
fn_has_self_parameter: false,
|
2023-03-12 15:32:50 -05:00
|
|
|
opt_rpitit_info: Some(ImplTraitInTraitData::Trait {
|
|
|
|
fn_def_id,
|
|
|
|
opaque_def_id: opaque_ty_def_id.to_def_id(),
|
|
|
|
}),
|
2023-02-28 10:05:37 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
// Copy visility of the containing function.
|
|
|
|
trait_assoc_ty.visibility(tcx.visibility(fn_def_id));
|
|
|
|
|
|
|
|
// Copy impl_defaultness of the containing function.
|
|
|
|
trait_assoc_ty.impl_defaultness(tcx.impl_defaultness(fn_def_id));
|
|
|
|
|
|
|
|
// Copy type_of of the opaque.
|
|
|
|
trait_assoc_ty.type_of(ty::EarlyBinder(tcx.mk_opaque(
|
|
|
|
opaque_ty_def_id.to_def_id(),
|
|
|
|
InternalSubsts::identity_for_item(tcx, opaque_ty_def_id.to_def_id()),
|
|
|
|
)));
|
|
|
|
|
2023-03-13 17:02:28 -05:00
|
|
|
trait_assoc_ty.is_type_alias_impl_trait(false);
|
|
|
|
|
2023-03-14 12:29:06 -05:00
|
|
|
// Copy generics_of of the opaque type item but the trait is the parent.
|
|
|
|
trait_assoc_ty.generics_of({
|
|
|
|
let opaque_ty_generics = tcx.generics_of(opaque_ty_def_id);
|
|
|
|
let opaque_ty_parent_count = opaque_ty_generics.parent_count;
|
|
|
|
let mut params = opaque_ty_generics.params.clone();
|
|
|
|
|
|
|
|
let parent_generics = tcx.generics_of(trait_def_id);
|
|
|
|
let parent_count = parent_generics.parent_count + parent_generics.params.len();
|
|
|
|
|
|
|
|
let mut trait_fn_params = tcx.generics_of(fn_def_id).params.clone();
|
|
|
|
|
|
|
|
for param in &mut params {
|
|
|
|
param.index = param.index + parent_count as u32 + trait_fn_params.len() as u32
|
|
|
|
- opaque_ty_parent_count as u32;
|
|
|
|
}
|
|
|
|
|
|
|
|
trait_fn_params.extend(params);
|
|
|
|
params = trait_fn_params;
|
|
|
|
|
|
|
|
let param_def_id_to_index =
|
|
|
|
params.iter().map(|param| (param.def_id, param.index)).collect();
|
|
|
|
|
|
|
|
ty::Generics {
|
|
|
|
parent: Some(trait_def_id),
|
|
|
|
parent_count,
|
|
|
|
params,
|
|
|
|
param_def_id_to_index,
|
|
|
|
has_self: false,
|
|
|
|
has_late_bound_regions: opaque_ty_generics.has_late_bound_regions,
|
|
|
|
}
|
|
|
|
});
|
2023-02-28 10:05:37 -06:00
|
|
|
|
|
|
|
// There are no predicates for the synthesized associated type.
|
|
|
|
trait_assoc_ty.explicit_predicates_of(ty::GenericPredicates {
|
|
|
|
parent: Some(trait_def_id),
|
|
|
|
predicates: &[],
|
|
|
|
});
|
|
|
|
|
|
|
|
// There are no inferred outlives for the synthesized associated type.
|
|
|
|
trait_assoc_ty.inferred_outlives_of(&[]);
|
|
|
|
|
|
|
|
local_def_id
|
2022-12-16 14:15:34 -06:00
|
|
|
}
|
2023-02-16 15:12:53 -06:00
|
|
|
|
2023-03-16 22:45:14 -05:00
|
|
|
/// Given an `trait_assoc_def_id` corresponding to an associated item synthesized
|
|
|
|
/// from an `impl Trait` in an associated function from a trait, and an
|
|
|
|
/// `impl_fn_def_id` that represents an implementation of the associated function
|
|
|
|
/// that the `impl Trait` comes from, synthesize an associated type for that `impl Trait`
|
|
|
|
/// that inherits properties that we infer from the method and the associated type.
|
|
|
|
fn associated_type_for_impl_trait_in_impl(
|
2023-02-16 15:12:53 -06:00
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
trait_assoc_def_id: LocalDefId,
|
|
|
|
impl_fn_def_id: LocalDefId,
|
|
|
|
) -> LocalDefId {
|
2023-03-03 09:40:32 -06:00
|
|
|
let impl_local_def_id = tcx.local_parent(impl_fn_def_id);
|
|
|
|
let impl_def_id = impl_local_def_id.to_def_id();
|
2023-02-16 15:12:53 -06:00
|
|
|
|
2023-03-02 13:20:48 -06:00
|
|
|
// FIXME fix the span, we probably want the def_id of the return type of the function
|
|
|
|
let span = tcx.def_span(impl_fn_def_id);
|
2023-03-03 09:40:32 -06:00
|
|
|
let impl_assoc_ty = tcx.at(span).create_def(impl_local_def_id, DefPathData::ImplTraitAssocTy);
|
2023-02-16 15:12:53 -06:00
|
|
|
|
2023-03-02 13:20:48 -06:00
|
|
|
let local_def_id = impl_assoc_ty.def_id();
|
|
|
|
let def_id = local_def_id.to_def_id();
|
|
|
|
|
|
|
|
impl_assoc_ty.opt_def_kind(Some(DefKind::AssocTy));
|
|
|
|
|
|
|
|
// There's no HIR associated with this new synthesized `def_id`, so feed
|
|
|
|
// `opt_local_def_id_to_hir_id` with `None`.
|
|
|
|
impl_assoc_ty.opt_local_def_id_to_hir_id(None);
|
|
|
|
|
2023-03-08 14:59:06 -06:00
|
|
|
// Copy span of the opaque.
|
|
|
|
impl_assoc_ty.def_ident_span(Some(span));
|
|
|
|
|
2023-03-02 13:20:48 -06:00
|
|
|
impl_assoc_ty.associated_item(ty::AssocItem {
|
|
|
|
name: kw::Empty,
|
|
|
|
kind: ty::AssocKind::Type,
|
|
|
|
def_id,
|
|
|
|
trait_item_def_id: Some(trait_assoc_def_id.to_def_id()),
|
|
|
|
container: ty::ImplContainer,
|
|
|
|
fn_has_self_parameter: false,
|
2023-03-12 15:32:50 -05:00
|
|
|
opt_rpitit_info: Some(ImplTraitInTraitData::Impl { fn_def_id: impl_fn_def_id.to_def_id() }),
|
2023-03-02 13:20:48 -06:00
|
|
|
});
|
|
|
|
|
2023-03-03 09:38:36 -06:00
|
|
|
// Copy param_env of the containing function. The synthesized associated type doesn't have
|
|
|
|
// extra predicates to assume.
|
|
|
|
impl_assoc_ty.param_env(tcx.param_env(impl_fn_def_id));
|
|
|
|
|
2023-03-08 14:59:06 -06:00
|
|
|
// Copy visility of the containing function.
|
|
|
|
impl_assoc_ty.visibility(tcx.visibility(impl_fn_def_id));
|
|
|
|
|
2023-03-02 13:20:48 -06:00
|
|
|
// Copy impl_defaultness of the containing function.
|
|
|
|
impl_assoc_ty.impl_defaultness(tcx.impl_defaultness(impl_fn_def_id));
|
|
|
|
|
2023-03-03 09:40:32 -06:00
|
|
|
// Copy generics_of the trait's associated item but the impl as the parent.
|
2023-03-17 12:05:26 -05:00
|
|
|
// FIXME(-Zlower-impl-trait-in-trait-to-assoc-ty) resolves to the trait instead of the impl
|
|
|
|
// generics.
|
2023-03-03 09:40:32 -06:00
|
|
|
impl_assoc_ty.generics_of({
|
|
|
|
let trait_assoc_generics = tcx.generics_of(trait_assoc_def_id);
|
|
|
|
let trait_assoc_parent_count = trait_assoc_generics.parent_count;
|
|
|
|
let mut params = trait_assoc_generics.params.clone();
|
|
|
|
|
|
|
|
let parent_generics = tcx.generics_of(impl_def_id);
|
|
|
|
let parent_count = parent_generics.parent_count + parent_generics.params.len();
|
|
|
|
|
|
|
|
for param in &mut params {
|
2023-03-17 12:05:26 -05:00
|
|
|
param.index = param.index + parent_count as u32 - trait_assoc_parent_count as u32;
|
2023-03-03 09:40:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let param_def_id_to_index =
|
|
|
|
params.iter().map(|param| (param.def_id, param.index)).collect();
|
|
|
|
|
|
|
|
ty::Generics {
|
|
|
|
parent: Some(impl_def_id),
|
|
|
|
parent_count,
|
|
|
|
params,
|
|
|
|
param_def_id_to_index,
|
|
|
|
has_self: false,
|
|
|
|
has_late_bound_regions: trait_assoc_generics.has_late_bound_regions,
|
|
|
|
}
|
|
|
|
});
|
2023-03-02 13:20:48 -06:00
|
|
|
|
2023-03-03 09:41:10 -06:00
|
|
|
// There are no predicates for the synthesized associated type.
|
|
|
|
impl_assoc_ty.explicit_predicates_of(ty::GenericPredicates {
|
|
|
|
parent: Some(impl_def_id),
|
|
|
|
predicates: &[],
|
|
|
|
});
|
|
|
|
|
2023-03-03 09:41:22 -06:00
|
|
|
// There are no inferred outlives for the synthesized associated type.
|
|
|
|
impl_assoc_ty.inferred_outlives_of(&[]);
|
2023-03-02 13:20:48 -06:00
|
|
|
|
|
|
|
local_def_id
|
2023-02-16 15:12:53 -06:00
|
|
|
}
|