From 24db8eaefd794624ba79569bfcfaed65d8134634 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 21 Mar 2024 16:50:10 -0400 Subject: [PATCH] Remove TypeAndMut from relate --- compiler/rustc_middle/src/ty/relate.rs | 61 ++++++++++++++------------ 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/compiler/rustc_middle/src/ty/relate.rs b/compiler/rustc_middle/src/ty/relate.rs index 990e78aff8af..764ee8d5652c 100644 --- a/compiler/rustc_middle/src/ty/relate.rs +++ b/compiler/rustc_middle/src/ty/relate.rs @@ -94,28 +94,6 @@ pub trait Relate<'tcx>: TypeFoldable> + PartialEq + Copy { /////////////////////////////////////////////////////////////////////////// // Relate impls -pub fn relate_type_and_mut<'tcx, R: TypeRelation<'tcx>>( - relation: &mut R, - a: ty::TypeAndMut<'tcx>, - b: ty::TypeAndMut<'tcx>, - base_ty: Ty<'tcx>, -) -> RelateResult<'tcx, ty::TypeAndMut<'tcx>> { - debug!("{}.mts({:?}, {:?})", relation.tag(), a, b); - if a.mutbl != b.mutbl { - Err(TypeError::Mutability) - } else { - let mutbl = a.mutbl; - let (variance, info) = match mutbl { - hir::Mutability::Not => (ty::Covariant, ty::VarianceDiagInfo::None), - hir::Mutability::Mut => { - (ty::Invariant, ty::VarianceDiagInfo::Invariant { ty: base_ty, param_index: 0 }) - } - }; - let ty = relation.relate_with_variance(variance, info, a.ty, b.ty)?; - Ok(ty::TypeAndMut { ty, mutbl }) - } -} - #[inline] pub fn relate_args_invariantly<'tcx, R: TypeRelation<'tcx>>( relation: &mut R, @@ -465,17 +443,42 @@ pub fn structurally_relate_tys<'tcx, R: TypeRelation<'tcx>>( Ok(Ty::new_coroutine_closure(tcx, a_id, args)) } - (&ty::RawPtr(a_mt), &ty::RawPtr(b_mt)) => { - let mt = relate_type_and_mut(relation, a_mt, b_mt, a)?; - Ok(Ty::new_ptr(tcx, mt)) + ( + &ty::RawPtr(ty::TypeAndMut { ty: a_ty, mutbl: a_mutbl }), + &ty::RawPtr(ty::TypeAndMut { ty: b_ty, mutbl: b_mutbl }), + ) => { + if a_mutbl != b_mutbl { + return Err(TypeError::Mutability); + } + + let (variance, info) = match a_mutbl { + hir::Mutability::Not => (ty::Covariant, ty::VarianceDiagInfo::None), + hir::Mutability::Mut => { + (ty::Invariant, ty::VarianceDiagInfo::Invariant { ty: a, param_index: 0 }) + } + }; + + let ty = relation.relate_with_variance(variance, info, a_ty, b_ty)?; + + Ok(Ty::new_ptr(tcx, ty::TypeAndMut { mutbl: a_mutbl, ty })) } (&ty::Ref(a_r, a_ty, a_mutbl), &ty::Ref(b_r, b_ty, b_mutbl)) => { + if a_mutbl != b_mutbl { + return Err(TypeError::Mutability); + } + + let (variance, info) = match a_mutbl { + hir::Mutability::Not => (ty::Covariant, ty::VarianceDiagInfo::None), + hir::Mutability::Mut => { + (ty::Invariant, ty::VarianceDiagInfo::Invariant { ty: a, param_index: 0 }) + } + }; + let r = relation.relate(a_r, b_r)?; - let a_mt = ty::TypeAndMut { ty: a_ty, mutbl: a_mutbl }; - let b_mt = ty::TypeAndMut { ty: b_ty, mutbl: b_mutbl }; - let mt = relate_type_and_mut(relation, a_mt, b_mt, a)?; - Ok(Ty::new_ref(tcx, r, mt)) + let ty = relation.relate_with_variance(variance, info, a_ty, b_ty)?; + + Ok(Ty::new_ref(tcx, r, ty::TypeAndMut { mutbl: a_mutbl, ty })) } (&ty::Array(a_t, sz_a), &ty::Array(b_t, sz_b)) => {