|
| 1 | +use std::ops::ControlFlow; |
| 2 | + |
| 3 | +use rustc_data_structures::fx::FxIndexMap; |
| 4 | +use rustc_hir as hir; |
| 5 | +use rustc_hir::def_id::DefId; |
| 6 | +use rustc_infer::infer::TyCtxtInferExt; |
| 7 | +use rustc_infer::traits::Obligation; |
| 8 | +use rustc_middle::traits::ObligationCause; |
| 9 | +use rustc_middle::ty::{ |
| 10 | + self, Ty, TyCtxt, TypeFolder, TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitor, |
| 11 | +}; |
| 12 | +use rustc_span::ErrorGuaranteed; |
| 13 | +use rustc_span::{sym, Span}; |
| 14 | +use rustc_trait_selection::traits::ObligationCtxt; |
| 15 | +use rustc_type_ir::fold::TypeFoldable; |
| 16 | + |
| 17 | +/// Check that an implementation does not refine an RPITIT from a trait method signature. |
| 18 | +pub(super) fn compare_impl_trait_in_trait_predicate_entailment<'tcx>( |
| 19 | + tcx: TyCtxt<'tcx>, |
| 20 | + impl_m: ty::AssocItem, |
| 21 | + trait_m: ty::AssocItem, |
| 22 | + impl_trait_ref: ty::TraitRef<'tcx>, |
| 23 | +) -> Result<(), ErrorGuaranteed> { |
| 24 | + if !tcx.impl_method_has_trait_impl_trait_tys(impl_m.def_id) |
| 25 | + || tcx.has_attr(impl_m.def_id, sym::refine) |
| 26 | + { |
| 27 | + return Ok(()); |
| 28 | + } |
| 29 | + |
| 30 | + let hidden_tys = tcx.collect_return_position_impl_trait_in_trait_tys(impl_m.def_id)?; |
| 31 | + |
| 32 | + let impl_def_id = impl_m.container_id(tcx); |
| 33 | + //let trait_def_id = trait_m.container_id(tcx); |
| 34 | + let trait_m_to_impl_m_substs = ty::InternalSubsts::identity_for_item(tcx, impl_m.def_id) |
| 35 | + .rebase_onto(tcx, impl_def_id, impl_trait_ref.substs); |
| 36 | + |
| 37 | + let bound_trait_m_sig = tcx.fn_sig(trait_m.def_id).subst(tcx, trait_m_to_impl_m_substs); |
| 38 | + let trait_m_sig = tcx.liberate_late_bound_regions(impl_m.def_id, bound_trait_m_sig); |
| 39 | + |
| 40 | + let mut visitor = ImplTraitInTraitCollector { tcx, types: FxIndexMap::default() }; |
| 41 | + trait_m_sig.visit_with(&mut visitor); |
| 42 | + |
| 43 | + let mut reverse_mapping = FxIndexMap::default(); |
| 44 | + let mut bounds_to_prove = vec![]; |
| 45 | + for (rpitit_def_id, rpitit_substs) in visitor.types { |
| 46 | + let hidden_ty = hidden_tys |
| 47 | + .get(&rpitit_def_id) |
| 48 | + .expect("expected hidden type for RPITIT") |
| 49 | + .subst_identity(); |
| 50 | + reverse_mapping.insert(hidden_ty, tcx.mk_projection(rpitit_def_id, rpitit_substs)); |
| 51 | + |
| 52 | + let ty::Alias(ty::Opaque, opaque_ty) = *hidden_ty.kind() else { |
| 53 | + return Err(report_mismatched_rpitit_signature( |
| 54 | + tcx, |
| 55 | + trait_m_sig, |
| 56 | + trait_m.def_id, |
| 57 | + impl_m.def_id, |
| 58 | + None, |
| 59 | + )); |
| 60 | + }; |
| 61 | + |
| 62 | + // Check that this is an opaque that comes from our impl fn |
| 63 | + if !tcx.hir().get_if_local(opaque_ty.def_id).map_or(false, |node| { |
| 64 | + matches!( |
| 65 | + node.expect_item().expect_opaque_ty().origin, |
| 66 | + hir::OpaqueTyOrigin::AsyncFn(def_id) | hir::OpaqueTyOrigin::FnReturn(def_id) |
| 67 | + if def_id == impl_m.def_id.expect_local() |
| 68 | + ) |
| 69 | + }) { |
| 70 | + return Err(report_mismatched_rpitit_signature( |
| 71 | + tcx, |
| 72 | + trait_m_sig, |
| 73 | + trait_m.def_id, |
| 74 | + impl_m.def_id, |
| 75 | + None, |
| 76 | + )); |
| 77 | + } |
| 78 | + |
| 79 | + bounds_to_prove.extend( |
| 80 | + tcx.explicit_item_bounds(opaque_ty.def_id) |
| 81 | + .iter_instantiated_copied(tcx, opaque_ty.args), |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + let infcx = tcx.infer_ctxt().build(); |
| 86 | + let ocx = ObligationCtxt::new(&infcx); |
| 87 | + let param_env = |
| 88 | + tcx.param_env(impl_m.def_id).with_hidden_return_position_impl_trait_in_trait_tys(); |
| 89 | + |
| 90 | + ocx.register_obligations( |
| 91 | + bounds_to_prove.fold_with(&mut ReverseMapper { tcx, reverse_mapping }).into_iter().map( |
| 92 | + |(pred, span)| { |
| 93 | + Obligation::new(tcx, ObligationCause::dummy_with_span(span), param_env, pred) |
| 94 | + }, |
| 95 | + ), |
| 96 | + ); |
| 97 | + |
| 98 | + let errors = ocx.select_all_or_error(); |
| 99 | + if !errors.is_empty() { |
| 100 | + let span = errors.first().unwrap().obligation.cause.span; |
| 101 | + return Err(report_mismatched_rpitit_signature( |
| 102 | + tcx, |
| 103 | + trait_m_sig, |
| 104 | + trait_m.def_id, |
| 105 | + impl_m.def_id, |
| 106 | + Some(span), |
| 107 | + )); |
| 108 | + } |
| 109 | + |
| 110 | + Ok(()) |
| 111 | +} |
| 112 | + |
| 113 | +struct ImplTraitInTraitCollector<'tcx> { |
| 114 | + tcx: TyCtxt<'tcx>, |
| 115 | + types: FxIndexMap<DefId, ty::GenericArgsRef<'tcx>>, |
| 116 | +} |
| 117 | + |
| 118 | +impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ImplTraitInTraitCollector<'tcx> { |
| 119 | + type BreakTy = !; |
| 120 | + |
| 121 | + fn visit_ty(&mut self, ty: Ty<'tcx>) -> std::ops::ControlFlow<Self::BreakTy> { |
| 122 | + if let ty::Alias(ty::Projection, proj) = *ty.kind() |
| 123 | + && self.tcx.is_impl_trait_in_trait(proj.def_id) |
| 124 | + { |
| 125 | + if self.types.insert(proj.def_id, proj.args).is_none() { |
| 126 | + for (pred, _) in self |
| 127 | + .tcx |
| 128 | + .explicit_item_bounds(proj.def_id) |
| 129 | + .iter_instantiated_copied(self.tcx, proj.args) |
| 130 | + { |
| 131 | + pred.visit_with(self)?; |
| 132 | + } |
| 133 | + } |
| 134 | + ControlFlow::Continue(()) |
| 135 | + } else { |
| 136 | + ty.super_visit_with(self) |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +struct ReverseMapper<'tcx> { |
| 142 | + tcx: TyCtxt<'tcx>, |
| 143 | + reverse_mapping: FxIndexMap<Ty<'tcx>, Ty<'tcx>>, |
| 144 | +} |
| 145 | + |
| 146 | +impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReverseMapper<'tcx> { |
| 147 | + fn interner(&self) -> TyCtxt<'tcx> { |
| 148 | + self.tcx |
| 149 | + } |
| 150 | + |
| 151 | + fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { |
| 152 | + if let Some(ty) = self.reverse_mapping.get(&ty) { *ty } else { ty.super_fold_with(self) } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +fn report_mismatched_rpitit_signature<'tcx>( |
| 157 | + tcx: TyCtxt<'tcx>, |
| 158 | + trait_m_sig: ty::FnSig<'tcx>, |
| 159 | + trait_m_def_id: DefId, |
| 160 | + impl_m_def_id: DefId, |
| 161 | + unmatched_bound: Option<Span>, |
| 162 | +) -> ErrorGuaranteed { |
| 163 | + let mapping = std::iter::zip( |
| 164 | + tcx.fn_sig(trait_m_def_id).skip_binder().bound_vars(), |
| 165 | + tcx.fn_sig(impl_m_def_id).skip_binder().bound_vars(), |
| 166 | + ) |
| 167 | + .filter_map(|(impl_bv, trait_bv)| { |
| 168 | + if let ty::BoundVariableKind::Region(impl_bv) = impl_bv |
| 169 | + && let ty::BoundVariableKind::Region(trait_bv) = trait_bv |
| 170 | + { |
| 171 | + Some((impl_bv, trait_bv)) |
| 172 | + } else { |
| 173 | + None |
| 174 | + } |
| 175 | + }) |
| 176 | + .collect(); |
| 177 | + |
| 178 | + let return_ty = |
| 179 | + trait_m_sig.output().fold_with(&mut super::RemapLateBound { tcx, mapping: &mapping }); |
| 180 | + |
| 181 | + let (span, impl_return_span, sugg) = |
| 182 | + match tcx.hir().get_by_def_id(impl_m_def_id.expect_local()).fn_decl().unwrap().output { |
| 183 | + hir::FnRetTy::DefaultReturn(span) => { |
| 184 | + (tcx.def_span(impl_m_def_id), span, format!("-> {return_ty} ")) |
| 185 | + } |
| 186 | + hir::FnRetTy::Return(ty) => (ty.span, ty.span, format!("{return_ty}")), |
| 187 | + }; |
| 188 | + let trait_return_span = |
| 189 | + tcx.hir().get_if_local(trait_m_def_id).map(|node| match node.fn_decl().unwrap().output { |
| 190 | + hir::FnRetTy::DefaultReturn(_) => tcx.def_span(trait_m_def_id), |
| 191 | + hir::FnRetTy::Return(ty) => ty.span, |
| 192 | + }); |
| 193 | + |
| 194 | + tcx.sess.emit_err(crate::errors::ReturnPositionImplTraitInTraitRefined { |
| 195 | + span, |
| 196 | + impl_return_span, |
| 197 | + trait_return_span, |
| 198 | + sugg, |
| 199 | + unmatched_bound, |
| 200 | + }) |
| 201 | +} |
0 commit comments