From 3637b153f7cb8de14588ea82a15a28499bcb3b3f Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Tue, 25 Jun 2024 07:51:44 +0000 Subject: [PATCH] move desugaring to item bounds --- .../src/collect/item_bounds.rs | 26 ++++++++++++++++ .../src/collect/predicates_of.rs | 31 ------------------- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect/item_bounds.rs b/compiler/rustc_hir_analysis/src/collect/item_bounds.rs index 57142414b9d..707a3f2d3c7 100644 --- a/compiler/rustc_hir_analysis/src/collect/item_bounds.rs +++ b/compiler/rustc_hir_analysis/src/collect/item_bounds.rs @@ -8,6 +8,7 @@ use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFold use rustc_middle::{bug, span_bug}; use rustc_span::def_id::{DefId, LocalDefId}; use rustc_span::Span; +use rustc_type_ir::Upcast; /// For associated types we include both bounds written on the type /// (`type X: Trait`) and predicates from the trait: `where Self::X: Trait`. @@ -124,6 +125,31 @@ pub(super) fn explicit_item_bounds_with_filter( None => {} } + if tcx.is_effects_desugared_assoc_ty(def_id.to_def_id()) { + let mut predicates = Vec::new(); + + let parent = tcx.local_parent(def_id); + + let identity_args = ty::GenericArgs::identity_for_item(tcx, def_id); + let preds = tcx.explicit_predicates_of(parent); + + if let ty::AssocItemContainer::TraitContainer = tcx.associated_item(def_id).container { + // for traits, emit `type Effects: TyCompat<<(T1::Effects, ..) as Min>::Output>` + // TODO do the same for impls + let tup = Ty::new(tcx, ty::Tuple(preds.effects_min_tys)); + // TODO span + let span = tcx.def_span(def_id); + let assoc = tcx.require_lang_item(hir::LangItem::EffectsMinOutput, Some(span)); + let proj = Ty::new_projection(tcx, assoc, [tup]); + // TODO this is bad + let self_proj = Ty::new_projection(tcx, def_id.to_def_id(), identity_args); + let trait_ = tcx.require_lang_item(hir::LangItem::EffectsTyCompat, Some(span)); + let trait_ref = ty::TraitRef::new(tcx, trait_, [self_proj, proj]); + predicates.push((ty::Binder::dummy(trait_ref).upcast(tcx), span)); + } + return ty::EarlyBinder::bind(tcx.arena.alloc_from_iter(predicates)); + } + let bounds = match tcx.hir_node_by_def_id(def_id) { hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(bounds, _), diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index e3d1e1c423e..9a5feaf3d3c 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -57,7 +57,6 @@ pub(super) fn predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredic #[instrument(level = "trace", skip(tcx), ret)] fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::GenericPredicates<'_> { use rustc_hir::*; - use rustc_middle::ty::Ty; // to override hir::Ty match tcx.opt_rpitit_info(def_id.to_def_id()) { Some(ImplTraitInTraitData::Trait { fn_def_id, .. }) => { @@ -114,36 +113,6 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen None => {} } - if tcx.is_effects_desugared_assoc_ty(def_id.to_def_id()) { - let mut predicates = Vec::new(); - - // Inherit predicates of parent (impl or trait) - let parent = tcx.local_parent(def_id); - - let identity_args = ty::GenericArgs::identity_for_item(tcx, def_id); - let preds = tcx.explicit_predicates_of(parent); - - if let ty::AssocItemContainer::TraitContainer = tcx.associated_item(def_id).container { - // for traits, emit `type Effects: TyCompat<<(T1::Effects, ..) as Min>::Output>` - // TODO do the same for impls - let tup = Ty::new(tcx, ty::Tuple(preds.effects_min_tys)); - // TODO span - let span = tcx.def_span(def_id); - let assoc = tcx.require_lang_item(LangItem::EffectsMinOutput, Some(span)); - let proj = Ty::new_projection(tcx, assoc, [tup]); - // TODO this is bad - let self_proj = Ty::new_projection(tcx, def_id.to_def_id(), identity_args); - let trait_ = tcx.require_lang_item(LangItem::EffectsTyCompat, Some(span)); - let trait_ref = ty::TraitRef::new(tcx, trait_, [self_proj, proj]); - predicates.push((ty::Binder::dummy(trait_ref).upcast(tcx), span)); - } - return ty::GenericPredicates { - parent: Some(parent.to_def_id()), - predicates: tcx.arena.alloc_from_iter(predicates), - effects_min_tys: ty::List::empty(), - }; - } - let hir_id = tcx.local_def_id_to_hir_id(def_id); let node = tcx.hir_node(hir_id);