Skip to content

Commit c55d1ee

Browse files
committed
Auto merge of #112119 - zirconium-n:issue-113072-merge-borrow-kind, r=lcnr
Merge `BorrowKind::Unique` into `BorrowKind::Mut` Fixes #112072 Might have conflict with #112070 r? `@lcnr` I'm not sure what's the suitable change in a couple places.
2 parents 4457658 + b8a250f commit c55d1ee

File tree

22 files changed

+118
-120
lines changed

22 files changed

+118
-120
lines changed

compiler/rustc_borrowck/src/borrow_set.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ impl<'tcx> fmt::Display for BorrowData<'tcx> {
7272
let kind = match self.kind {
7373
mir::BorrowKind::Shared => "",
7474
mir::BorrowKind::Shallow => "shallow ",
75-
mir::BorrowKind::Unique => "uniq ",
76-
mir::BorrowKind::Mut { .. } => "mut ",
75+
mir::BorrowKind::Mut { kind: mir::MutBorrowKind::ClosureCapture } => "uniq ",
76+
// FIXME: differentiate `TwoPhaseBorrow`
77+
mir::BorrowKind::Mut {
78+
kind: mir::MutBorrowKind::Default | mir::MutBorrowKind::TwoPhaseBorrow,
79+
} => "mut ",
7780
};
7881
write!(w, "&{:?} {}{:?}", self.region, kind, self.borrowed_place)
7982
}

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+25-15
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ use rustc_middle::hir::nested_filter::OnlyBodies;
1515
use rustc_middle::mir::tcx::PlaceTy;
1616
use rustc_middle::mir::{
1717
self, AggregateKind, BindingForm, BorrowKind, CallSource, ClearCrossCrate, ConstraintCategory,
18-
FakeReadCause, LocalDecl, LocalInfo, LocalKind, Location, Operand, Place, PlaceRef,
19-
ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind, VarBindingForm,
18+
FakeReadCause, LocalDecl, LocalInfo, LocalKind, Location, MutBorrowKind, Operand, Place,
19+
PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
20+
VarBindingForm,
2021
};
2122
use rustc_middle::ty::{self, suggest_constraining_type_params, PredicateKind, Ty};
2223
use rustc_middle::util::CallKind;
@@ -926,7 +927,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
926927
// FIXME: supply non-"" `opt_via` when appropriate
927928
let first_borrow_desc;
928929
let mut err = match (gen_borrow_kind, issued_borrow.kind) {
929-
(BorrowKind::Shared, BorrowKind::Mut { .. }) => {
930+
(
931+
BorrowKind::Shared,
932+
BorrowKind::Mut { kind: MutBorrowKind::Default | MutBorrowKind::TwoPhaseBorrow },
933+
) => {
930934
first_borrow_desc = "mutable ";
931935
self.cannot_reborrow_already_borrowed(
932936
span,
@@ -940,7 +944,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
940944
None,
941945
)
942946
}
943-
(BorrowKind::Mut { .. }, BorrowKind::Shared) => {
947+
(
948+
BorrowKind::Mut { kind: MutBorrowKind::Default | MutBorrowKind::TwoPhaseBorrow },
949+
BorrowKind::Shared,
950+
) => {
944951
first_borrow_desc = "immutable ";
945952
let mut err = self.cannot_reborrow_already_borrowed(
946953
span,
@@ -962,7 +969,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
962969
err
963970
}
964971

965-
(BorrowKind::Mut { .. }, BorrowKind::Mut { .. }) => {
972+
(
973+
BorrowKind::Mut { kind: MutBorrowKind::Default | MutBorrowKind::TwoPhaseBorrow },
974+
BorrowKind::Mut { kind: MutBorrowKind::Default | MutBorrowKind::TwoPhaseBorrow },
975+
) => {
966976
first_borrow_desc = "first ";
967977
let mut err = self.cannot_mutably_borrow_multiply(
968978
span,
@@ -985,12 +995,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
985995
err
986996
}
987997

988-
(BorrowKind::Unique, BorrowKind::Unique) => {
998+
(
999+
BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture },
1000+
BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture },
1001+
) => {
9891002
first_borrow_desc = "first ";
9901003
self.cannot_uniquely_borrow_by_two_closures(span, &desc_place, issued_span, None)
9911004
}
9921005

993-
(BorrowKind::Mut { .. } | BorrowKind::Unique, BorrowKind::Shallow) => {
1006+
(BorrowKind::Mut { .. }, BorrowKind::Shallow) => {
9941007
if let Some(immutable_section_description) =
9951008
self.classify_immutable_section(issued_borrow.assigned_place)
9961009
{
@@ -1004,7 +1017,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10041017
borrow_spans.var_subdiag(
10051018
None,
10061019
&mut err,
1007-
Some(BorrowKind::Unique),
1020+
Some(BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture }),
10081021
|kind, var_span| {
10091022
use crate::session_diagnostics::CaptureVarCause::*;
10101023
match kind {
@@ -1038,7 +1051,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10381051
}
10391052
}
10401053

1041-
(BorrowKind::Unique, _) => {
1054+
(BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture }, _) => {
10421055
first_borrow_desc = "first ";
10431056
self.cannot_uniquely_borrow_by_one_closure(
10441057
span,
@@ -1052,7 +1065,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10521065
)
10531066
}
10541067

1055-
(BorrowKind::Shared, BorrowKind::Unique) => {
1068+
(BorrowKind::Shared, BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture }) => {
10561069
first_borrow_desc = "first ";
10571070
self.cannot_reborrow_already_uniquely_borrowed(
10581071
span,
@@ -1067,7 +1080,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10671080
)
10681081
}
10691082

1070-
(BorrowKind::Mut { .. }, BorrowKind::Unique) => {
1083+
(BorrowKind::Mut { .. }, BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture }) => {
10711084
first_borrow_desc = "first ";
10721085
self.cannot_reborrow_already_uniquely_borrowed(
10731086
span,
@@ -1085,10 +1098,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10851098
(BorrowKind::Shared, BorrowKind::Shared | BorrowKind::Shallow)
10861099
| (
10871100
BorrowKind::Shallow,
1088-
BorrowKind::Mut { .. }
1089-
| BorrowKind::Unique
1090-
| BorrowKind::Shared
1091-
| BorrowKind::Shallow,
1101+
BorrowKind::Mut { .. } | BorrowKind::Shared | BorrowKind::Shallow,
10921102
) => unreachable!(),
10931103
};
10941104

compiler/rustc_borrowck/src/diagnostics/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,7 @@ impl UseSpans<'_> {
628628
err.subdiagnostic(match kind {
629629
Some(kd) => match kd {
630630
rustc_middle::mir::BorrowKind::Shared
631-
| rustc_middle::mir::BorrowKind::Shallow
632-
| rustc_middle::mir::BorrowKind::Unique => {
631+
| rustc_middle::mir::BorrowKind::Shallow => {
633632
CaptureVarKind::Immut { kind_span: capture_kind_span }
634633
}
635634

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
234234
borrow_spans.var_subdiag(
235235
None,
236236
&mut err,
237-
Some(mir::BorrowKind::Mut { allow_two_phase_borrow: false }),
237+
Some(mir::BorrowKind::Mut { kind: mir::MutBorrowKind::Default }),
238238
|_kind, var_span| {
239239
let place = self.describe_any_place(access_place.as_ref());
240240
crate::session_diagnostics::CaptureVarCause::MutableBorrowUsePlaceClosure {
@@ -300,7 +300,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
300300
_,
301301
mir::Rvalue::Ref(
302302
_,
303-
mir::BorrowKind::Mut { allow_two_phase_borrow: false },
303+
mir::BorrowKind::Mut { kind: mir::MutBorrowKind::Default },
304304
_,
305305
),
306306
)),

compiler/rustc_borrowck/src/invalidation.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl<'cx, 'tcx> InvalidationGenerator<'cx, 'tcx> {
255255
(Shallow(Some(ArtificialField::ShallowBorrow)), Read(ReadKind::Borrow(bk)))
256256
}
257257
BorrowKind::Shared => (Deep, Read(ReadKind::Borrow(bk))),
258-
BorrowKind::Unique | BorrowKind::Mut { .. } => {
258+
BorrowKind::Mut { .. } => {
259259
let wk = WriteKind::MutableBorrow(bk);
260260
if allow_two_phase_borrow(bk) {
261261
(Deep, Reservation(wk))
@@ -273,7 +273,7 @@ impl<'cx, 'tcx> InvalidationGenerator<'cx, 'tcx> {
273273
Mutability::Mut => (
274274
Deep,
275275
Write(WriteKind::MutableBorrow(BorrowKind::Mut {
276-
allow_two_phase_borrow: false,
276+
kind: mir::MutBorrowKind::Default,
277277
})),
278278
),
279279
Mutability::Not => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
@@ -376,14 +376,11 @@ impl<'cx, 'tcx> InvalidationGenerator<'cx, 'tcx> {
376376
}
377377

378378
(Read(_), BorrowKind::Shallow | BorrowKind::Shared)
379-
| (
380-
Read(ReadKind::Borrow(BorrowKind::Shallow)),
381-
BorrowKind::Unique | BorrowKind::Mut { .. },
382-
) => {
379+
| (Read(ReadKind::Borrow(BorrowKind::Shallow)), BorrowKind::Mut { .. }) => {
383380
// Reads don't invalidate shared or shallow borrows
384381
}
385382

386-
(Read(_), BorrowKind::Unique | BorrowKind::Mut { .. }) => {
383+
(Read(_), BorrowKind::Mut { .. }) => {
387384
// Reading from mere reservations of mutable-borrows is OK.
388385
if !is_active(&this.dominators, borrow, location) {
389386
// If the borrow isn't active yet, reads don't invalidate it
@@ -425,7 +422,7 @@ impl<'cx, 'tcx> InvalidationGenerator<'cx, 'tcx> {
425422
// only mutable borrows should be 2-phase
426423
assert!(match borrow.kind {
427424
BorrowKind::Shared | BorrowKind::Shallow => false,
428-
BorrowKind::Unique | BorrowKind::Mut { .. } => true,
425+
BorrowKind::Mut { .. } => true,
429426
});
430427

431428
self.access_place(

compiler/rustc_borrowck/src/lib.rs

+19-26
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use rustc_infer::infer::{
2929
InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin, TyCtxtInferExt,
3030
};
3131
use rustc_middle::mir::{
32-
traversal, Body, ClearCrossCrate, Local, Location, Mutability, NonDivergingIntrinsic, Operand,
33-
Place, PlaceElem, PlaceRef, VarDebugInfoContents,
32+
traversal, Body, ClearCrossCrate, Local, Location, MutBorrowKind, Mutability,
33+
NonDivergingIntrinsic, Operand, Place, PlaceElem, PlaceRef, VarDebugInfoContents,
3434
};
3535
use rustc_middle::mir::{AggregateKind, BasicBlock, BorrowCheckResult, BorrowKind};
3636
use rustc_middle::mir::{InlineAsmOperand, Terminator, TerminatorKind};
@@ -1071,10 +1071,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10711071
}
10721072

10731073
(Read(_), BorrowKind::Shared | BorrowKind::Shallow)
1074-
| (
1075-
Read(ReadKind::Borrow(BorrowKind::Shallow)),
1076-
BorrowKind::Unique | BorrowKind::Mut { .. },
1077-
) => Control::Continue,
1074+
| (Read(ReadKind::Borrow(BorrowKind::Shallow)), BorrowKind::Mut { .. }) => {
1075+
Control::Continue
1076+
}
10781077

10791078
(Reservation(_), BorrowKind::Shallow | BorrowKind::Shared) => {
10801079
// This used to be a future compatibility warning (to be
@@ -1087,7 +1086,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10871086
Control::Continue
10881087
}
10891088

1090-
(Read(kind), BorrowKind::Unique | BorrowKind::Mut { .. }) => {
1089+
(Read(kind), BorrowKind::Mut { .. }) => {
10911090
// Reading from mere reservations of mutable-borrows is OK.
10921091
if !is_active(this.dominators(), borrow, location) {
10931092
assert!(allow_two_phase_borrow(borrow.kind));
@@ -1194,7 +1193,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11941193
(Shallow(Some(ArtificialField::ShallowBorrow)), Read(ReadKind::Borrow(bk)))
11951194
}
11961195
BorrowKind::Shared => (Deep, Read(ReadKind::Borrow(bk))),
1197-
BorrowKind::Unique | BorrowKind::Mut { .. } => {
1196+
BorrowKind::Mut { .. } => {
11981197
let wk = WriteKind::MutableBorrow(bk);
11991198
if allow_two_phase_borrow(bk) {
12001199
(Deep, Reservation(wk))
@@ -1231,7 +1230,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12311230
Mutability::Mut => (
12321231
Deep,
12331232
Write(WriteKind::MutableBorrow(BorrowKind::Mut {
1234-
allow_two_phase_borrow: false,
1233+
kind: MutBorrowKind::Default,
12351234
})),
12361235
),
12371236
Mutability::Not => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
@@ -1565,7 +1564,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
15651564
// only mutable borrows should be 2-phase
15661565
assert!(match borrow.kind {
15671566
BorrowKind::Shared | BorrowKind::Shallow => false,
1568-
BorrowKind::Unique | BorrowKind::Mut { .. } => true,
1567+
BorrowKind::Mut { .. } => true,
15691568
});
15701569

15711570
self.access_place(
@@ -1959,16 +1958,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
19591958
let the_place_err;
19601959

19611960
match kind {
1962-
Reservation(WriteKind::MutableBorrow(
1963-
borrow_kind @ (BorrowKind::Unique | BorrowKind::Mut { .. }),
1964-
))
1965-
| Write(WriteKind::MutableBorrow(
1966-
borrow_kind @ (BorrowKind::Unique | BorrowKind::Mut { .. }),
1967-
)) => {
1968-
let is_local_mutation_allowed = match borrow_kind {
1969-
BorrowKind::Unique => LocalMutationIsAllowed::Yes,
1970-
BorrowKind::Mut { .. } => is_local_mutation_allowed,
1971-
BorrowKind::Shared | BorrowKind::Shallow => unreachable!(),
1961+
Reservation(WriteKind::MutableBorrow(BorrowKind::Mut { kind: mut_borrow_kind }))
1962+
| Write(WriteKind::MutableBorrow(BorrowKind::Mut { kind: mut_borrow_kind })) => {
1963+
let is_local_mutation_allowed = match mut_borrow_kind {
1964+
// `ClosureCapture` is used for mutable variable with a immutable binding.
1965+
// This is only behaviour difference between `ClosureCapture` and mutable borrows.
1966+
MutBorrowKind::ClosureCapture => LocalMutationIsAllowed::Yes,
1967+
MutBorrowKind::Default | MutBorrowKind::TwoPhaseBorrow => {
1968+
is_local_mutation_allowed
1969+
}
19721970
};
19731971
match self.is_mutable(place.as_ref(), is_local_mutation_allowed) {
19741972
Ok(root_place) => {
@@ -2031,12 +2029,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
20312029
return false;
20322030
}
20332031
Read(
2034-
ReadKind::Borrow(
2035-
BorrowKind::Unique
2036-
| BorrowKind::Mut { .. }
2037-
| BorrowKind::Shared
2038-
| BorrowKind::Shallow,
2039-
)
2032+
ReadKind::Borrow(BorrowKind::Mut { .. } | BorrowKind::Shared | BorrowKind::Shallow)
20402033
| ReadKind::Copy,
20412034
) => {
20422035
// Access authorized

compiler/rustc_borrowck/src/places_conflict.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use crate::ArtificialField;
44
use crate::Overlap;
55
use crate::{AccessDepth, Deep, Shallow};
66
use rustc_hir as hir;
7-
use rustc_middle::mir::{Body, BorrowKind, Local, Place, PlaceElem, PlaceRef, ProjectionElem};
7+
use rustc_middle::mir::{
8+
Body, BorrowKind, Local, MutBorrowKind, Place, PlaceElem, PlaceRef, ProjectionElem,
9+
};
810
use rustc_middle::ty::{self, TyCtxt};
911
use std::cmp::max;
1012
use std::iter;
@@ -35,7 +37,7 @@ pub fn places_conflict<'tcx>(
3537
tcx,
3638
body,
3739
borrow_place,
38-
BorrowKind::Mut { allow_two_phase_borrow: true },
40+
BorrowKind::Mut { kind: MutBorrowKind::TwoPhaseBorrow },
3941
access_place.as_ref(),
4042
AccessDepth::Deep,
4143
bias,

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
412412
BorrowKind::Shallow => {
413413
PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow)
414414
}
415-
BorrowKind::Unique => PlaceContext::MutatingUse(MutatingUseContext::Borrow),
416415
BorrowKind::Mut { .. } => {
417416
PlaceContext::MutatingUse(MutatingUseContext::Borrow)
418417
}
@@ -457,7 +456,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
457456
}
458457
}
459458

460-
Rvalue::Ref(_, kind @ (BorrowKind::Mut { .. } | BorrowKind::Unique), place) => {
459+
Rvalue::Ref(_, BorrowKind::Mut { .. }, place) => {
461460
let ty = place.ty(self.body, self.tcx).ty;
462461
let is_allowed = match ty.kind() {
463462
// Inside a `static mut`, `&mut [...]` is allowed.
@@ -478,11 +477,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
478477
};
479478

480479
if !is_allowed {
481-
if let BorrowKind::Mut { .. } = kind {
482-
self.check_mut_borrow(place.local, hir::BorrowKind::Ref)
483-
} else {
484-
self.check_op(ops::CellBorrow);
485-
}
480+
self.check_mut_borrow(place.local, hir::BorrowKind::Ref)
486481
}
487482
}
488483

compiler/rustc_const_eval/src/transform/check_consts/resolver.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ where
103103
fn ref_allows_mutation(&self, kind: mir::BorrowKind, place: mir::Place<'tcx>) -> bool {
104104
match kind {
105105
mir::BorrowKind::Mut { .. } => true,
106-
mir::BorrowKind::Shared | mir::BorrowKind::Shallow | mir::BorrowKind::Unique => {
106+
mir::BorrowKind::Shared | mir::BorrowKind::Shallow => {
107107
self.shared_borrow_allows_mutation(place)
108108
}
109109
}

compiler/rustc_const_eval/src/transform/promote_consts.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,9 @@ impl<'tcx> Validator<'_, 'tcx> {
454454
match kind {
455455
// Reject these borrow types just to be safe.
456456
// FIXME(RalfJung): could we allow them? Should we? No point in it until we have a usecase.
457-
BorrowKind::Shallow | BorrowKind::Unique => return Err(Unpromotable),
457+
BorrowKind::Shallow | BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture } => {
458+
return Err(Unpromotable);
459+
}
458460

459461
BorrowKind::Shared => {
460462
let has_mut_interior = self.qualif_local::<qualifs::HasMutInterior>(place.local);
@@ -463,7 +465,9 @@ impl<'tcx> Validator<'_, 'tcx> {
463465
}
464466
}
465467

466-
BorrowKind::Mut { .. } => {
468+
// FIXME: consider changing this to only promote &mut [] for default borrows,
469+
// also forbidding two phase borrows
470+
BorrowKind::Mut { kind: MutBorrowKind::Default | MutBorrowKind::TwoPhaseBorrow } => {
467471
let ty = place.ty(self.body, self.tcx).ty;
468472

469473
// In theory, any zero-sized value could be borrowed

0 commit comments

Comments
 (0)