Skip to content

Commit ed636c5

Browse files
committed
remove is_unique
1 parent b64ddec commit ed636c5

File tree

1 file changed

+15
-74
lines changed
  • src/librustc_mir/borrow_check

1 file changed

+15
-74
lines changed

src/librustc_mir/borrow_check/mod.rs

+15-74
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
12511251
let mut error_reported = false;
12521252
match kind {
12531253
Write(WriteKind::MutableBorrow(BorrowKind::Unique)) => {
1254-
if let Err(_place_err) = self.is_unique(place) {
1254+
if let Err(_place_err) = self.is_mutable(place, LocalMutationIsAllowed::Yes) {
12551255
span_bug!(span, "&unique borrow for {:?} should not fail", place);
12561256
}
12571257
}
@@ -1358,25 +1358,24 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
13581358
// Mutably borrowed data is mutable, but only if we have a
13591359
// unique path to the `&mut`
13601360
hir::MutMutable => {
1361-
match self.is_upvar_field_projection(&proj.base) {
1361+
let mode = match self.is_upvar_field_projection(&proj.base) {
13621362
Some(field) if {
13631363
self.mir.upvar_decls[field.index()].by_ref
1364-
} => {
1365-
self.is_mutable(&proj.base,
1366-
is_local_mutation_allowed)
1367-
}
1368-
_ => self.is_unique(&proj.base)
1369-
}
1364+
} => is_local_mutation_allowed,
1365+
_ => LocalMutationIsAllowed::Yes
1366+
};
1367+
1368+
self.is_mutable(&proj.base, mode)
13701369
}
13711370
}
13721371
}
13731372
ty::TyRawPtr(tnm) => {
13741373
match tnm.mutbl {
13751374
// `*const` raw pointers are not mutable
1376-
hir::MutImmutable => Err(place),
1375+
hir::MutImmutable => return Err(place),
13771376
// `*mut` raw pointers are always mutable, regardless of context
13781377
// The users have to check by themselve.
1379-
hir::MutMutable => Ok(()),
1378+
hir::MutMutable => return Ok(()),
13801379
}
13811380
}
13821381
// `Box<T>` owns its content, so mutable if its location is mutable
@@ -1394,80 +1393,22 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
13941393
ProjectionElem::ConstantIndex { .. } |
13951394
ProjectionElem::Subslice { .. } |
13961395
ProjectionElem::Downcast(..) => {
1397-
let field_projection = self.is_upvar_field_projection(place);
1398-
1399-
if let Some(field) = field_projection {
1396+
if let Some(field) = self.is_upvar_field_projection(place) {
14001397
let decl = &self.mir.upvar_decls[field.index()];
14011398
debug!("decl.mutability={:?} local_mutation_is_allowed={:?} place={:?}",
14021399
decl, is_local_mutation_allowed, place);
1403-
return match (decl.mutability, is_local_mutation_allowed) {
1400+
match (decl.mutability, is_local_mutation_allowed) {
14041401
(Mutability::Not, LocalMutationIsAllowed::No) |
14051402
(Mutability::Not, LocalMutationIsAllowed::ExceptUpvars)
14061403
=> Err(place),
14071404
(Mutability::Not, LocalMutationIsAllowed::Yes) |
1408-
(Mutability::Mut, _) => self.is_unique(&proj.base),
1409-
};
1410-
}
1411-
1412-
self.is_mutable(&proj.base, is_local_mutation_allowed)
1413-
}
1414-
}
1415-
}
1416-
}
1417-
}
1418-
1419-
/// Does this place have a unique path
1420-
fn is_unique<'d>(&self, place: &'d Place<'tcx>) -> Result<(), &'d Place<'tcx>> {
1421-
match *place {
1422-
Place::Local(..) => {
1423-
// Local variables are unique
1424-
Ok(())
1425-
}
1426-
Place::Static(ref static_) => {
1427-
if !self.tcx.is_static_mut(static_.def_id) {
1428-
Err(place)
1429-
} else {
1430-
Ok(())
1431-
}
1432-
}
1433-
Place::Projection(ref proj) => {
1434-
match proj.elem {
1435-
ProjectionElem::Deref => {
1436-
let base_ty = proj.base.ty(self.mir, self.tcx).to_ty(self.tcx);
1437-
1438-
// `Box<T>` referent is unique if box is a unique spot
1439-
if base_ty.is_box() {
1440-
return self.is_unique(&proj.base);
1441-
}
1442-
1443-
// Otherwise we check the kind of deref to decide
1444-
match base_ty.sty {
1445-
ty::TyRef(_, tnm) => {
1446-
match tnm.mutbl {
1447-
// place represent an aliased location
1448-
hir::MutImmutable => Err(place),
1449-
// `&mut T` is as unique as the context in which it is found
1450-
hir::MutMutable => self.is_unique(&proj.base),
1451-
}
1452-
}
1453-
ty::TyRawPtr(tnm) => {
1454-
match tnm.mutbl {
1455-
// `*mut` can be aliased, but we leave it to user
1456-
hir::MutMutable => Ok(()),
1457-
// `*const` is treated the same as `*mut`
1458-
hir::MutImmutable => Ok(()),
1459-
}
1405+
(Mutability::Mut, _) =>
1406+
self.is_mutable(&proj.base, is_local_mutation_allowed)
14601407
}
1461-
// Deref should only be for reference, pointers or boxes
1462-
_ => bug!("Deref of unexpected type: {:?}", base_ty),
1408+
} else {
1409+
self.is_mutable(&proj.base, is_local_mutation_allowed)
14631410
}
14641411
}
1465-
// Other projections are unique if the base is unique
1466-
ProjectionElem::Field(..) |
1467-
ProjectionElem::Index(..) |
1468-
ProjectionElem::ConstantIndex { .. } |
1469-
ProjectionElem::Subslice { .. } |
1470-
ProjectionElem::Downcast(..) => self.is_unique(&proj.base),
14711412
}
14721413
}
14731414
}

0 commit comments

Comments
 (0)