Simplify creation of AutoBorrowMutability
This commit is contained in:
parent
6486ba94a3
commit
f9a332a48c
5 changed files with 34 additions and 48 deletions
|
@ -258,15 +258,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
|
|
||||||
let mutbl = match mutbl {
|
|
||||||
hir::Mutability::Not => AutoBorrowMutability::Not,
|
|
||||||
hir::Mutability::Mut => AutoBorrowMutability::Mut {
|
|
||||||
// For initial two-phase borrow
|
// For initial two-phase borrow
|
||||||
// deployment, conservatively omit
|
// deployment, conservatively omit
|
||||||
// overloaded function call ops.
|
// overloaded function call ops.
|
||||||
allow_two_phase_borrow: AllowTwoPhase::No,
|
let mutbl = AutoBorrowMutability::new(*mutbl, AllowTwoPhase::No);
|
||||||
},
|
|
||||||
};
|
|
||||||
autoref = Some(Adjustment {
|
autoref = Some(Adjustment {
|
||||||
kind: Adjust::Borrow(AutoBorrow::Ref(*region, mutbl)),
|
kind: Adjust::Borrow(AutoBorrow::Ref(*region, mutbl)),
|
||||||
target: method.sig.inputs()[0],
|
target: method.sig.inputs()[0],
|
||||||
|
|
|
@ -478,12 +478,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
|
||||||
let ty::Ref(r_borrow, _, _) = ty.kind() else {
|
let ty::Ref(r_borrow, _, _) = ty.kind() else {
|
||||||
span_bug!(span, "expected a ref type, got {:?}", ty);
|
span_bug!(span, "expected a ref type, got {:?}", ty);
|
||||||
};
|
};
|
||||||
let mutbl = match mutbl_b {
|
let mutbl = AutoBorrowMutability::new(mutbl_b, self.allow_two_phase);
|
||||||
hir::Mutability::Not => AutoBorrowMutability::Not,
|
|
||||||
hir::Mutability::Mut => {
|
|
||||||
AutoBorrowMutability::Mut { allow_two_phase_borrow: self.allow_two_phase }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
adjustments.push(Adjustment {
|
adjustments.push(Adjustment {
|
||||||
kind: Adjust::Borrow(AutoBorrow::Ref(*r_borrow, mutbl)),
|
kind: Adjust::Borrow(AutoBorrow::Ref(*r_borrow, mutbl)),
|
||||||
target: ty,
|
target: ty,
|
||||||
|
@ -552,15 +547,12 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
|
||||||
|
|
||||||
let coercion = Coercion(self.cause.span);
|
let coercion = Coercion(self.cause.span);
|
||||||
let r_borrow = self.next_region_var(coercion);
|
let r_borrow = self.next_region_var(coercion);
|
||||||
let mutbl = match mutbl_b {
|
|
||||||
hir::Mutability::Not => AutoBorrowMutability::Not,
|
|
||||||
hir::Mutability::Mut => AutoBorrowMutability::Mut {
|
|
||||||
// We don't allow two-phase borrows here, at least for initial
|
// We don't allow two-phase borrows here, at least for initial
|
||||||
// implementation. If it happens that this coercion is a function argument,
|
// implementation. If it happens that this coercion is a function argument,
|
||||||
// the reborrow in coerce_borrowed_ptr will pick it up.
|
// the reborrow in coerce_borrowed_ptr will pick it up.
|
||||||
allow_two_phase_borrow: AllowTwoPhase::No,
|
let mutbl = AutoBorrowMutability::new(mutbl_b, AllowTwoPhase::No);
|
||||||
},
|
|
||||||
};
|
|
||||||
Some((
|
Some((
|
||||||
Adjustment { kind: Adjust::Deref(None), target: ty_a },
|
Adjustment { kind: Adjust::Deref(None), target: ty_a },
|
||||||
Adjustment {
|
Adjustment {
|
||||||
|
|
|
@ -170,14 +170,11 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
|
||||||
let base_ty = target;
|
let base_ty = target;
|
||||||
|
|
||||||
target = self.tcx.mk_ref(region, ty::TypeAndMut { mutbl, ty: target });
|
target = self.tcx.mk_ref(region, ty::TypeAndMut { mutbl, ty: target });
|
||||||
let mutbl = match mutbl {
|
|
||||||
hir::Mutability::Not => AutoBorrowMutability::Not,
|
|
||||||
hir::Mutability::Mut => AutoBorrowMutability::Mut {
|
|
||||||
// Method call receivers are the primary use case
|
// Method call receivers are the primary use case
|
||||||
// for two-phase borrows.
|
// for two-phase borrows.
|
||||||
allow_two_phase_borrow: AllowTwoPhase::Yes,
|
let mutbl = AutoBorrowMutability::new(mutbl, AllowTwoPhase::Yes);
|
||||||
},
|
|
||||||
};
|
|
||||||
adjustments.push(Adjustment {
|
adjustments.push(Adjustment {
|
||||||
kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
|
kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
|
||||||
target,
|
target,
|
||||||
|
|
|
@ -263,14 +263,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
let by_ref_binop = !op.node.is_by_value();
|
let by_ref_binop = !op.node.is_by_value();
|
||||||
if is_assign == IsAssign::Yes || by_ref_binop {
|
if is_assign == IsAssign::Yes || by_ref_binop {
|
||||||
if let ty::Ref(region, _, mutbl) = method.sig.inputs()[0].kind() {
|
if let ty::Ref(region, _, mutbl) = method.sig.inputs()[0].kind() {
|
||||||
let mutbl = match mutbl {
|
let mutbl = AutoBorrowMutability::new(*mutbl, AllowTwoPhase::Yes);
|
||||||
hir::Mutability::Not => AutoBorrowMutability::Not,
|
|
||||||
hir::Mutability::Mut => AutoBorrowMutability::Mut {
|
|
||||||
// Allow two-phase borrows for binops in initial deployment
|
|
||||||
// since they desugar to methods
|
|
||||||
allow_two_phase_borrow: AllowTwoPhase::Yes,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
let autoref = Adjustment {
|
let autoref = Adjustment {
|
||||||
kind: Adjust::Borrow(AutoBorrow::Ref(*region, mutbl)),
|
kind: Adjust::Borrow(AutoBorrow::Ref(*region, mutbl)),
|
||||||
target: method.sig.inputs()[0],
|
target: method.sig.inputs()[0],
|
||||||
|
@ -280,14 +273,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
if by_ref_binop {
|
if by_ref_binop {
|
||||||
if let ty::Ref(region, _, mutbl) = method.sig.inputs()[1].kind() {
|
if let ty::Ref(region, _, mutbl) = method.sig.inputs()[1].kind() {
|
||||||
let mutbl = match mutbl {
|
|
||||||
hir::Mutability::Not => AutoBorrowMutability::Not,
|
|
||||||
hir::Mutability::Mut => AutoBorrowMutability::Mut {
|
|
||||||
// Allow two-phase borrows for binops in initial deployment
|
// Allow two-phase borrows for binops in initial deployment
|
||||||
// since they desugar to methods
|
// since they desugar to methods
|
||||||
allow_two_phase_borrow: AllowTwoPhase::Yes,
|
let mutbl = AutoBorrowMutability::new(*mutbl, AllowTwoPhase::Yes);
|
||||||
},
|
|
||||||
};
|
|
||||||
let autoref = Adjustment {
|
let autoref = Adjustment {
|
||||||
kind: Adjust::Borrow(AutoBorrow::Ref(*region, mutbl)),
|
kind: Adjust::Borrow(AutoBorrow::Ref(*region, mutbl)),
|
||||||
target: method.sig.inputs()[1],
|
target: method.sig.inputs()[1],
|
||||||
|
|
|
@ -159,6 +159,18 @@ pub enum AutoBorrowMutability {
|
||||||
Not,
|
Not,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AutoBorrowMutability {
|
||||||
|
/// Creates an `AutoBorrowMutability` from a mutability and allowance of two phase borrows.
|
||||||
|
///
|
||||||
|
/// Note that when `mutbl.is_not()`, `allow_two_phase_borrow` is ignored
|
||||||
|
pub fn new(mutbl: hir::Mutability, allow_two_phase_borrow: AllowTwoPhase) -> Self {
|
||||||
|
match mutbl {
|
||||||
|
hir::Mutability::Not => Self::Not,
|
||||||
|
hir::Mutability::Mut => Self::Mut { allow_two_phase_borrow },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<AutoBorrowMutability> for hir::Mutability {
|
impl From<AutoBorrowMutability> for hir::Mutability {
|
||||||
fn from(m: AutoBorrowMutability) -> Self {
|
fn from(m: AutoBorrowMutability) -> Self {
|
||||||
match m {
|
match m {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue