Skip to content

Commit 19dedf3

Browse files
committed
Split relate_substs into two functions
One for the case with variances, and one without. All callers use an explicit Option for the variable anyway.
1 parent 041121a commit 19dedf3

File tree

3 files changed

+42
-31
lines changed

3 files changed

+42
-31
lines changed

compiler/rustc_infer/src/infer/combine.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,17 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
567567
// Avoid fetching the variance if we are in an invariant
568568
// context; no need, and it can induce dependency cycles
569569
// (e.g., #41849).
570-
relate::relate_substs(self, None, a_subst, b_subst)
570+
relate::relate_substs(self, a_subst, b_subst)
571571
} else {
572572
let tcx = self.tcx();
573573
let opt_variances = tcx.variances_of(item_def_id);
574-
relate::relate_substs(self, Some((item_def_id, &opt_variances)), a_subst, b_subst)
574+
relate::relate_substs_with_variances(
575+
self,
576+
item_def_id,
577+
&opt_variances,
578+
a_subst,
579+
b_subst,
580+
)
575581
}
576582
}
577583

compiler/rustc_infer/src/infer/equate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
5353
// performing trait matching (which then performs equality
5454
// unification).
5555

56-
relate::relate_substs(self, None, a_subst, b_subst)
56+
relate::relate_substs(self, a_subst, b_subst)
5757
}
5858

5959
fn relate_with_variance<T: Relate<'tcx>>(

compiler/rustc_middle/src/ty/relate.rs

+33-28
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub trait TypeRelation<'tcx>: Sized {
6161

6262
let tcx = self.tcx();
6363
let opt_variances = tcx.variances_of(item_def_id);
64-
relate_substs(self, Some((item_def_id, opt_variances)), a_subst, b_subst)
64+
relate_substs_with_variances(self, item_def_id, opt_variances, a_subst, b_subst)
6565
}
6666

6767
/// Switch variance for the purpose of relating `a` and `b`.
@@ -135,34 +135,39 @@ pub fn relate_type_and_mut<'tcx, R: TypeRelation<'tcx>>(
135135
}
136136
}
137137

138+
#[inline]
138139
pub fn relate_substs<'tcx, R: TypeRelation<'tcx>>(
139140
relation: &mut R,
140-
variances: Option<(DefId, &[ty::Variance])>,
141+
a_subst: SubstsRef<'tcx>,
142+
b_subst: SubstsRef<'tcx>,
143+
) -> RelateResult<'tcx, SubstsRef<'tcx>> {
144+
relation.tcx().mk_substs(iter::zip(a_subst, b_subst).map(|(a, b)| {
145+
relation.relate_with_variance(ty::Invariant, ty::VarianceDiagInfo::default(), a, b)
146+
}))
147+
}
148+
149+
pub fn relate_substs_with_variances<'tcx, R: TypeRelation<'tcx>>(
150+
relation: &mut R,
151+
ty_def_id: DefId,
152+
variances: &[ty::Variance],
141153
a_subst: SubstsRef<'tcx>,
142154
b_subst: SubstsRef<'tcx>,
143155
) -> RelateResult<'tcx, SubstsRef<'tcx>> {
144156
let tcx = relation.tcx();
145157

146-
let zipped = iter::zip(a_subst, b_subst);
147-
match variances {
148-
Some((ty_def_id, variances)) => {
149-
let mut cached_ty = None;
150-
tcx.mk_substs(zipped.enumerate().map(|(i, (a, b))| {
151-
let variance = variances[i];
152-
let variance_info = if variance == ty::Invariant {
153-
let ty = *cached_ty
154-
.get_or_insert_with(|| tcx.type_of(ty_def_id).subst(tcx, a_subst));
155-
ty::VarianceDiagInfo::Invariant { ty, param_index: i.try_into().unwrap() }
156-
} else {
157-
ty::VarianceDiagInfo::default()
158-
};
159-
relation.relate_with_variance(variance, variance_info, a, b)
160-
}))
161-
}
162-
None => tcx.mk_substs(zipped.map(|(a, b)| {
163-
relation.relate_with_variance(ty::Invariant, ty::VarianceDiagInfo::default(), a, b)
164-
})),
165-
}
158+
let mut cached_ty = None;
159+
let params = iter::zip(a_subst, b_subst).enumerate().map(|(i, (a, b))| {
160+
let variance = variances[i];
161+
let variance_info = if variance == ty::Invariant {
162+
let ty = *cached_ty.get_or_insert_with(|| tcx.type_of(ty_def_id).subst(tcx, a_subst));
163+
ty::VarianceDiagInfo::Invariant { ty, param_index: i.try_into().unwrap() }
164+
} else {
165+
ty::VarianceDiagInfo::default()
166+
};
167+
relation.relate_with_variance(variance, variance_info, a, b)
168+
});
169+
170+
tcx.mk_substs(params)
166171
}
167172

