Skip to content

Commit 97098f4

Browse files
authoredAug 2, 2019
Rollup merge of #63203 - spastorino:is-mutable-use-place-ref, r=oli-obk
Make is_mutable use PlaceRef instead of it's fields r? @oli-obk
2 parents 3396550 + 7052c35 commit 97098f4

File tree

1 file changed

+71
-62
lines changed
  • src/librustc_mir/borrow_check

1 file changed

+71
-62
lines changed
 

‎src/librustc_mir/borrow_check/mod.rs

+71-62
Original file line numberDiff line numberDiff line change
@@ -1942,7 +1942,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
19421942
BorrowKind::Mut { .. } => is_local_mutation_allowed,
19431943
BorrowKind::Shared | BorrowKind::Shallow => unreachable!(),
19441944
};
1945-
match self.is_mutable(&place.base, &place.projection, is_local_mutation_allowed) {
1945+
match self.is_mutable(place.as_ref(), is_local_mutation_allowed) {
19461946
Ok(root_place) => {
19471947
self.add_used_mut(root_place, flow_state);
19481948
return false;
@@ -1954,7 +1954,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
19541954
}
19551955
}
19561956
Reservation(WriteKind::Mutate) | Write(WriteKind::Mutate) => {
1957-
match self.is_mutable(&place.base, &place.projection, is_local_mutation_allowed) {
1957+
match self.is_mutable(place.as_ref(), is_local_mutation_allowed) {
19581958
Ok(root_place) => {
19591959
self.add_used_mut(root_place, flow_state);
19601960
return false;
@@ -1974,8 +1974,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
19741974
| Write(wk @ WriteKind::StorageDeadOrDrop)
19751975
| Write(wk @ WriteKind::MutableBorrow(BorrowKind::Shared))
19761976
| Write(wk @ WriteKind::MutableBorrow(BorrowKind::Shallow)) => {
1977-
if let (Err(_place_err), true) = (
1978-
self.is_mutable(&place.base, &place.projection, is_local_mutation_allowed),
1977+
if let (Err(place_err), true) = (
1978+
self.is_mutable(place.as_ref(), is_local_mutation_allowed),
19791979
self.errors_buffer.is_empty()
19801980
) {
19811981
if self.infcx.tcx.migrate_borrowck() {
@@ -1996,10 +1996,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
19961996
self.report_mutability_error(
19971997
place,
19981998
span,
1999-
PlaceRef {
2000-
base: _place_err.0,
2001-
projection: _place_err.1,
2002-
},
1999+
place_err,
20032000
error_access,
20042001
location,
20052002
);
@@ -2033,10 +2030,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
20332030
self.report_mutability_error(
20342031
place,
20352032
span,
2036-
PlaceRef {
2037-
base: the_place_err.0,
2038-
projection: the_place_err.1,
2039-
},
2033+
the_place_err,
20402034
error_access,
20412035
location,
20422036
);
@@ -2107,78 +2101,86 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
21072101
/// Returns the root place if the place passed in is a projection.
21082102
fn is_mutable<'d>(
21092103
&self,
2110-
place_base: &'d PlaceBase<'tcx>,
2111-
place_projection: &'d Option<Box<Projection<'tcx>>>,
2104+
place: PlaceRef<'d, 'tcx>,
21122105
is_local_mutation_allowed: LocalMutationIsAllowed,
2113-
) -> Result<RootPlace<'d, 'tcx>, (&'d PlaceBase<'tcx>, &'d Option<Box<Projection<'tcx>>>)> {
2114-
match (place_base, place_projection) {
2115-
(PlaceBase::Local(local), None) => {
2106+
) -> Result<RootPlace<'d, 'tcx>, PlaceRef<'d, 'tcx>> {
2107+
match place {
2108+
PlaceRef {
2109+
base: PlaceBase::Local(local),
2110+
projection: None,
2111+
} => {
21162112
let local = &self.body.local_decls[*local];
21172113
match local.mutability {
21182114
Mutability::Not => match is_local_mutation_allowed {
21192115
LocalMutationIsAllowed::Yes => Ok(RootPlace {
2120-
place_base,
2121-
place_projection,
2116+
place_base: place.base,
2117+
place_projection: place.projection,
21222118
is_local_mutation_allowed: LocalMutationIsAllowed::Yes,
21232119
}),
21242120
LocalMutationIsAllowed::ExceptUpvars => Ok(RootPlace {
2125-
place_base,
2126-
place_projection,
2121+
place_base: place.base,
2122+
place_projection: place.projection,
21272123
is_local_mutation_allowed: LocalMutationIsAllowed::ExceptUpvars,
21282124
}),
2129-
LocalMutationIsAllowed::No => Err((place_base, place_projection)),
2125+
LocalMutationIsAllowed::No => Err(place),
21302126
},
21312127
Mutability::Mut => Ok(RootPlace {
2132-
place_base,
2133-
place_projection,
2128+
place_base: place.base,
2129+
place_projection: place.projection,
21342130
is_local_mutation_allowed,
21352131
}),
21362132
}
21372133
}
21382134
// The rules for promotion are made by `qualify_consts`, there wouldn't even be a
21392135
// `Place::Promoted` if the promotion weren't 100% legal. So we just forward this
2140-
(PlaceBase::Static(box Static {
2141-
kind: StaticKind::Promoted(_),
2142-
..
2143-
}), None) =>
2136+
PlaceRef {
2137+
base: PlaceBase::Static(box Static {
2138+
kind: StaticKind::Promoted(_),
2139+
..
2140+
}),
2141+
projection: None,
2142+
} =>
21442143
Ok(RootPlace {
2145-
place_base,
2146-
place_projection,
2144+
place_base: place.base,
2145+
place_projection: place.projection,
21472146
is_local_mutation_allowed,
21482147
}),
2149-
(PlaceBase::Static(box Static {
2150-
kind: StaticKind::Static(def_id),
2151-
..
2152-
}), None) => {
2148+
PlaceRef {
2149+
base: PlaceBase::Static(box Static {
2150+
kind: StaticKind::Static(def_id),
2151+
..
2152+
}),
2153+
projection: None,
2154+
} => {
21532155
if !self.infcx.tcx.is_mutable_static(*def_id) {
2154-
Err((place_base, place_projection))
2156+
Err(place)
21552157
} else {
21562158
Ok(RootPlace {
2157-
place_base,
2158-
place_projection,
2159+
place_base: place.base,
2160+
place_projection: place.projection,
21592161
is_local_mutation_allowed,
21602162
})
21612163
}
21622164
}
2163-
(_, Some(ref proj)) => {
2165+
PlaceRef {
2166+
base: _,
2167+
projection: Some(proj),
2168+
} => {
21642169
match proj.elem {
21652170
ProjectionElem::Deref => {
21662171
let base_ty =
2167-
Place::ty_from(place_base, &proj.base, self.body, self.infcx.tcx).ty;
2172+
Place::ty_from(place.base, &proj.base, self.body, self.infcx.tcx).ty;
21682173

21692174
// Check the kind of deref to decide
21702175
match base_ty.sty {
21712176
ty::Ref(_, _, mutbl) => {
21722177
match mutbl {
21732178
// Shared borrowed data is never mutable
2174-
hir::MutImmutable => Err((place_base, place_projection)),
2179+
hir::MutImmutable => Err(place),
21752180
// Mutably borrowed data is mutable, but only if we have a
21762181
// unique path to the `&mut`
21772182
hir::MutMutable => {
2178-
let mode = match self.is_upvar_field_projection(PlaceRef {
2179-
base: &place_base,
2180-
projection: &place_projection,
2181-
}) {
2183+
let mode = match self.is_upvar_field_projection(place) {
21822184
Some(field)
21832185
if self.upvars[field.index()].by_ref =>
21842186
{
@@ -2187,28 +2189,34 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
21872189
_ => LocalMutationIsAllowed::Yes,
21882190
};
21892191

2190-
self.is_mutable(place_base, &proj.base, mode)
2192+
self.is_mutable(PlaceRef {
2193+
base: place.base,
2194+
projection: &proj.base,
2195+
}, mode)
21912196
}
21922197
}
21932198
}
21942199
ty::RawPtr(tnm) => {
21952200
match tnm.mutbl {
21962201
// `*const` raw pointers are not mutable
2197-
hir::MutImmutable => Err((place_base, place_projection)),
2202+
hir::MutImmutable => Err(place),
21982203
// `*mut` raw pointers are always mutable, regardless of
21992204
// context. The users have to check by themselves.
22002205
hir::MutMutable => {
22012206
Ok(RootPlace {
2202-
place_base,
2203-
place_projection,
2207+
place_base: place.base,
2208+
place_projection: place.projection,
22042209
is_local_mutation_allowed,
22052210
})
22062211
}
22072212
}
22082213
}
22092214
// `Box<T>` owns its content, so mutable if its location is mutable
22102215
_ if base_ty.is_box() => {
2211-
self.is_mutable(place_base, &proj.base, is_local_mutation_allowed)
2216+
self.is_mutable(PlaceRef {
2217+
base: place.base,
2218+
projection: &proj.base,
2219+
}, is_local_mutation_allowed)
22122220
}
22132221
// Deref should only be for reference, pointers or boxes
22142222
_ => bug!("Deref of unexpected type: {:?}", base_ty),
@@ -2221,21 +2229,18 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
22212229
| ProjectionElem::ConstantIndex { .. }
22222230
| ProjectionElem::Subslice { .. }
22232231
| ProjectionElem::Downcast(..) => {
2224-
let upvar_field_projection = self.is_upvar_field_projection(PlaceRef {
2225-
base: &place_base,
2226-
projection: &place_projection,
2227-
});
2232+
let upvar_field_projection = self.is_upvar_field_projection(place);
22282233
if let Some(field) = upvar_field_projection {
22292234
let upvar = &self.upvars[field.index()];
22302235
debug!(
22312236
"upvar.mutability={:?} local_mutation_is_allowed={:?} \
2232-
place={:?} {:?}",
2233-
upvar, is_local_mutation_allowed, place_base, place_projection
2237+
place={:?}",
2238+
upvar, is_local_mutation_allowed, place
22342239
);
22352240
match (upvar.mutability, is_local_mutation_allowed) {
22362241
(Mutability::Not, LocalMutationIsAllowed::No)
22372242
| (Mutability::Not, LocalMutationIsAllowed::ExceptUpvars) => {
2238-
Err((place_base, place_projection))
2243+
Err(place)
22392244
}
22402245
(Mutability::Not, LocalMutationIsAllowed::Yes)
22412246
| (Mutability::Mut, _) => {
@@ -2265,18 +2270,22 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
22652270
// });
22662271
// }
22672272
// ```
2268-
let _ = self.is_mutable(place_base,
2269-
&proj.base,
2270-
is_local_mutation_allowed)?;
2273+
let _ = self.is_mutable(PlaceRef {
2274+
base: place.base,
2275+
projection: &proj.base,
2276+
}, is_local_mutation_allowed)?;
22712277
Ok(RootPlace {
2272-
place_base,
2273-
place_projection,
2278+
place_base: place.base,
2279+
place_projection: place.projection,
22742280
is_local_mutation_allowed,
22752281
})
22762282
}
22772283
}
22782284
} else {
2279-
self.is_mutable(place_base, &proj.base, is_local_mutation_allowed)
2285+
self.is_mutable(PlaceRef {
2286+
base: place.base,
2287+
projection: &proj.base,
2288+
}, is_local_mutation_allowed)
22802289
}
22812290
}
22822291
}

0 commit comments

Comments
 (0)