Skip to content

Commit 9b9c7d0

Browse files
committed
Depend on Mutability ordering
1 parent 8195e12 commit 9b9c7d0

File tree

5 files changed

+6
-13
lines changed

5 files changed

+6
-13
lines changed

compiler/rustc_hir_analysis/src/coherence/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUn
370370
let check_mutbl = |mt_a: ty::TypeAndMut<'tcx>,
371371
mt_b: ty::TypeAndMut<'tcx>,
372372
mk_ptr: &dyn Fn(Ty<'tcx>) -> Ty<'tcx>| {
373-
if (mt_a.mutbl, mt_b.mutbl) == (hir::Mutability::Not, hir::Mutability::Mut) {
373+
if mt_a.mutbl < mt_b.mutbl {
374374
infcx
375375
.err_ctxt()
376376
.report_mismatched_types(

compiler/rustc_hir_typeck/src/_match.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,5 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
572572
}
573573

574574
fn arms_contain_ref_bindings<'tcx>(arms: &'tcx [hir::Arm<'tcx>]) -> Option<hir::Mutability> {
575-
arms.iter().filter_map(|a| a.pat.contains_explicit_ref_binding()).max_by_key(|m| match *m {
576-
hir::Mutability::Mut => 1,
577-
hir::Mutability::Not => 0,
578-
})
575+
arms.iter().filter_map(|a| a.pat.contains_explicit_ref_binding()).max()
579576
}

compiler/rustc_hir_typeck/src/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
951951
m_cast: ty::TypeAndMut<'tcx>,
952952
) -> Result<CastKind, CastError> {
953953
// array-ptr-cast: allow mut-to-mut, mut-to-const, const-to-const
954-
if m_expr.mutbl == hir::Mutability::Mut || m_cast.mutbl == hir::Mutability::Not {
954+
if m_expr.mutbl >= m_cast.mutbl {
955955
if let ty::Array(ety, _) = m_expr.ty.kind() {
956956
// Due to the limitations of LLVM global constants,
957957
// region pointers end up pointing at copies of

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,7 @@ fn coerce_mutbls<'tcx>(
108108
from_mutbl: hir::Mutability,
109109
to_mutbl: hir::Mutability,
110110
) -> RelateResult<'tcx, ()> {
111-
match (from_mutbl, to_mutbl) {
112-
(hir::Mutability::Mut, hir::Mutability::Mut | hir::Mutability::Not)
113-
| (hir::Mutability::Not, hir::Mutability::Not) => Ok(()),
114-
(hir::Mutability::Not, hir::Mutability::Mut) => Err(TypeError::Mutability),
115-
}
111+
if from_mutbl >= to_mutbl { Ok(()) } else { Err(TypeError::Mutability) }
116112
}
117113

118114
/// Do not require any adjustments, i.e. coerce `x -> x`.

compiler/rustc_lint/src/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,10 +1268,10 @@ declare_lint_pass!(MutableTransmutes => [MUTABLE_TRANSMUTES]);
12681268

12691269
impl<'tcx> LateLintPass<'tcx> for MutableTransmutes {
12701270
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
1271-
if let Some((&ty::Ref(_, _, from_mt), &ty::Ref(_, _, to_mt))) =
1271+
if let Some((&ty::Ref(_, _, from_mutbl), &ty::Ref(_, _, to_mutbl))) =
12721272
get_transmute_from_to(cx, expr).map(|(ty1, ty2)| (ty1.kind(), ty2.kind()))
12731273
{
1274-
if to_mt == hir::Mutability::Mut && from_mt == hir::Mutability::Not {
1274+
if from_mutbl < to_mutbl {
12751275
cx.struct_span_lint(
12761276
MUTABLE_TRANSMUTES,
12771277
expr.span,

0 commit comments

Comments
 (0)