168173
impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
@@ -318,7 +323,7 @@ impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
318323
if a.def_id != b.def_id {
319324
Err(TypeError::Traits(expected_found(relation, a.def_id, b.def_id)))
320325
} else {
321-
let substs = relate_substs(relation, None, a.substs, b.substs)?;
326+
let substs = relate_substs(relation, a.substs, b.substs)?;
322327
Ok(ty::TraitRef { def_id: a.def_id, substs })
323328
}
324329
}
@@ -334,7 +339,7 @@ impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
334339
if a.def_id != b.def_id {
335340
Err(TypeError::Traits(expected_found(relation, a.def_id, b.def_id)))
336341
} else {
337-
let substs = relate_substs(relation, None, a.substs, b.substs)?;
342+
let substs = relate_substs(relation, a.substs, b.substs)?;
338343
Ok(ty::ExistentialTraitRef { def_id: a.def_id, substs })
339344
}
340345
}
@@ -554,7 +559,7 @@ pub fn super_relate_tys<'tcx, R: TypeRelation<'tcx>>(
554559
(&ty::Opaque(a_def_id, a_substs), &ty::Opaque(b_def_id, b_substs))
555560
if a_def_id == b_def_id =>
556561
{
557-
let substs = relate_substs(relation, None, a_substs, b_substs)?;
562+
let substs = relate_substs(relation, a_substs, b_substs)?;
558563
Ok(tcx.mk_opaque(a_def_id, substs))
559564
}
560565

@@ -742,7 +747,7 @@ impl<'tcx> Relate<'tcx> for ty::ClosureSubsts<'tcx> {
742747
a: ty::ClosureSubsts<'tcx>,
743748
b: ty::ClosureSubsts<'tcx>,
744749
) -> RelateResult<'tcx, ty::ClosureSubsts<'tcx>> {
745-
let substs = relate_substs(relation, None, a.substs, b.substs)?;
750+
let substs = relate_substs(relation, a.substs, b.substs)?;
746751
Ok(ty::ClosureSubsts { substs })
747752
}
748753
}
@@ -753,7 +758,7 @@ impl<'tcx> Relate<'tcx> for ty::GeneratorSubsts<'tcx> {
753758
a: ty::GeneratorSubsts<'tcx>,
754759
b: ty::GeneratorSubsts<'tcx>,
755760
) -> RelateResult<'tcx, ty::GeneratorSubsts<'tcx>> {
756-
let substs = relate_substs(relation, None, a.substs, b.substs)?;
761+
let substs = relate_substs(relation, a.substs, b.substs)?;
757762
Ok(ty::GeneratorSubsts { substs })
758763
}
759764
}
@@ -764,7 +769,7 @@ impl<'tcx> Relate<'tcx> for SubstsRef<'tcx> {
764769
a: SubstsRef<'tcx>,
765770
b: SubstsRef<'tcx>,
766771
) -> RelateResult<'tcx, SubstsRef<'tcx>> {
767-
relate_substs(relation, None, a, b)
772+
relate_substs(relation, a, b)
768773
}
769774
}
770775

0 commit comments

Comments
 (0)