Skip to content

Commit 3b3699f

Browse files
committed
Colocate Rvalue::Ref and Rvalue::Reborrow
1 parent 02d9f7a commit 3b3699f

22 files changed

Lines changed: 136 additions & 102 deletions

File tree

compiler/rustc_borrowck/src/borrow_set.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherBorrows<'a, 'tcx> {
312312
else {
313313
unreachable!()
314314
};
315-
let Some(ty::GenericArgKind::Lifetime(region)) =
316-
assigned_args.get(0).map(|r| r.kind())
315+
let Some(ty::GenericArgKind::Lifetime(region)) = assigned_args.get(0).map(|r| r.kind())
317316
else {
318317
bug!(
319318
"hir-typeck passed but {} does not have a lifetime argument",

compiler/rustc_borrowck/src/lib.rs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,36 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
14951495
);
14961496
}
14971497

1498+
&Rvalue::Reborrow(mutability, place) => {
1499+
let access_kind = (
1500+
Deep,
1501+
if mutability == Mutability::Mut {
1502+
Write(WriteKind::MutableBorrow(BorrowKind::Mut {
1503+
kind: MutBorrowKind::Default,
1504+
}))
1505+
} else {
1506+
Read(ReadKind::Borrow(BorrowKind::Shared))
1507+
},
1508+
);
1509+
1510+
self.access_place(
1511+
location,
1512+
(place, span),
1513+
access_kind,
1514+
LocalMutationIsAllowed::Yes,
1515+
state,
1516+
);
1517+
1518+
let action = InitializationRequiringAction::Borrow;
1519+
1520+
self.check_if_path_or_subpath_is_moved(
1521+
location,
1522+
action,
1523+
(place.as_ref(), span),
1524+
state,
1525+
);
1526+
}
1527+
14981528
&Rvalue::RawPtr(kind, place) => {
14991529
let access_kind = match kind {
15001530
RawPtrKind::Mut => (
@@ -1594,35 +1624,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
15941624
}
15951625

15961626
Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in borrowck"),
1597-
&Rvalue::Reborrow(mutability, place) => {
1598-
let access_kind = (
1599-
Deep,
1600-
if mutability == Mutability::Mut {
1601-
Write(WriteKind::MutableBorrow(BorrowKind::Mut {
1602-
kind: MutBorrowKind::Default,
1603-
}))
1604-
} else {
1605-
Read(ReadKind::Borrow(BorrowKind::Shared))
1606-
},
1607-
);
1608-
1609-
self.access_place(
1610-
location,
1611-
(place, span),
1612-
access_kind,
1613-
LocalMutationIsAllowed::Yes,
1614-
state,
1615-
);
1616-
1617-
let action = InitializationRequiringAction::Borrow;
1618-
1619-
self.check_if_path_or_subpath_is_moved(
1620-
location,
1621-
action,
1622-
(place.as_ref(), span),
1623-
state,
1624-
);
1625-
}
16261627
}
16271628
}
16281629

compiler/rustc_borrowck/src/polonius/legacy/loan_invalidations.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,21 @@ impl<'a, 'tcx> LoanInvalidationsGenerator<'a, 'tcx> {
275275
self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);
276276
}
277277

278+
&Rvalue::Reborrow(mutability, place) => {
279+
let access_kind = (
280+
Deep,
281+
if mutability == Mutability::Mut {
282+
Reservation(WriteKind::MutableBorrow(BorrowKind::Mut {
283+
kind: MutBorrowKind::TwoPhaseBorrow,
284+
}))
285+
} else {
286+
Read(ReadKind::Borrow(BorrowKind::Shared))
287+
},
288+
);
289+
290+
self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);
291+
}
292+
278293
&Rvalue::RawPtr(kind, place) => {
279294
let access_kind = match kind {
280295
RawPtrKind::Mut => (
@@ -325,20 +340,6 @@ impl<'a, 'tcx> LoanInvalidationsGenerator<'a, 'tcx> {
325340
}
326341

327342
Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in borrowck"),
328-
&Rvalue::Reborrow(mutability, place) => {
329-
let access_kind = (
330-
Deep,
331-
if mutability == Mutability::Mut {
332-
Reservation(WriteKind::MutableBorrow(BorrowKind::Mut {
333-
kind: MutBorrowKind::TwoPhaseBorrow,
334-
}))
335-
} else {
336-
Read(ReadKind::Borrow(BorrowKind::Shared))
337-
},
338-
);
339-
340-
self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);
341-
}
342343
}
343344
}
344345

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,12 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
16131613
self.add_reborrow_constraint(location, *region, borrowed_place);
16141614
}
16151615

