diff --git a/src/librustdoc/clean/blanket_impl.rs b/src/librustdoc/clean/blanket_impl.rs index 48c3fb65203cc..8e59bbb83c8b1 100644 --- a/src/librustdoc/clean/blanket_impl.rs +++ b/src/librustdoc/clean/blanket_impl.rs @@ -1,10 +1,10 @@ use rustc_hir as hir; -use rustc_infer::infer::{DefineOpaqueTypes, InferOk, TyCtxtInferExt}; -use rustc_infer::traits; +use rustc_infer::infer::TyCtxtInferExt; +use rustc_infer::traits::ObligationCause; use rustc_middle::ty::{self, Upcast}; use rustc_span::def_id::DefId; use rustc_span::DUMMY_SP; -use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt; +use rustc_trait_selection::traits; use thin_vec::ThinVec; use crate::clean; @@ -30,53 +30,47 @@ pub(crate) fn synthesize_blanket_impls( } // NOTE: doesn't use `for_each_relevant_impl` to avoid looking at anything besides blanket impls let trait_impls = tcx.trait_impls_of(trait_def_id); - 'blanket_impls: for &impl_def_id in trait_impls.blanket_impls() { + for &impl_def_id in trait_impls.blanket_impls() { trace!("considering impl `{impl_def_id:?}` for trait `{trait_def_id:?}`"); let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap(); if !matches!(trait_ref.skip_binder().self_ty().kind(), ty::Param(_)) { continue; } - let infcx = tcx.infer_ctxt().build(); + let infcx = tcx.infer_ctxt().with_next_trait_solver(true).build(); + let ocx = traits::ObligationCtxt::new(&infcx); + let args = infcx.fresh_args_for_item(DUMMY_SP, item_def_id); let impl_ty = ty.instantiate(tcx, args); let param_env = ty::ParamEnv::empty(); + let cause = ObligationCause::dummy(); let impl_args = infcx.fresh_args_for_item(DUMMY_SP, impl_def_id); let impl_trait_ref = trait_ref.instantiate(tcx, impl_args); // Require the type the impl is implemented on to match // our type, and ignore the impl if there was a mismatch. - let Ok(eq_result) = infcx.at(&traits::ObligationCause::dummy(), param_env).eq( - DefineOpaqueTypes::Yes, - impl_trait_ref.self_ty(), - impl_ty, - ) else { + if ocx.eq(&cause, param_env, impl_trait_ref.self_ty(), impl_ty).is_err() { continue; - }; - let InferOk { value: (), obligations } = eq_result; - // FIXME(eddyb) ignoring `obligations` might cause false positives. - drop(obligations); + } - let predicates = tcx - .predicates_of(impl_def_id) - .instantiate(tcx, impl_args) - .predicates - .into_iter() - .chain(Some(impl_trait_ref.upcast(tcx))); - for predicate in predicates { - let obligation = traits::Obligation::new( - tcx, - traits::ObligationCause::dummy(), - param_env, - predicate, - ); - match infcx.evaluate_obligation(&obligation) { - Ok(eval_result) if eval_result.may_apply() => {} - Err(traits::OverflowError::Canonical) => {} - _ => continue 'blanket_impls, - } + ocx.register_obligations(traits::predicates_for_generics( + |_, _| cause.clone(), + param_env, + tcx.predicates_of(impl_def_id).instantiate(tcx, impl_args), + )); + + ocx.register_obligation(traits::Obligation { + cause, + recursion_depth: 0, + param_env, + predicate: impl_trait_ref.upcast(tcx), + }); + + if !ocx.select_where_possible().is_empty() { + continue; } + debug!("found applicable impl for trait ref {trait_ref:?}"); cx.generated_synthetics.insert((ty.skip_binder(), trait_def_id));