Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: use the next solver for blanket impl synthesis #125907

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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::DUMMY_SP;
use rustc_span::def_id::DefId;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
use rustc_trait_selection::traits;
use thin_vec::ThinVec;
use tracing::{debug, instrument, trace};

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
Loading