1616+
Rvalue::Reborrow(..) => {
1617+
// Reborrow needs to produce a relation between the source and destination fields,
1618+
// which means that we have had to already handle this in visit_assign.
1619+
unreachable!()
1620+
}
1621+
16161622
Rvalue::BinaryOp(
16171623
BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge,
16181624
box (left, right),
@@ -1690,8 +1696,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
16901696
| Rvalue::BinaryOp(..)
16911697
| Rvalue::RawPtr(..)
16921698
| Rvalue::ThreadLocalRef(..)
1693-
| Rvalue::Discriminant(..)
1694-
| Rvalue::Reborrow(..) => {}
1699+
| Rvalue::Discriminant(..) => {}
16951700
}
16961701
}
16971702

@@ -2250,15 +2255,15 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
22502255
| Rvalue::ThreadLocalRef(_)
22512256
| Rvalue::Repeat(..)
22522257
| Rvalue::Ref(..)
2258+
| Rvalue::Reborrow(..)
22532259
| Rvalue::RawPtr(..)
22542260
| Rvalue::Cast(..)
22552261
| Rvalue::ShallowInitBox(..)
22562262
| Rvalue::BinaryOp(..)
22572263
| Rvalue::CopyForDeref(..)
22582264
| Rvalue::UnaryOp(..)
22592265
| Rvalue::Discriminant(..)
2260-
| Rvalue::WrapUnsafeBinder(..)
2261-
| Rvalue::Reborrow(..) => None,
2266+
| Rvalue::WrapUnsafeBinder(..) => None,
22622267

22632268
Rvalue::Aggregate(aggregate, _) => match **aggregate {
22642269
AggregateKind::Adt(_, _, _, user_ty, _) => user_ty,
@@ -2464,7 +2469,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
24642469
borrowed_place: &Place<'tcx>,
24652470
) {
24662471
// These constraints are only meaningful during borrowck:
2467-
let Self { borrow_set, location_table, polonius_facts, constraints, infcx, body, .. } = self;
2472+
let Self { borrow_set, location_table, polonius_facts, constraints, infcx, body, .. } =
2473+
self;
24682474

24692475
// If we are reborrowing the referent of another reference, we
24702476
// need to add outlives relationships. In a case like `&mut

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -624,16 +624,16 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
624624
let val = codegen_operand(fx, operand);
625625
lval.write_cvalue(fx, val);
626626
}
627-
Rvalue::Reborrow(_, place) => {
628-
let cplace = codegen_place(fx, place);
629-
let val = cplace.to_cvalue(fx);
630-
lval.write_cvalue(fx, val)
631-
}
632627
Rvalue::Ref(_, _, place) | Rvalue::RawPtr(_, place) => {
633628
let place = codegen_place(fx, place);
634629
let ref_ = place.place_ref(fx, lval.layout());
635630
lval.write_cvalue(fx, ref_);
636631
}
632+
Rvalue::Reborrow(_, place) => {
633+
let cplace = codegen_place(fx, place);
634+
let val = cplace.to_cvalue(fx);
635+
lval.write_cvalue(fx, val)
636+
}
637637
Rvalue::ThreadLocalRef(def_id) => {
638638
let val = crate::constant::codegen_tls_ref(fx, def_id, lval.layout());
639639
lval.write_cvalue(fx, val);

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
510510
self.codegen_place_to_pointer(bx, place, mk_ref)
511511
}
512512

513+
// Note: Exclusive reborrowing is always equal to a memcpy, as the types do not change.
514+
// Generic shared reborrowing is not (necessarily) a simple memcpy, but currently the
515+
// coherence check places such restrictions on the CoerceShared trait as to guarantee
516+
// that it is.
517+
mir::Rvalue::Reborrow(_, place) => self.codegen_operand(bx, &mir::Operand::Copy(place)),
518+
513519
mir::Rvalue::RawPtr(kind, place) => {
514520
let mk_ptr = move |tcx: TyCtxt<'tcx>, ty: Ty<'tcx>| {
515521
Ty::new_ptr(tcx, ty, kind.to_mutbl_lossy())
@@ -709,12 +715,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
709715
let layout = bx.cx().layout_of(binder_ty);
710716
OperandRef { val: operand.val, layout, move_annotation: None }
711717
}
712-
// Note: Exclusive reborrowing is always equal to a memcpy, as the
713-
// types do not change. Generic shared reborrowing is not
714-
// (necessarily) a simple memcpy, but currently the coherence check
715-
// places such restrictions on the CoerceShared trait as to
716-
// guarantee that it is.
717-
mir::Rvalue::Reborrow(_, place) => self.codegen_operand(bx, &mir::Operand::Copy(place)),
718718
mir::Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in codegen"),
719719
mir::Rvalue::ShallowInitBox(..) => bug!("`ShallowInitBox` in codegen"),
720720
}

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,10 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
610610
}
611611
}
612612

