From 9b9c7d0ecc9d902b3ed5eac2f06f8669fed57f43 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Wed, 23 Nov 2022 18:22:26 +0000 Subject: [PATCH] Depend on `Mutability` ordering --- compiler/rustc_hir_analysis/src/coherence/builtin.rs | 2 +- compiler/rustc_hir_typeck/src/_match.rs | 5 +---- compiler/rustc_hir_typeck/src/cast.rs | 2 +- compiler/rustc_hir_typeck/src/coercion.rs | 6 +----- compiler/rustc_lint/src/builtin.rs | 4 ++-- 5 files changed, 6 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/coherence/builtin.rs b/compiler/rustc_hir_analysis/src/coherence/builtin.rs index 11661215ae1..70943eb7943 100644 --- a/compiler/rustc_hir_analysis/src/coherence/builtin.rs +++ b/compiler/rustc_hir_analysis/src/coherence/builtin.rs @@ -370,7 +370,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUn let check_mutbl = |mt_a: ty::TypeAndMut<'tcx>, mt_b: ty::TypeAndMut<'tcx>, mk_ptr: &dyn Fn(Ty<'tcx>) -> Ty<'tcx>| { - if (mt_a.mutbl, mt_b.mutbl) == (hir::Mutability::Not, hir::Mutability::Mut) { + if mt_a.mutbl < mt_b.mutbl { infcx .err_ctxt() .report_mismatched_types( diff --git a/compiler/rustc_hir_typeck/src/_match.rs b/compiler/rustc_hir_typeck/src/_match.rs index 31432bb6e41..1d55378b91b 100644 --- a/compiler/rustc_hir_typeck/src/_match.rs +++ b/compiler/rustc_hir_typeck/src/_match.rs @@ -572,8 +572,5 @@ pub(crate) fn opt_suggest_box_span( } fn arms_contain_ref_bindings<'tcx>(arms: &'tcx [hir::Arm<'tcx>]) -> Option { - arms.iter().filter_map(|a| a.pat.contains_explicit_ref_binding()).max_by_key(|m| match *m { - hir::Mutability::Mut => 1, - hir::Mutability::Not => 0, - }) + arms.iter().filter_map(|a| a.pat.contains_explicit_ref_binding()).max() } diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs index 5e1e44dcb6d..1cea8c9dadc 100644 --- a/compiler/rustc_hir_typeck/src/cast.rs +++ b/compiler/rustc_hir_typeck/src/cast.rs @@ -951,7 +951,7 @@ fn check_ref_cast( m_cast: ty::TypeAndMut<'tcx>, ) -> Result { // array-ptr-cast: allow mut-to-mut, mut-to-const, const-to-const - if m_expr.mutbl == hir::Mutability::Mut || m_cast.mutbl == hir::Mutability::Not { + if m_expr.mutbl >= m_cast.mutbl { if let ty::Array(ety, _) = m_expr.ty.kind() { // Due to the limitations of LLVM global constants, // region pointers end up pointing at copies of diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 3f0d0a76027..0dffde40eeb 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -108,11 +108,7 @@ fn coerce_mutbls<'tcx>( from_mutbl: hir::Mutability, to_mutbl: hir::Mutability, ) -> RelateResult<'tcx, ()> { - match (from_mutbl, to_mutbl) { - (hir::Mutability::Mut, hir::Mutability::Mut | hir::Mutability::Not) - | (hir::Mutability::Not, hir::Mutability::Not) => Ok(()), - (hir::Mutability::Not, hir::Mutability::Mut) => Err(TypeError::Mutability), - } + if from_mutbl >= to_mutbl { Ok(()) } else { Err(TypeError::Mutability) } } /// Do not require any adjustments, i.e. coerce `x -> x`. diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index ada3c3b67fb..914ff1ab3da 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1268,10 +1268,10 @@ fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) { impl<'tcx> LateLintPass<'tcx> for MutableTransmutes { fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) { - if let Some((&ty::Ref(_, _, from_mt), &ty::Ref(_, _, to_mt))) = + if let Some((&ty::Ref(_, _, from_mutbl), &ty::Ref(_, _, to_mutbl))) = get_transmute_from_to(cx, expr).map(|(ty1, ty2)| (ty1.kind(), ty2.kind())) { - if to_mt == hir::Mutability::Mut && from_mt == hir::Mutability::Not { + if from_mutbl < to_mutbl { cx.struct_span_lint( MUTABLE_TRANSMUTES, expr.span,