Skip to content

Commit 8fb4c41

Browse files
committed
merge BorrowKind::Unique into BorrowKind::Mut
1 parent 6fc0273 commit 8fb4c41

File tree

21 files changed

+109
-105
lines changed

21 files changed

+109
-105
lines changed

compiler/rustc_borrowck/src/borrow_set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ 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 ",
75+
mir::BorrowKind::Mut { kind: mir::MutBorrowKind::ClosureCapture } => "uniq ",
7676
mir::BorrowKind::Mut { .. } => "mut ",
7777
};
7878
write!(w, "&{:?} {}{:?}", self.region, kind, self.borrowed_place)

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,9 @@ impl UseSpans<'_> {
629629
Some(kd) => match kd {
630630
rustc_middle::mir::BorrowKind::Shared
631631
| rustc_middle::mir::BorrowKind::Shallow
632-
| rustc_middle::mir::BorrowKind::Unique => {
633-
CaptureVarKind::Immut { kind_span: capture_kind_span }
634-
}
632+
| rustc_middle::mir::BorrowKind::Mut {
633+
kind: rustc_middle::mir::MutBorrowKind::ClosureCapture,
634+
} => CaptureVarKind::Immut { kind_span: capture_kind_span },
635635

636636
rustc_middle::mir::BorrowKind::Mut { .. } => {
637637
CaptureVarKind::Mut { kind_span: capture_kind_span }

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

+15-23
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,14 +1958,12 @@ 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-
)) => {
1961+
Reservation(WriteKind::MutableBorrow(borrow_kind @ BorrowKind::Mut { .. }))
1962+
| Write(WriteKind::MutableBorrow(borrow_kind @ BorrowKind::Mut { .. })) => {
19681963
let is_local_mutation_allowed = match borrow_kind {
1969-
BorrowKind::Unique => LocalMutationIsAllowed::Yes,
1964+
BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture } => {
1965+
LocalMutationIsAllowed::Yes
1966+
}
19701967
BorrowKind::Mut { .. } => is_local_mutation_allowed,
19711968
BorrowKind::Shared | BorrowKind::Shallow => unreachable!(),
19721969
};
@@ -2031,12 +2028,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
20312028
return false;
20322029
}
20332030
Read(
2034-
ReadKind::Borrow(
2035-
BorrowKind::Unique
2036-
| BorrowKind::Mut { .. }
2037-
| BorrowKind::Shared
2038-
| BorrowKind::Shallow,
2039-
)
2031+
ReadKind::Borrow(BorrowKind::Mut { .. } | BorrowKind::Shared | BorrowKind::Shallow)
20402032
| ReadKind::Copy,
20412033
) => {
20422034
// 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

+1-2
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(_, kind @ 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.

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

+3-1
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);

compiler/rustc_middle/src/mir/mod.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -2035,22 +2035,24 @@ impl<'tcx> Rvalue<'tcx> {
20352035
impl BorrowKind {
20362036
pub fn mutability(&self) -> Mutability {
20372037
match *self {
2038-
BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => Mutability::Not,
2038+
BorrowKind::Shared | BorrowKind::Shallow => Mutability::Not,
20392039
BorrowKind::Mut { .. } => Mutability::Mut,
20402040
}
20412041
}
20422042

20432043
pub fn allows_two_phase_borrow(&self) -> bool {
20442044
match *self {
2045-
BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => false,
2046-
BorrowKind::Mut { allow_two_phase_borrow } => allow_two_phase_borrow,
2045+
BorrowKind::Shared | BorrowKind::Shallow => false,
2046+
BorrowKind::Mut { kind } => kind == MutBorrowKind::TwoPhaseBorrow,
20472047
}
20482048
}
20492049

20502050
// FIXME: won't be used after diagnostic migration
20512051
pub fn describe_mutability(&self) -> &str {
20522052
match *self {
2053-
BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => "immutable",
2053+
BorrowKind::Shared
2054+
| BorrowKind::Shallow
2055+
| BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture } => "immutable",
20542056
BorrowKind::Mut { .. } => "mutable",
20552057
}
20562058
}
@@ -2090,7 +2092,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
20902092
let kind_str = match borrow_kind {
20912093
BorrowKind::Shared => "",
20922094
BorrowKind::Shallow => "shallow ",
2093-
BorrowKind::Mut { .. } | BorrowKind::Unique => "mut ",
2095+
BorrowKind::Mut { .. } => "mut ",
20942096
};
20952097

20962098
// When printing regions, add trailing space if necessary.

0 commit comments

Comments
 (0)