613+
Rvalue::Reborrow(..) => {
614+
// FIXME(@aapoalas): figure out if this is relevant at all.
615+
}
616+
613617
Rvalue::RawPtr(RawPtrKind::FakeForPtrMetadata, place) => {
614618
// These are only inserted for slice length, so the place must already be indirect.
615619
// This implies we do not have to worry about whether the borrow escapes.
@@ -699,8 +703,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
699703
Rvalue::WrapUnsafeBinder(..) => {
700704
// Unsafe binders are always trivial to create.
701705
}
702-
703-
Rvalue::Reborrow(..) => {}
704706
}
705707
}
706708

compiler/rustc_const_eval/src/check_consts/qualifs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ where
256256
in_place::<Q, _>(cx, in_local, place.as_ref())
257257
}
258258

259+
Rvalue::Reborrow(_, place) => in_place::<Q, _>(cx, in_local, place.as_ref()),
260+
259261
Rvalue::WrapUnsafeBinder(op, _) => in_operand::<Q, _>(cx, in_local, op),
260262

261263
Rvalue::Aggregate(kind, operands) => {
@@ -276,8 +278,6 @@ where
276278
// Otherwise, proceed structurally...
277279
operands.iter().any(|o| in_operand::<Q, _>(cx, in_local, o))
278280
}
279-
280-
Rvalue::Reborrow(_, place) => in_place::<Q, _>(cx, in_local, place.as_ref()),
281281
}
282282
}
283283

compiler/rustc_const_eval/src/check_consts/resolver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ where
191191
}
192192
}
193193

194-
mir::Rvalue::Reborrow(_, borrowed_place) => {
195-
if !borrowed_place.is_indirect() {
194+
mir::Rvalue::Reborrow(mutability, borrowed_place) => {
195+
if !borrowed_place.is_indirect() && mutability.is_mut() {
196196
let place_ty = borrowed_place.ty(self.ccx.body, self.ccx.tcx).ty;
197197
if Q::in_any_value_of_ty(self.ccx, place_ty) {
198198
self.state.qualif.insert(borrowed_place.local);

compiler/rustc_middle/src/mir/statement.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ impl<'tcx> Rvalue<'tcx> {
748748
| Rvalue::CopyForDeref(_)
749749
| Rvalue::Repeat(_, _)
750750
| Rvalue::Ref(_, _, _)
751+
| Rvalue::Reborrow(_, _)
751752
| Rvalue::ThreadLocalRef(_)
752753
| Rvalue::RawPtr(_, _)
753754
| Rvalue::Cast(
@@ -769,8 +770,7 @@ impl<'tcx> Rvalue<'tcx> {
769770
| Rvalue::Discriminant(_)
770771
| Rvalue::Aggregate(_, _)
771772
| Rvalue::ShallowInitBox(_, _)
772-
| Rvalue::WrapUnsafeBinder(_, _)
773-
| Rvalue::Reborrow(..) => true,
773+
| Rvalue::WrapUnsafeBinder(_, _) => true,
774774
}
775775
}
776776

@@ -794,6 +794,10 @@ impl<'tcx> Rvalue<'tcx> {
794794
let place_ty = place.ty(local_decls, tcx).ty;
795795
Ty::new_ref(tcx, reg, place_ty, bk.to_mutbl_lossy())
796796
}
797+
Rvalue::Reborrow(_mutability, ref place) => {
798+
// FIXME(@aapoalas): when mutability is Not, type changes.
799+
place.ty(local_decls, tcx).ty
800+
}
797801
Rvalue::RawPtr(kind, ref place) => {
798802
let place_ty = place.ty(local_decls, tcx).ty;
799803
Ty::new_ptr(tcx, place_ty, kind.to_mutbl_lossy())
@@ -825,7 +829,6 @@ impl<'tcx> Rvalue<'tcx> {
825829
Rvalue::ShallowInitBox(_, ty) => Ty::new_box(tcx, ty),
826830
Rvalue::CopyForDeref(ref place) => place.ty(local_decls, tcx).ty,
827831
Rvalue::WrapUnsafeBinder(_, ty) => ty,
828-
Rvalue::Reborrow(_, ref place) => place.ty(local_decls, tcx).ty,
829832
}
830833
}
831834

0 commit comments

Comments
 (0)