Skip to content

Commit

Permalink
rustdoc: use the next solver for blanket impl synthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
fmease committed Jun 3, 2024
1 parent 77d4115 commit 737b8ac
Showing 1 changed file with 26 additions and 32 deletions.
58 changes: 26 additions & 32 deletions src/librustdoc/clean/blanket_impl.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -31,53 +31,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));
Expand Down

0 comments on commit 737b8ac

Please sign in to comment.