Skip to content

Commit 47225e8

Browse files
committed
Introduce DeepRejectCtxt::substs_refs_may_unify.
It factors out a repeated code pattern.
1 parent 478cbb4 commit 47225e8

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

compiler/rustc_middle/src/ty/fast_reject.rs

+11-4
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,6 +188,15 @@ pub struct DeepRejectCtxt {
188188
}
189189

190190
impl DeepRejectCtxt {
191+
pub fn substs_refs_may_unify<'tcx>(
192+
self,
193+
obligation_substs: SubstsRef<'tcx>,
194+
impl_substs: SubstsRef<'tcx>,
195+
) -> bool {
196+
iter::zip(obligation_substs, impl_substs)
197+
.all(|(obl, imp)| self.generic_args_may_unify(obl, imp))
198+
}
199+
191200
pub fn generic_args_may_unify<'tcx>(
192201
self,
193202
obligation_arg: ty::GenericArg<'tcx>,
@@ -258,9 +267,7 @@ impl DeepRejectCtxt {
258267
},
259268
ty::Adt(obl_def, obl_substs) => match k {
260269
&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))
270+
obl_def == impl_def && self.substs_refs_may_unify(obl_substs, impl_substs)
264271
}
265272
_ => false,
266273
},

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/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2542,8 +2542,10 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
25422542
// substitution if we find that any of the input types, when
25432543
// simplified, do not match.
25442544
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))
2545+
!drcx.substs_refs_may_unify(
2546+
obligation.predicate.skip_binder().trait_ref.substs,
2547+
impl_trait_ref.substs,
2548+
)
25472549
}
25482550

25492551
/// Normalize `where_clause_trait_ref` and try to match it against

0 commit comments

Comments
 (0)