Skip to content

Commit 30c9e19

Browse files
committed
Add a non-shallow fake borrow
1 parent 4719aad commit 30c9e19

32 files changed

+189
-93
lines changed

compiler/rustc_borrowck/src/borrow_set.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ impl<'tcx> fmt::Display for BorrowData<'tcx> {
6969
fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
7070
let kind = match self.kind {
7171
mir::BorrowKind::Shared => "",
72-
mir::BorrowKind::Fake => "fake ",
72+
mir::BorrowKind::Fake(mir::FakeBorrowKind::Deep) => "fake ",
73+
mir::BorrowKind::Fake(mir::FakeBorrowKind::Shallow) => "fake shallow",
7374
mir::BorrowKind::Mut { kind: mir::MutBorrowKind::ClosureCapture } => "uniq ",
7475
// FIXME: differentiate `TwoPhaseBorrow`
7576
mir::BorrowKind::Mut {

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ use rustc_middle::hir::nested_filter::OnlyBodies;
1717
use rustc_middle::mir::tcx::PlaceTy;
1818
use rustc_middle::mir::{
1919
self, AggregateKind, BindingForm, BorrowKind, CallSource, ClearCrossCrate, ConstraintCategory,
20-
FakeReadCause, LocalDecl, LocalInfo, LocalKind, Location, MutBorrowKind, Operand, Place,
21-
PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
22-
VarBindingForm,
20+
FakeBorrowKind, FakeReadCause, LocalDecl, LocalInfo, LocalKind, Location, MutBorrowKind,
21+
Operand, Place, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator,
22+
TerminatorKind, VarBindingForm,
2323
};
2424
use rustc_middle::ty::{
2525
self, suggest_constraining_type_params, PredicateKind, ToPredicate, Ty, TyCtxt,
@@ -1202,7 +1202,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12021202
let first_borrow_desc;
12031203
let mut err = match (gen_borrow_kind, issued_borrow.kind) {
12041204
(
1205-
BorrowKind::Shared,
1205+
BorrowKind::Shared | BorrowKind::Fake(FakeBorrowKind::Deep),
12061206
BorrowKind::Mut { kind: MutBorrowKind::Default | MutBorrowKind::TwoPhaseBorrow },
12071207
) => {
12081208
first_borrow_desc = "mutable ";
@@ -1220,7 +1220,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12201220
}
12211221
(
12221222
BorrowKind::Mut { kind: MutBorrowKind::Default | MutBorrowKind::TwoPhaseBorrow },
1223-
BorrowKind::Shared,
1223+
BorrowKind::Shared | BorrowKind::Fake(FakeBorrowKind::Deep),
12241224
) => {
12251225
first_borrow_desc = "immutable ";
12261226
let mut err = self.cannot_reborrow_already_borrowed(
@@ -1282,7 +1282,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12821282
self.cannot_uniquely_borrow_by_two_closures(span, &desc_place, issued_span, None)
12831283
}
12841284

1285-
(BorrowKind::Mut { .. }, BorrowKind::Fake) => {
1285+
(BorrowKind::Mut { .. }, BorrowKind::Fake(FakeBorrowKind::Shallow)) => {
12861286
if let Some(immutable_section_description) =
12871287
self.classify_immutable_section(issued_borrow.assigned_place)
12881288
{
@@ -1345,7 +1345,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13451345
)
13461346
}
13471347

1348-
(BorrowKind::Shared, BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture }) => {
1348+
(
1349+
BorrowKind::Shared | BorrowKind::Fake(FakeBorrowKind::Deep),
1350+
BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture },
1351+
) => {
13491352
first_borrow_desc = "first ";
13501353
self.cannot_reborrow_already_uniquely_borrowed(
13511354
span,
@@ -1375,8 +1378,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13751378
)
13761379
}
13771380

1378-
(BorrowKind::Shared, BorrowKind::Shared | BorrowKind::Fake)
1379-
| (BorrowKind::Fake, BorrowKind::Mut { .. } | BorrowKind::Shared | BorrowKind::Fake) => {
1381+
(
1382+
BorrowKind::Shared | BorrowKind::Fake(FakeBorrowKind::Deep),
1383+
BorrowKind::Shared | BorrowKind::Fake(_),
1384+
)
1385+
| (
1386+
BorrowKind::Fake(FakeBorrowKind::Shallow),
1387+
BorrowKind::Mut { .. } | BorrowKind::Shared | BorrowKind::Fake(_),
1388+
) => {
13801389
unreachable!()
13811390
}
13821391
};
@@ -3266,7 +3275,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
32663275
let loan_span = loan_spans.args_or_use();
32673276

32683277
let descr_place = self.describe_any_place(place.as_ref());
3269-
if loan.kind == BorrowKind::Fake {
3278+
if let BorrowKind::Fake(_) = loan.kind {
32703279
if let Some(section) = self.classify_immutable_section(loan.assigned_place) {
32713280
let mut err = self.cannot_mutate_in_immutable_section(
32723281
span,

compiler/rustc_borrowck/src/diagnostics/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ impl UseSpans<'_> {
651651
match kind {
652652
Some(kd) => match kd {
653653
rustc_middle::mir::BorrowKind::Shared
654-
| rustc_middle::mir::BorrowKind::Fake => {
654+
| rustc_middle::mir::BorrowKind::Fake(_) => {
655655
CaptureVarKind::Immut { kind_span: capture_kind_span }
656656
}
657657

compiler/rustc_borrowck/src/lib.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -1057,18 +1057,19 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10571057
Control::Continue
10581058
}
10591059

1060-
(Read(_), BorrowKind::Shared | BorrowKind::Fake)
1061-
| (Read(ReadKind::Borrow(BorrowKind::Fake)), BorrowKind::Mut { .. }) => {
1062-
Control::Continue
1063-
}
1060+
(Read(_), BorrowKind::Shared | BorrowKind::Fake(_))
1061+
| (
1062+
Read(ReadKind::Borrow(BorrowKind::Fake(FakeBorrowKind::Shallow))),
1063+
BorrowKind::Mut { .. },
1064+
) => Control::Continue,
10641065

1065-
(Reservation(_), BorrowKind::Fake | BorrowKind::Shared) => {
1066+
(Reservation(_), BorrowKind::Fake(_) | BorrowKind::Shared) => {
10661067
// This used to be a future compatibility warning (to be
10671068
// disallowed on NLL). See rust-lang/rust#56254
10681069
Control::Continue
10691070
}
10701071

1071-
(Write(WriteKind::Move), BorrowKind::Fake) => {
1072+
(Write(WriteKind::Move), BorrowKind::Fake(FakeBorrowKind::Shallow)) => {
10721073
// Handled by initialization checks.
10731074
Control::Continue
10741075
}
@@ -1176,10 +1177,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11761177
match rvalue {
11771178
&Rvalue::Ref(_ /*rgn*/, bk, place) => {
11781179
let access_kind = match bk {
1179-
BorrowKind::Fake => {
1180+
BorrowKind::Fake(FakeBorrowKind::Shallow) => {
11801181
(Shallow(Some(ArtificialField::FakeBorrow)), Read(ReadKind::Borrow(bk)))
11811182
}
1182-
BorrowKind::Shared => (Deep, Read(ReadKind::Borrow(bk))),
1183+
BorrowKind::Shared | BorrowKind::Fake(FakeBorrowKind::Deep) => {
1184+
(Deep, Read(ReadKind::Borrow(bk)))
1185+
}
11831186
BorrowKind::Mut { .. } => {
11841187
let wk = WriteKind::MutableBorrow(bk);
11851188
if allow_two_phase_borrow(bk) {
@@ -1198,7 +1201,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11981201
flow_state,
11991202
);
12001203

1201-
let action = if bk == BorrowKind::Fake {
1204+
let action = if bk == BorrowKind::Fake(FakeBorrowKind::Shallow) {
12021205
InitializationRequiringAction::MatchOn
12031206
} else {
12041207
InitializationRequiringAction::Borrow
@@ -1557,7 +1560,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
15571560

15581561
// only mutable borrows should be 2-phase
15591562
assert!(match borrow.kind {
1560-
BorrowKind::Shared | BorrowKind::Fake => false,
1563+
BorrowKind::Shared | BorrowKind::Fake(_) => false,
15611564
BorrowKind::Mut { .. } => true,
15621565
});
15631566

@@ -2120,14 +2123,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
21202123
| WriteKind::Replace
21212124
| WriteKind::StorageDeadOrDrop
21222125
| WriteKind::MutableBorrow(BorrowKind::Shared)
2123-
| WriteKind::MutableBorrow(BorrowKind::Fake),
2126+
| WriteKind::MutableBorrow(BorrowKind::Fake(_)),
21242127
)
21252128
| Write(
21262129
WriteKind::Move
21272130
| WriteKind::Replace
21282131
| WriteKind::StorageDeadOrDrop
21292132
| WriteKind::MutableBorrow(BorrowKind::Shared)
2130-
| WriteKind::MutableBorrow(BorrowKind::Fake),
2133+
| WriteKind::MutableBorrow(BorrowKind::Fake(_)),
21312134
) => {
21322135
if self.is_mutable(place.as_ref(), is_local_mutation_allowed).is_err()
21332136
&& !self.has_buffered_diags()
@@ -2151,7 +2154,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
21512154
return false;
21522155
}
21532156
Read(
2154-
ReadKind::Borrow(BorrowKind::Mut { .. } | BorrowKind::Shared | BorrowKind::Fake)
2157+
ReadKind::Borrow(BorrowKind::Mut { .. } | BorrowKind::Shared | BorrowKind::Fake(_))
21552158
| ReadKind::Copy,
21562159
) => {
21572160
// Access authorized

compiler/rustc_borrowck/src/places_conflict.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use crate::Overlap;
5555
use crate::{AccessDepth, Deep, Shallow};
5656
use rustc_hir as hir;
5757
use rustc_middle::mir::{
58-
Body, BorrowKind, MutBorrowKind, Place, PlaceElem, PlaceRef, ProjectionElem,
58+
Body, BorrowKind, FakeBorrowKind, MutBorrowKind, Place, PlaceElem, PlaceRef, ProjectionElem,
5959
};
6060
use rustc_middle::ty::{self, TyCtxt};
6161
use std::cmp::max;
@@ -271,10 +271,10 @@ fn place_components_conflict<'tcx>(
271271
// If the second example, where we did, then we still know
272272
// that the borrow can access a *part* of our place that
273273
// our access cares about, so we still have a conflict.
274-
if borrow_kind == BorrowKind::Fake
274+
if borrow_kind == BorrowKind::Fake(FakeBorrowKind::Shallow)
275275
&& borrow_place.projection.len() < access_place.projection.len()
276276
{
277-
debug!("borrow_conflicts_with_place: fake borrow");
277+
debug!("borrow_conflicts_with_place: shallow borrow");
278278
false
279279
} else {
280280
debug!("borrow_conflicts_with_place: full borrow, CONFLICT");

compiler/rustc_borrowck/src/polonius/loan_invalidations.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use rustc_data_structures::graph::dominators::Dominators;
22
use rustc_middle::mir::visit::Visitor;
3-
use rustc_middle::mir::{self, BasicBlock, Body, Location, NonDivergingIntrinsic, Place, Rvalue};
3+
use rustc_middle::mir::{
4+
self, BasicBlock, Body, FakeBorrowKind, Location, NonDivergingIntrinsic, Place, Rvalue,
5+
};
46
use rustc_middle::mir::{BorrowKind, Mutability, Operand};
57
use rustc_middle::mir::{InlineAsmOperand, Terminator, TerminatorKind};
68
use rustc_middle::mir::{Statement, StatementKind};
@@ -239,10 +241,12 @@ impl<'cx, 'tcx> LoanInvalidationsGenerator<'cx, 'tcx> {
239241
match rvalue {
240242
&Rvalue::Ref(_ /*rgn*/, bk, place) => {
241243
let access_kind = match bk {
242-
BorrowKind::Fake => {
244+
BorrowKind::Fake(FakeBorrowKind::Shallow) => {
243245
(Shallow(Some(ArtificialField::FakeBorrow)), Read(ReadKind::Borrow(bk)))
244246
}
245-
BorrowKind::Shared => (Deep, Read(ReadKind::Borrow(bk))),
247+
BorrowKind::Shared | BorrowKind::Fake(FakeBorrowKind::Deep) => {
248+
(Deep, Read(ReadKind::Borrow(bk)))
249+
}
246250
BorrowKind::Mut { .. } => {
247251
let wk = WriteKind::MutableBorrow(bk);
248252
if allow_two_phase_borrow(bk) {
@@ -357,8 +361,11 @@ impl<'cx, 'tcx> LoanInvalidationsGenerator<'cx, 'tcx> {
357361
// have already taken the reservation
358362
}
359363

360-
(Read(_), BorrowKind::Fake | BorrowKind::Shared)
361-
| (Read(ReadKind::Borrow(BorrowKind::Fake)), BorrowKind::Mut { .. }) => {
364+
(Read(_), BorrowKind::Fake(_) | BorrowKind::Shared)
365+
| (
366+
Read(ReadKind::Borrow(BorrowKind::Fake(FakeBorrowKind::Shallow))),
367+
BorrowKind::Mut { .. },
368+
) => {
362369
// Reads don't invalidate shared or shallow borrows
363370
}
364371

@@ -403,7 +410,7 @@ impl<'cx, 'tcx> LoanInvalidationsGenerator<'cx, 'tcx> {
403410

404411
// only mutable borrows should be 2-phase
405412
assert!(match borrow.kind {
406-
BorrowKind::Shared | BorrowKind::Fake => false,
413+
BorrowKind::Shared | BorrowKind::Fake(_) => false,
407414
BorrowKind::Mut { .. } => true,
408415
});
409416

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
409409
BorrowKind::Shared => {
410410
PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow)
411411
}
412-
BorrowKind::Fake => {
412+
BorrowKind::Fake(_) => {
413413
PlaceContext::NonMutatingUse(NonMutatingUseContext::FakeBorrow)
414414
}
415415
BorrowKind::Mut { .. } => {
@@ -482,7 +482,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
482482
}
483483
}
484484

485-
Rvalue::Ref(_, BorrowKind::Shared | BorrowKind::Fake, place)
485+
Rvalue::Ref(_, BorrowKind::Shared | BorrowKind::Fake(_), place)
486486
| Rvalue::AddressOf(Mutability::Not, place) => {
487487
let borrowed_place_has_mut_interior = qualifs::in_place::<HasMutInterior, _>(
488488
self.ccx,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ where
105105
fn ref_allows_mutation(&self, kind: mir::BorrowKind, place: mir::Place<'tcx>) -> bool {
106106
match kind {
107107
mir::BorrowKind::Mut { .. } => true,
108-
mir::BorrowKind::Shared | mir::BorrowKind::Fake => {
108+
mir::BorrowKind::Shared | mir::BorrowKind::Fake(_) => {
109109
self.shared_borrow_allows_mutation(place)
110110
}
111111
}

compiler/rustc_const_eval/src/transform/validate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
924924
}
925925
}
926926
},
927-
Rvalue::Ref(_, BorrowKind::Fake, _) => {
927+
Rvalue::Ref(_, BorrowKind::Fake(_), _) => {
928928
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
929929
self.fail(
930930
location,

compiler/rustc_middle/src/mir/pretty.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,8 @@ impl<'tcx> Debug for Rvalue<'tcx> {
954954
Ref(region, borrow_kind, ref place) => {
955955
let kind_str = match borrow_kind {
956956
BorrowKind::Shared => "",
957-
BorrowKind::Fake => "fake ",
957+
BorrowKind::Fake(FakeBorrowKind::Deep) => "fake ",
958+
BorrowKind::Fake(FakeBorrowKind::Shallow) => "fake shallow",
958959
BorrowKind::Mut { .. } => "mut ",
959960
};
960961

compiler/rustc_middle/src/mir/statement.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -451,15 +451,15 @@ impl<'tcx> Rvalue<'tcx> {
451451
impl BorrowKind {
452452
pub fn mutability(&self) -> Mutability {
453453
match *self {
454-
BorrowKind::Shared | BorrowKind::Fake => Mutability::Not,
454+
BorrowKind::Shared | BorrowKind::Fake(_) => Mutability::Not,
455455
BorrowKind::Mut { .. } => Mutability::Mut,
456456
}
457457
}
458458

459459
pub fn allows_two_phase_borrow(&self) -> bool {
460460
match *self {
461461
BorrowKind::Shared
462-
| BorrowKind::Fake
462+
| BorrowKind::Fake(_)
463463
| BorrowKind::Mut { kind: MutBorrowKind::Default | MutBorrowKind::ClosureCapture } => {
464464
false
465465
}

0 commit comments

Comments
 (0)