Skip to content

Commit a52cc0a

Browse files
committed
address most easy comments
1 parent 8fb4c41 commit a52cc0a

File tree

8 files changed

+33
-42
lines changed

8 files changed

+33
-42
lines changed

compiler/rustc_borrowck/src/borrow_set.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ impl<'tcx> fmt::Display for BorrowData<'tcx> {
7373
mir::BorrowKind::Shared => "",
7474
mir::BorrowKind::Shallow => "shallow ",
7575
mir::BorrowKind::Mut { kind: mir::MutBorrowKind::ClosureCapture } => "uniq ",
76-
mir::BorrowKind::Mut { .. } => "mut ",
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/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -628,10 +628,9 @@ 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::Mut {
633-
kind: rustc_middle::mir::MutBorrowKind::ClosureCapture,
634-
} => CaptureVarKind::Immut { kind_span: capture_kind_span },
631+
| rustc_middle::mir::BorrowKind::Shallow => {
632+
CaptureVarKind::Immut { kind_span: capture_kind_span }
633+
}
635634

636635
rustc_middle::mir::BorrowKind::Mut { .. } => {
637636
CaptureVarKind::Mut { kind_span: capture_kind_span }

compiler/rustc_borrowck/src/lib.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -1958,14 +1958,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
19581958
let the_place_err;
19591959

19601960
match kind {
1961-
Reservation(WriteKind::MutableBorrow(borrow_kind @ BorrowKind::Mut { .. }))
1962-
| Write(WriteKind::MutableBorrow(borrow_kind @ BorrowKind::Mut { .. })) => {
1963-
let is_local_mutation_allowed = match borrow_kind {
1964-
BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture } => {
1965-
LocalMutationIsAllowed::Yes
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
19661969
}
1967-
BorrowKind::Mut { .. } => is_local_mutation_allowed,
1968-
BorrowKind::Shared | BorrowKind::Shallow => unreachable!(),
19691970
};
19701971
match self.is_mutable(place.as_ref(), is_local_mutation_allowed) {
19711972
Ok(root_place) => {

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
456456
}
457457
}
458458

459-
Rvalue::Ref(_, kind @ BorrowKind::Mut { .. }, place) => {
459+
Rvalue::Ref(_, BorrowKind::Mut { .. }, place) => {
460460
let ty = place.ty(self.body, self.tcx).ty;
461461
let is_allowed = match ty.kind() {
462462
// Inside a `static mut`, `&mut [...]` is allowed.
@@ -477,11 +477,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
477477
};
478478

479479
if !is_allowed {
480-
if let BorrowKind::Mut { .. } = kind {
481-
self.check_mut_borrow(place.local, hir::BorrowKind::Ref)
482-
} else {
483-
self.check_op(ops::CellBorrow);
484-
}
480+
self.check_mut_borrow(place.local, hir::BorrowKind::Ref)
485481
}
486482
}
487483

compiler/rustc_const_eval/src/transform/promote_consts.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,9 @@ impl<'tcx> Validator<'_, 'tcx> {
465465
}
466466
}
467467

468-
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 } => {
469471
let ty = place.ty(self.body, self.tcx).ty;
470472

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

compiler/rustc_middle/src/mir/mod.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -2041,19 +2041,13 @@ impl BorrowKind {
20412041
}
20422042

20432043
pub fn allows_two_phase_borrow(&self) -> bool {
2044-
match *self {
2045-
BorrowKind::Shared | BorrowKind::Shallow => false,
2046-
BorrowKind::Mut { kind } => kind == MutBorrowKind::TwoPhaseBorrow,
2047-
}
2048-
}
2049-
2050-
// FIXME: won't be used after diagnostic migration
2051-
pub fn describe_mutability(&self) -> &str {
20522044
match *self {
20532045
BorrowKind::Shared
20542046
| BorrowKind::Shallow
2055-
| BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture } => "immutable",
2056-
BorrowKind::Mut { .. } => "mutable",
2047+
| BorrowKind::Mut { kind: MutBorrowKind::Default | MutBorrowKind::ClosureCapture } => {
2048+
false
2049+
}
2050+
BorrowKind::Mut { kind: MutBorrowKind::TwoPhaseBorrow } => true,
20572051
}
20582052
}
20592053
}

compiler/rustc_middle/src/ty/closure.rs

+2
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ pub enum BorrowKind {
427427
/// immutable, but not aliasable. This solves the problem. For
428428
/// simplicity, we don't give users the way to express this
429429
/// borrow, it's just used when translating closures.
430+
///
431+
/// FIXME: Rename this to indicate the borrow is actually not immutable.
430432
UniqueImmBorrow,
431433

432434
/// Data is mutable and not aliasable.

compiler/rustc_mir_build/src/check_unsafety.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::errors::*;
33
use rustc_middle::thir::visit::{self, Visitor};
44

55
use rustc_hir as hir;
6-
use rustc_middle::mir::{BorrowKind, MutBorrowKind};
6+
use rustc_middle::mir::BorrowKind;
77
use rustc_middle::thir::*;
88
use rustc_middle::ty::print::with_no_trimmed_paths;
99
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};
@@ -254,9 +254,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
254254
);
255255
};
256256
match borrow_kind {
257-
BorrowKind::Shallow
258-
| BorrowKind::Shared
259-
| BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture } => {
257+
BorrowKind::Shallow | BorrowKind::Shared => {
260258
if !ty.is_freeze(self.tcx, self.param_env) {
261259
self.requires_unsafe(pat.span, BorrowOfLayoutConstrainedField);
262260
}
@@ -442,19 +440,15 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
442440
visit::walk_expr(&mut visitor, expr);
443441
if visitor.found {
444442
match borrow_kind {
445-
BorrowKind::Shallow
446-
| BorrowKind::Shared
447-
| BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture }
443+
BorrowKind::Shallow | BorrowKind::Shared
448444
if !self.thir[arg].ty.is_freeze(self.tcx, self.param_env) =>
449445
{
450446
self.requires_unsafe(expr.span, BorrowOfLayoutConstrainedField)
451447
}
452-
BorrowKind::Mut {
453-
kind: MutBorrowKind::Default | MutBorrowKind::TwoPhaseBorrow,
454-
} => self.requires_unsafe(expr.span, MutationOfLayoutConstrainedField),
455-
BorrowKind::Shallow
456-
| BorrowKind::Shared
457-
| BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture } => {}
448+
BorrowKind::Mut { .. } => {
449+
self.requires_unsafe(expr.span, MutationOfLayoutConstrainedField)
450+
}
451+
BorrowKind::Shallow | BorrowKind::Shared => {}
458452
}
459453
}
460454
}

0 commit comments

Comments
 (0)