From: Martin Gammelsæter Date: Sun, 17 Apr 2022 09:04:58 +0000 (+0200) Subject: Split relate_substs into two functions X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=19dedf3a4b6cfd42ec46949b1d0c4416dd80d11b;p=rust.git 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. --- diff --git a/compiler/rustc_infer/src/infer/combine.rs b/compiler/rustc_infer/src/infer/combine.rs index 86229dbfad7..0ca0fe33614 100644 --- a/compiler/rustc_infer/src/infer/combine.rs +++ b/compiler/rustc_infer/src/infer/combine.rs @@ -567,11 +567,17 @@ fn relate_item_substs( // Avoid fetching the variance if we are in an invariant // context; no need, and it can induce dependency cycles // (e.g., #41849). - relate::relate_substs(self, None, a_subst, b_subst) + relate::relate_substs(self, a_subst, b_subst) } else { let tcx = self.tcx(); let opt_variances = tcx.variances_of(item_def_id); - relate::relate_substs(self, Some((item_def_id, &opt_variances)), a_subst, b_subst) + relate::relate_substs_with_variances( + self, + item_def_id, + &opt_variances, + a_subst, + b_subst, + ) } } diff --git a/compiler/rustc_infer/src/infer/equate.rs b/compiler/rustc_infer/src/infer/equate.rs index 65c0eba4b3d..ef6d464d3c6 100644 --- a/compiler/rustc_infer/src/infer/equate.rs +++ b/compiler/rustc_infer/src/infer/equate.rs @@ -53,7 +53,7 @@ fn relate_item_substs( // performing trait matching (which then performs equality // unification). - relate::relate_substs(self, None, a_subst, b_subst) + relate::relate_substs(self, a_subst, b_subst) } fn relate_with_variance>( diff --git a/compiler/rustc_middle/src/ty/relate.rs b/compiler/rustc_middle/src/ty/relate.rs index b1cfa3fa964..4c1160e21fe 100644 --- a/compiler/rustc_middle/src/ty/relate.rs +++ b/compiler/rustc_middle/src/ty/relate.rs @@ -61,7 +61,7 @@ fn relate_item_substs( let tcx = self.tcx(); let opt_variances = tcx.variances_of(item_def_id); - relate_substs(self, Some((item_def_id, opt_variances)), a_subst, b_subst) + relate_substs_with_variances(self, item_def_id, opt_variances, a_subst, b_subst) } /// Switch variance for the purpose of relating `a` and `b`. @@ -135,34 +135,39 @@ pub fn relate_type_and_mut<'tcx, R: TypeRelation<'tcx>>( } } +#[inline] pub fn relate_substs<'tcx, R: TypeRelation<'tcx>>( relation: &mut R, - variances: Option<(DefId, &[ty::Variance])>, + a_subst: SubstsRef<'tcx>, + b_subst: SubstsRef<'tcx>, +) -> RelateResult<'tcx, SubstsRef<'tcx>> { + relation.tcx().mk_substs(iter::zip(a_subst, b_subst).map(|(a, b)| { + relation.relate_with_variance(ty::Invariant, ty::VarianceDiagInfo::default(), a, b) + })) +} + +pub fn relate_substs_with_variances<'tcx, R: TypeRelation<'tcx>>( + relation: &mut R, + ty_def_id: DefId, + variances: &[ty::Variance], a_subst: SubstsRef<'tcx>, b_subst: SubstsRef<'tcx>, ) -> RelateResult<'tcx, SubstsRef<'tcx>> { let tcx = relation.tcx(); - let zipped = iter::zip(a_subst, b_subst); - match variances { - Some((ty_def_id, variances)) => { - let mut cached_ty = None; - tcx.mk_substs(zipped.enumerate().map(|(i, (a, b))| { - let variance = variances[i]; - let variance_info = if variance == ty::Invariant { - let ty = *cached_ty - .get_or_insert_with(|| tcx.type_of(ty_def_id).subst(tcx, a_subst)); - ty::VarianceDiagInfo::Invariant { ty, param_index: i.try_into().unwrap() } - } else { - ty::VarianceDiagInfo::default() - }; - relation.relate_with_variance(variance, variance_info, a, b) - })) - } - None => tcx.mk_substs(zipped.map(|(a, b)| { - relation.relate_with_variance(ty::Invariant, ty::VarianceDiagInfo::default(), a, b) - })), - } + let mut cached_ty = None; + let params = iter::zip(a_subst, b_subst).enumerate().map(|(i, (a, b))| { + let variance = variances[i]; + let variance_info = if variance == ty::Invariant { + let ty = *cached_ty.get_or_insert_with(|| tcx.type_of(ty_def_id).subst(tcx, a_subst)); + ty::VarianceDiagInfo::Invariant { ty, param_index: i.try_into().unwrap() } + } else { + ty::VarianceDiagInfo::default() + }; + relation.relate_with_variance(variance, variance_info, a, b) + }); + + tcx.mk_substs(params) } impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> { @@ -318,7 +323,7 @@ fn relate>( if a.def_id != b.def_id { Err(TypeError::Traits(expected_found(relation, a.def_id, b.def_id))) } else { - let substs = relate_substs(relation, None, a.substs, b.substs)?; + let substs = relate_substs(relation, a.substs, b.substs)?; Ok(ty::TraitRef { def_id: a.def_id, substs }) } } @@ -334,7 +339,7 @@ fn relate>( if a.def_id != b.def_id { Err(TypeError::Traits(expected_found(relation, a.def_id, b.def_id))) } else { - let substs = relate_substs(relation, None, a.substs, b.substs)?; + let substs = relate_substs(relation, a.substs, b.substs)?; Ok(ty::ExistentialTraitRef { def_id: a.def_id, substs }) } } @@ -554,7 +559,7 @@ pub fn super_relate_tys<'tcx, R: TypeRelation<'tcx>>( (&ty::Opaque(a_def_id, a_substs), &ty::Opaque(b_def_id, b_substs)) if a_def_id == b_def_id => { - let substs = relate_substs(relation, None, a_substs, b_substs)?; + let substs = relate_substs(relation, a_substs, b_substs)?; Ok(tcx.mk_opaque(a_def_id, substs)) } @@ -742,7 +747,7 @@ fn relate>( a: ty::ClosureSubsts<'tcx>, b: ty::ClosureSubsts<'tcx>, ) -> RelateResult<'tcx, ty::ClosureSubsts<'tcx>> { - let substs = relate_substs(relation, None, a.substs, b.substs)?; + let substs = relate_substs(relation, a.substs, b.substs)?; Ok(ty::ClosureSubsts { substs }) } } @@ -753,7 +758,7 @@ fn relate>( a: ty::GeneratorSubsts<'tcx>, b: ty::GeneratorSubsts<'tcx>, ) -> RelateResult<'tcx, ty::GeneratorSubsts<'tcx>> { - let substs = relate_substs(relation, None, a.substs, b.substs)?; + let substs = relate_substs(relation, a.substs, b.substs)?; Ok(ty::GeneratorSubsts { substs }) } } @@ -764,7 +769,7 @@ fn relate>( a: SubstsRef<'tcx>, b: SubstsRef<'tcx>, ) -> RelateResult<'tcx, SubstsRef<'tcx>> { - relate_substs(relation, None, a, b) + relate_substs(relation, a, b) } }