Skip to content

Commit ba21643

Browse files
authored
Rollup merge of #109367 - nnethercote:opt-fast-rejection, r=compiler-errors
Streamline fast rejection Some reworkings of this code that make it a little nicer. r? `@lcnr`
2 parents 23813d8 + 0392366 commit ba21643

File tree

6 files changed

+29
-44
lines changed

6 files changed

+29
-44
lines changed

compiler/rustc_middle/src/ty/fast_reject.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::mir::Mutability;
22
use crate::ty::subst::GenericArgKind;
3-
use crate::ty::{self, Ty, TyCtxt, TypeVisitableExt};
3+
use crate::ty::{self, SubstsRef, Ty, TyCtxt, TypeVisitableExt};
44
use rustc_hir::def_id::DefId;
55
use std::fmt::Debug;
66
use std::hash::Hash;
@@ -188,22 +188,24 @@ pub struct DeepRejectCtxt {
188188
}
189189

190190
impl DeepRejectCtxt {
191-
pub fn generic_args_may_unify<'tcx>(
191+
pub fn substs_refs_may_unify<'tcx>(
192192
self,
193-
obligation_arg: ty::GenericArg<'tcx>,
194-
impl_arg: ty::GenericArg<'tcx>,
193+
obligation_substs: SubstsRef<'tcx>,
194+
impl_substs: SubstsRef<'tcx>,
195195
) -> bool {
196-
match (obligation_arg.unpack(), impl_arg.unpack()) {
197-
// We don't fast reject based on regions for now.
198-
(GenericArgKind::Lifetime(_), GenericArgKind::Lifetime(_)) => true,
199-
(GenericArgKind::Type(obl), GenericArgKind::Type(imp)) => {
200-
self.types_may_unify(obl, imp)
201-
}
202-
(GenericArgKind::Const(obl), GenericArgKind::Const(imp)) => {
203-
self.consts_may_unify(obl, imp)
196+
iter::zip(obligation_substs, impl_substs).all(|(obl, imp)| {
197+
match (obl.unpack(), imp.unpack()) {
198+
// We don't fast reject based on regions for now.
199+
(GenericArgKind::Lifetime(_), GenericArgKind::Lifetime(_)) => true,
200+
(GenericArgKind::Type(obl), GenericArgKind::Type(imp)) => {
201+
self.types_may_unify(obl, imp)
202+
}
203+
(GenericArgKind::Const(obl), GenericArgKind::Const(imp)) => {
204+
self.consts_may_unify(obl, imp)
205+
}
206+
_ => bug!("kind mismatch: {obl} {imp}"),
204207
}
205-
_ => bug!("kind mismatch: {obligation_arg} {impl_arg}"),
206-
}
208+
})
207209
}
208210

209211
pub fn types_may_unify<'tcx>(self, obligation_ty: Ty<'tcx>, impl_ty: Ty<'tcx>) -> bool {
@@ -258,9 +260,7 @@ impl DeepRejectCtxt {
258260
},
259261
ty::Adt(obl_def, obl_substs) => match k {
260262
&ty::Adt(impl_def, impl_substs) => {
261-
obl_def == impl_def
262-
&& iter::zip(obl_substs, impl_substs)
263-
.all(|(obl, imp)| self.generic_args_may_unify(obl, imp))
263+
obl_def == impl_def && self.substs_refs_may_unify(obl_substs, impl_substs)
264264
}
265265
_ => false,
266266
},

compiler/rustc_trait_selection/src/solve/project_goals.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use rustc_middle::ty::ProjectionPredicate;
1717
use rustc_middle::ty::{self, Ty, TyCtxt};
1818
use rustc_middle::ty::{ToPredicate, TypeVisitableExt};
1919
use rustc_span::{sym, DUMMY_SP};
20-
use std::iter;
2120

2221
impl<'tcx> EvalCtxt<'_, 'tcx> {
2322
#[instrument(level = "debug", skip(self), ret)]
@@ -144,9 +143,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
144143
let goal_trait_ref = goal.predicate.projection_ty.trait_ref(tcx);
145144
let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
146145
let drcx = DeepRejectCtxt { treat_obligation_params: TreatParams::ForLookup };
147-
if iter::zip(goal_trait_ref.substs, impl_trait_ref.skip_binder().substs)
148-
.any(|(goal, imp)| !drcx.generic_args_may_unify(goal, imp))
149-
{
146+
if !drcx.substs_refs_may_unify(goal_trait_ref.substs, impl_trait_ref.skip_binder().substs) {
150147
return Err(NoSolution);
151148
}
152149

compiler/rustc_trait_selection/src/solve/trait_goals.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Dealing with trait goals, i.e. `T: Trait<'a, U>`.
22
3-
use std::iter;
4-
53
use super::{assembly, EvalCtxt, SolverMode};
64
use rustc_hir::def_id::DefId;
75
use rustc_hir::LangItem;
@@ -41,9 +39,10 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
4139

4240
let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
4341
let drcx = DeepRejectCtxt { treat_obligation_params: TreatParams::ForLookup };
44-
if iter::zip(goal.predicate.trait_ref.substs, impl_trait_ref.skip_binder().substs)
45-
.any(|(goal, imp)| !drcx.generic_args_may_unify(goal, imp))
46-
{
42+
if !drcx.substs_refs_may_unify(
43+
goal.predicate.trait_ref.substs,
44+
impl_trait_ref.skip_binder().substs,
45+
) {
4746
return Err(NoSolution);
4847
}
4948

compiler/rustc_trait_selection/src/traits/coherence.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ pub fn overlapping_impls(
7979
let impl1_ref = tcx.impl_trait_ref(impl1_def_id);
8080
let impl2_ref = tcx.impl_trait_ref(impl2_def_id);
8181
let may_overlap = match (impl1_ref, impl2_ref) {
82-
(Some(a), Some(b)) => iter::zip(a.skip_binder().substs, b.skip_binder().substs)
83-
.all(|(arg1, arg2)| drcx.generic_args_may_unify(arg1, arg2)),
82+
(Some(a), Some(b)) => {
83+
drcx.substs_refs_may_unify(a.skip_binder().substs, b.skip_binder().substs)
84+
}
8485
(None, None) => {
8586
let self_ty1 = tcx.type_of(impl1_def_id).skip_binder();
8687
let self_ty2 = tcx.type_of(impl2_def_id).skip_binder();

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use hir::LangItem;
1111
use rustc_hir as hir;
1212
use rustc_infer::traits::ObligationCause;
1313
use rustc_infer::traits::{Obligation, SelectionError, TraitObligation};
14-
use rustc_middle::ty::fast_reject::TreatProjections;
14+
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, TreatProjections};
1515
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
1616

1717
use crate::traits;
@@ -344,6 +344,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
344344
return;
345345
}
346346

347+
let drcx = DeepRejectCtxt { treat_obligation_params: TreatParams::ForLookup };
348+
let obligation_substs = obligation.predicate.skip_binder().trait_ref.substs;
347349
self.tcx().for_each_relevant_impl(
348350
obligation.predicate.def_id(),
349351
obligation.predicate.skip_binder().trait_ref.self_ty(),
@@ -352,7 +354,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
352354
// consider a "quick reject". This avoids creating more types
353355
// and so forth that we need to.
354356
let impl_trait_ref = self.tcx().impl_trait_ref(impl_def_id).unwrap();
355-
if self.fast_reject_trait_refs(obligation, &impl_trait_ref.0) {
357+
if !drcx.substs_refs_may_unify(obligation_substs, impl_trait_ref.0.substs) {
356358
return;
357359
}
358360
if self.reject_fn_ptr_impls(

compiler/rustc_trait_selection/src/traits/select/mod.rs

-14
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ use rustc_infer::traits::TraitEngineExt;
4545
use rustc_middle::dep_graph::{DepKind, DepNodeIndex};
4646
use rustc_middle::mir::interpret::ErrorHandled;
4747
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
48-
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
4948
use rustc_middle::ty::fold::BottomUpFolder;
5049
use rustc_middle::ty::relate::TypeRelation;
5150
use rustc_middle::ty::SubstsRef;
@@ -2533,19 +2532,6 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
25332532
Ok(Normalized { value: impl_substs, obligations: nested_obligations })
25342533
}
25352534

2536-
fn fast_reject_trait_refs(
2537-
&mut self,
2538-
obligation: &TraitObligation<'tcx>,
2539-
impl_trait_ref: &ty::TraitRef<'tcx>,
2540-
) -> bool {
2541-
// We can avoid creating type variables and doing the full
2542-
// substitution if we find that any of the input types, when
2543-
// simplified, do not match.
2544-
let drcx = DeepRejectCtxt { treat_obligation_params: TreatParams::ForLookup };
2545-
iter::zip(obligation.predicate.skip_binder().trait_ref.substs, impl_trait_ref.substs)
2546-
.any(|(obl, imp)| !drcx.generic_args_may_unify(obl, imp))
2547-
}
2548-
25492535
/// Normalize `where_clause_trait_ref` and try to match it against
25502536
/// `obligation`. If successful, return any predicates that
25512537
/// result from the normalization.

0 commit comments

Comments
 (0)