about summary refs log tree commit diff
path: root/compiler/rustc_hir_analysis/src/collect
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-07-04 04:24:24 +0000
committerbors <bors@rust-lang.org>2023-07-04 04:24:24 +0000
commit0130c3a06e50ebb166655f81997ce28b9e4029b0 (patch)
treea1cc4cb154f306a758f7509331b52130465be1bd /compiler/rustc_hir_analysis/src/collect
parente728b5b98d67c8493b37e56c2318ca3e7356d05a (diff)
parentb0ab37eb089ed79b38e3b54629831baf64e5a515 (diff)
downloadrust-0130c3a06e50ebb166655f81997ce28b9e4029b0.tar.gz
rust-0130c3a06e50ebb166655f81997ce28b9e4029b0.zip
Auto merge of #113215 - compiler-errors:rpitit-predicates-tweaks, r=spastorino
Make RPITITs assume/require their parent method's predicates

Removes a FIXME from the `param_env` query where we were manually adding the parent function's predicates to the RPITIT's assumptions.

r? `@spastorino`
Diffstat (limited to 'compiler/rustc_hir_analysis/src/collect')
-rw-r--r--compiler/rustc_hir_analysis/src/collect/predicates_of.rs19
1 files changed, 16 insertions, 3 deletions
diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs
index b9e71aaa004..1d00dc2a11d 100644
--- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs
+++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs
@@ -63,7 +63,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
     use rustc_hir::*;
 
     match tcx.opt_rpitit_info(def_id.to_def_id()) {
-        Some(ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
+        Some(ImplTraitInTraitData::Trait { opaque_def_id, fn_def_id }) => {
             let opaque_ty_id = tcx.hir().local_def_id_to_hir_id(opaque_def_id.expect_local());
             let opaque_ty_node = tcx.hir().get(opaque_ty_id);
             let Node::Item(&Item { kind: ItemKind::OpaqueTy(OpaqueTy { lifetime_mapping, .. }), .. }) = opaque_ty_node else {
@@ -71,6 +71,18 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
             };
 
             let mut predicates = Vec::new();
+
+            // RPITITs should inherit the predicates of their parent. This is
+            // both to ensure that the RPITITs are only instantiated when the
+            // parent predicates would hold, and also so that the param-env
+            // inherits these predicates as assumptions.
+            let identity_substs = InternalSubsts::identity_for_item(tcx, def_id);
+            predicates.extend(
+                tcx.explicit_predicates_of(fn_def_id).instantiate_own(tcx, identity_substs),
+            );
+
+            // We also install bidirectional outlives predicates for the RPITIT
+            // to keep the duplicates lifetimes from opaque lowering in sync.
             compute_bidirectional_outlives_predicates(
                 tcx,
                 def_id,
@@ -89,12 +101,13 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
 
         Some(ImplTraitInTraitData::Impl { fn_def_id }) => {
             let assoc_item = tcx.associated_item(def_id);
-            let trait_assoc_predicates = tcx.predicates_of(assoc_item.trait_item_def_id.unwrap());
+            let trait_assoc_predicates =
+                tcx.explicit_predicates_of(assoc_item.trait_item_def_id.unwrap());
 
             let impl_assoc_identity_substs = InternalSubsts::identity_for_item(tcx, def_id);
             let impl_def_id = tcx.parent(fn_def_id);
             let impl_trait_ref_substs =
-                tcx.impl_trait_ref(impl_def_id).unwrap().skip_binder().substs;
+                tcx.impl_trait_ref(impl_def_id).unwrap().subst_identity().substs;
 
             let impl_assoc_substs =
                 impl_assoc_identity_substs.rebase_onto(tcx, impl_def_id, impl_trait_ref_substs);