From d470ac93347e8bc7da6af2f2dd1bddf430fc8500 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 14 Nov 2022 18:05:14 +0000 Subject: [PATCH] Drop `relate_opaque_item_substs`. --- compiler/rustc_infer/src/infer/combine.rs | 1 + compiler/rustc_middle/src/ty/relate.rs | 35 ++++++++--------------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_infer/src/infer/combine.rs b/compiler/rustc_infer/src/infer/combine.rs index a973bf54b05..45972ada6ee 100644 --- a/compiler/rustc_infer/src/infer/combine.rs +++ b/compiler/rustc_infer/src/infer/combine.rs @@ -563,6 +563,7 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> { &opt_variances, a_subst, b_subst, + true, ) } } diff --git a/compiler/rustc_middle/src/ty/relate.rs b/compiler/rustc_middle/src/ty/relate.rs index 42a1314b14d..6d02551716e 100644 --- a/compiler/rustc_middle/src/ty/relate.rs +++ b/compiler/rustc_middle/src/ty/relate.rs @@ -60,7 +60,7 @@ pub trait TypeRelation<'tcx>: Sized { let tcx = self.tcx(); let opt_variances = tcx.variances_of(item_def_id); - relate_substs_with_variances(self, item_def_id, opt_variances, a_subst, b_subst) + relate_substs_with_variances(self, item_def_id, opt_variances, a_subst, b_subst, true) } /// Switch variance for the purpose of relating `a` and `b`. @@ -151,13 +151,14 @@ pub fn relate_substs_with_variances<'tcx, R: TypeRelation<'tcx>>( variances: &[ty::Variance], a_subst: SubstsRef<'tcx>, b_subst: SubstsRef<'tcx>, + fetch_ty_for_diag: bool, ) -> RelateResult<'tcx, SubstsRef<'tcx>> { let tcx = relation.tcx(); 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 variance_info = if variance == ty::Invariant && fetch_ty_for_diag { let ty = *cached_ty.get_or_insert_with(|| tcx.bound_type_of(ty_def_id).subst(tcx, a_subst)); ty::VarianceDiagInfo::Invariant { ty, param_index: i.try_into().unwrap() } @@ -170,26 +171,6 @@ pub fn relate_substs_with_variances<'tcx, R: TypeRelation<'tcx>>( tcx.mk_substs(params) } -#[instrument(level = "trace", skip(relation), ret)] -fn relate_opaque_item_substs<'tcx, R: TypeRelation<'tcx>>( - relation: &mut R, - def_id: DefId, - a_subst: SubstsRef<'tcx>, - b_subst: SubstsRef<'tcx>, -) -> RelateResult<'tcx, SubstsRef<'tcx>> { - let tcx = relation.tcx(); - let variances = tcx.variances_of(def_id); - debug!(?variances); - - let params = iter::zip(a_subst, b_subst).enumerate().map(|(i, (a, b))| { - let variance = variances[i]; - let variance_info = ty::VarianceDiagInfo::default(); - relation.relate_with_variance(variance, variance_info, a, b) - }); - - tcx.mk_substs(params) -} - impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> { fn relate>( relation: &mut R, @@ -581,7 +562,15 @@ 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_opaque_item_substs(relation, a_def_id, a_substs, b_substs)?; + let opt_variances = tcx.variances_of(a_def_id); + let substs = relate_substs_with_variances( + relation, + a_def_id, + opt_variances, + a_substs, + b_substs, + false, // do not fetch `type_of(a_def_id)`, as it will cause a cycle + )?; Ok(tcx.mk_opaque(a_def_id, substs)) }