Skip to content

Commit f9a332a

Browse files
committed
Simplify creation of AutoBorrowMutability
1 parent 6486ba9 commit f9a332a

File tree

5 files changed

+34
-48
lines changed

5 files changed

+34
-48
lines changed

compiler/rustc_hir_typeck/src/callee.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
258258
return None;
259259
};
260260

261-
let mutbl = match mutbl {
262-
hir::Mutability::Not => AutoBorrowMutability::Not,
263-
hir::Mutability::Mut => AutoBorrowMutability::Mut {
264-
// For initial two-phase borrow
265-
// deployment, conservatively omit
266-
// overloaded function call ops.
267-
allow_two_phase_borrow: AllowTwoPhase::No,
268-
},
269-
};
261+
// For initial two-phase borrow
262+
// deployment, conservatively omit
263+
// overloaded function call ops.
264+
let mutbl = AutoBorrowMutability::new(*mutbl, AllowTwoPhase::No);
265+
270266
autoref = Some(Adjustment {
271267
kind: Adjust::Borrow(AutoBorrow::Ref(*region, mutbl)),
272268
target: method.sig.inputs()[0],

compiler/rustc_hir_typeck/src/coercion.rs

+7-15
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
478478
let ty::Ref(r_borrow, _, _) = ty.kind() else {
479479
span_bug!(span, "expected a ref type, got {:?}", ty);
480480
};
481-
let mutbl = match mutbl_b {
482-
hir::Mutability::Not => AutoBorrowMutability::Not,
483-
hir::Mutability::Mut => {
484-
AutoBorrowMutability::Mut { allow_two_phase_borrow: self.allow_two_phase }
485-
}
486-
};
481+
let mutbl = AutoBorrowMutability::new(mutbl_b, self.allow_two_phase);
487482
adjustments.push(Adjustment {
488483
kind: Adjust::Borrow(AutoBorrow::Ref(*r_borrow, mutbl)),
489484
target: ty,
@@ -552,15 +547,12 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
552547

553548
let coercion = Coercion(self.cause.span);
554549
let r_borrow = self.next_region_var(coercion);
555-
let mutbl = match mutbl_b {
556-
hir::Mutability::Not => AutoBorrowMutability::Not,
557-
hir::Mutability::Mut => AutoBorrowMutability::Mut {
558-
// We don't allow two-phase borrows here, at least for initial
559-
// implementation. If it happens that this coercion is a function argument,
560-
// the reborrow in coerce_borrowed_ptr will pick it up.
561-
allow_two_phase_borrow: AllowTwoPhase::No,
562-
},
563-
};
550+
551+
// We don't allow two-phase borrows here, at least for initial
552+
// implementation. If it happens that this coercion is a function argument,
553+
// the reborrow in coerce_borrowed_ptr will pick it up.
554+
let mutbl = AutoBorrowMutability::new(mutbl_b, AllowTwoPhase::No);
555+
564556
Some((
565557
Adjustment { kind: Adjust::Deref(None), target: ty_a },
566558
Adjustment {

compiler/rustc_hir_typeck/src/method/confirm.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,11 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
170170
let base_ty = target;
171171

172172
target = self.tcx.mk_ref(region, ty::TypeAndMut { mutbl, ty: target });
173-
let mutbl = match mutbl {
174-
hir::Mutability::Not => AutoBorrowMutability::Not,
175-
hir::Mutability::Mut => AutoBorrowMutability::Mut {
176-
// Method call receivers are the primary use case
177-
// for two-phase borrows.
178-
allow_two_phase_borrow: AllowTwoPhase::Yes,
179-
},
180-
};
173+
174+
// Method call receivers are the primary use case
175+
// for two-phase borrows.
176+
let mutbl = AutoBorrowMutability::new(mutbl, AllowTwoPhase::Yes);
177+
181178
adjustments.push(Adjustment {
182179
kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
183180
target,

compiler/rustc_hir_typeck/src/op.rs

+5-16
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
263263
let by_ref_binop = !op.node.is_by_value();
264264
if is_assign == IsAssign::Yes || by_ref_binop {
265265
if let ty::Ref(region, _, mutbl) = method.sig.inputs()[0].kind() {
266-
let mutbl = match mutbl {
267-
hir::Mutability::Not => AutoBorrowMutability::Not,
268-
hir::Mutability::Mut => AutoBorrowMutability::Mut {
269-
// Allow two-phase borrows for binops in initial deployment
270-
// since they desugar to methods
271-
allow_two_phase_borrow: AllowTwoPhase::Yes,
272-
},
273-
};
266+
let mutbl = AutoBorrowMutability::new(*mutbl, AllowTwoPhase::Yes);
274267
let autoref = Adjustment {
275268
kind: Adjust::Borrow(AutoBorrow::Ref(*region, mutbl)),
276269
target: method.sig.inputs()[0],
@@ -280,14 +273,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
280273
}
281274
if by_ref_binop {
282275
if let ty::Ref(region, _, mutbl) = method.sig.inputs()[1].kind() {
283-
let mutbl = match mutbl {
284-
hir::Mutability::Not => AutoBorrowMutability::Not,
285-
hir::Mutability::Mut => AutoBorrowMutability::Mut {
286-
// Allow two-phase borrows for binops in initial deployment
287-
// since they desugar to methods
288-
allow_two_phase_borrow: AllowTwoPhase::Yes,
289-
},
290-
};
276+
// Allow two-phase borrows for binops in initial deployment
277+
// since they desugar to methods
278+
let mutbl = AutoBorrowMutability::new(*mutbl, AllowTwoPhase::Yes);
279+
291280
let autoref = Adjustment {
292281
kind: Adjust::Borrow(AutoBorrow::Ref(*region, mutbl)),
293282
target: method.sig.inputs()[1],

compiler/rustc_middle/src/ty/adjustment.rs

+12
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,18 @@ pub enum AutoBorrowMutability {
159159
Not,
160160
}
161161

162+
impl AutoBorrowMutability {
163+
/// Creates an `AutoBorrowMutability` from a mutability and allowance of two phase borrows.
164+
///
165+
/// Note that when `mutbl.is_not()`, `allow_two_phase_borrow` is ignored
166+
pub fn new(mutbl: hir::Mutability, allow_two_phase_borrow: AllowTwoPhase) -> Self {
167+
match mutbl {
168+
hir::Mutability::Not => Self::Not,
169+
hir::Mutability::Mut => Self::Mut { allow_two_phase_borrow },
170+
}
171+
}
172+
}
173+
162174
impl From<AutoBorrowMutability> for hir::Mutability {
163175
fn from(m: AutoBorrowMutability) -> Self {
164176
match m {

0 commit comments

Comments
 (0)