Skip to content

Commit

Permalink
Auto merge of rust-lang#125907 - fmease:rustdoc-synth-blanket-ocx-nex…
Browse files Browse the repository at this point in the history
…t, r=<try>

rustdoc: use the next solver for blanket impl synthesis

Presumably due to better caching behavior, switching from the old to the next solver *drastically* improves the compile time on certain inputs. See rust-lang#114891.
Fixes rust-lang#114891.

Furthermore use an `ObligationCtxt` instead of operating on an `InferCtxt` directly and don't drop the obligations generated by the type relating.

For context, originally I just wanted to submit the infcx→ocx change. However, that regressed `tests/rustdoc-ui/ice-blanket-impl-52873.rs` (pass→overflow error) because with `ocx.select_where_possible` we would no longer suppress / defatalize (canonical) overflow errors. CC rust-lang#54199 which introduced that special case. Obviously in the next solver overflows are non-fatal incl. `ice-blanket-impl-52873.rs`. Hence the switch now.

https://github.com/rust-lang/rust/labels/S-blocked on perf improvements for the next solver.
  • Loading branch information
bors committed Oct 16, 2024
2 parents d829780 + c32dd09 commit 9daeecb
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::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

0 comments on commit 9daeecb

Please sign in to comment.