Skip to content

Commit e73d189

Browse files
committed
Use slice patterns to match projection base
1 parent b04e6c7 commit e73d189

File tree

12 files changed

+67
-118
lines changed

12 files changed

+67
-118
lines changed

src/librustc/mir/visit.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -720,12 +720,9 @@ macro_rules! make_mir_visitor {
720720
projection: & $($mutability)? [PlaceElem<'tcx>],
721721
context: PlaceContext,
722722
location: Location) {
723-
if !projection.is_empty() {
724-
let proj_len = projection.len();
725-
let proj_base = & $($mutability)? projection[..proj_len - 1];
723+
if let [proj_base @ .., elem] = projection {
726724
self.visit_projection(base, proj_base, context, location);
727725

728-
let elem = & $($mutability)? projection[proj_len - 1];
729726
match elem {
730727
ProjectionElem::Field(_field, ty) => {
731728
self.visit_ty(ty, TyContext::Location(location));

src/librustc_codegen_ssa/mir/analyze.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,7 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
105105
) {
106106
let cx = self.fx.cx;
107107

108-
if let [.., elem] = place_ref.projection {
109-
// FIXME(spastorino) include this in the pattern when stabilized
110-
let proj_base = &place_ref.projection[..place_ref.projection.len() - 1];
111-
108+
if let [proj_base @ .., elem] = place_ref.projection {
112109
// Allow uses of projections that are ZSTs or from scalar fields.
113110
let is_consume = match context {
114111
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) |

src/librustc_codegen_ssa/mir/place.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -514,10 +514,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
514514
},
515515
mir::PlaceRef {
516516
base,
517-
projection: [.., mir::ProjectionElem::Deref],
517+
projection: [proj_base @ .., mir::ProjectionElem::Deref],
518518
} => {
519-
let proj_base = &place_ref.projection[..place_ref.projection.len() - 1];
520-
521519
// Load the pointer from its location.
522520
self.codegen_consume(bx, &mir::PlaceRef {
523521
base,
@@ -526,10 +524,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
526524
}
527525
mir::PlaceRef {
528526
base,
529-
projection: [.., elem],
527+
projection: [proj_base @ .., elem],
530528
} => {
531-
let proj_base = &place_ref.projection[..place_ref.projection.len() - 1];
532-
533529
// FIXME turn this recursion into iteration
534530
let cg_base = self.codegen_place(bx, &mir::PlaceRef {
535531
base,

src/librustc_mir/borrow_check/error_reporting.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
179179
}
180180
PlaceRef {
181181
base,
182-
projection: [.., elem],
182+
projection: [proj_base @ .., elem],
183183
} => {
184-
let proj_base = &place.projection[..place.projection.len() - 1];
185-
186184
match elem {
187185
ProjectionElem::Deref => {
188186
let upvar_field_projection =
@@ -363,11 +361,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
363361
self.describe_field_from_ty(&static_.ty, field, None),
364362
PlaceRef {
365363
base,
366-
projection: [.., elem],
364+
projection: [proj_base @ .., elem],
367365
} => match elem {
368366
ProjectionElem::Deref => {
369-
let proj_base = &place.projection[..place.projection.len() - 1];
370-
371367
self.describe_field(PlaceRef {
372368
base,
373369
projection: proj_base,
@@ -384,8 +380,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
384380
ProjectionElem::Index(..)
385381
| ProjectionElem::ConstantIndex { .. }
386382
| ProjectionElem::Subslice { .. } => {
387-
let proj_base = &place.projection[..place.projection.len() - 1];
388-
389383
self.describe_field(PlaceRef {
390384
base,
391385
projection: proj_base,

src/librustc_mir/borrow_check/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,10 +2187,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
21872187
}
21882188
PlaceRef {
21892189
base: _,
2190-
projection: [.., elem],
2190+
projection: [proj_base @ .., elem],
21912191
} => {
2192-
let proj_base = &place.projection[..place.projection.len() - 1];
2193-
21942192
match elem {
21952193
ProjectionElem::Deref => {
21962194
let base_ty =

src/librustc_mir/borrow_check/move_errors.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
305305
let upvar_field = self.prefixes(move_place.as_ref(), PrefixSet::All)
306306
.find_map(|p| self.is_upvar_field_projection(p));
307307

308-
let deref_base = match deref_target_place.projection {
309-
box [.., ProjectionElem::Deref] => {
310-
let proj_base =
311-
&deref_target_place.projection[..deref_target_place.projection.len() - 1];
312-
308+
let deref_base = match &deref_target_place.projection {
309+
box [proj_base @ .., ProjectionElem::Deref] => {
313310
PlaceRef {
314311
base: &deref_target_place.base,
315312
projection: proj_base,

src/librustc_mir/borrow_check/mutability_errors.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
6565

6666
PlaceRef {
6767
base: _,
68-
projection: [.., ProjectionElem::Field(upvar_index, _)],
68+
projection: [proj_base @ .., ProjectionElem::Field(upvar_index, _)],
6969
} => {
70-
let proj_base = &the_place_err.projection[..the_place_err.projection.len() - 1];
71-
7270
debug_assert!(is_closure_or_generator(
7371
Place::ty_from(&the_place_err.base, proj_base, self.body, self.infcx.tcx).ty
7472
));
@@ -329,10 +327,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
329327
// Also suggest adding mut for upvars
330328
PlaceRef {
331329
base,
332-
projection: [.., ProjectionElem::Field(upvar_index, _)],
330+
projection: [proj_base @ .., ProjectionElem::Field(upvar_index, _)],
333331
} => {
334-
let proj_base = &the_place_err.projection[..the_place_err.projection.len() - 1];
335-
336332
debug_assert!(is_closure_or_generator(
337333
Place::ty_from(base, proj_base, self.body, self.infcx.tcx).ty
338334
));

src/librustc_mir/borrow_check/prefixes.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,8 @@ impl<'cx, 'tcx> Iterator for Prefixes<'cx, 'tcx> {
8888
}
8989
PlaceRef {
9090
base: _,
91-
projection: [.., elem],
91+
projection: [proj_base @ .., elem],
9292
} => {
93-
let proj_base = &cursor.projection[..cursor.projection.len() - 1];
94-
9593
match elem {
9694
ProjectionElem::Field(_ /*field*/, _ /*ty*/) => {
9795
// FIXME: add union handling

src/librustc_mir/dataflow/move_paths/builder.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
436436

437437
// Check if we are assigning into a field of a union, if so, lookup the place
438438
// of the union so it is marked as initialized again.
439-
if let [.., ProjectionElem::Field(_, _)] = place.projection {
440-
let proj_base = &place.projection[..place.projection.len() - 1];
439+
if let [proj_base @ .., ProjectionElem::Field(_, _)] = place.projection {
441440
if let ty::Adt(def, _) =
442441
Place::ty_from(place.base, proj_base, self.builder.body, self.builder.tcx).ty.sty
443442
{

src/librustc_mir/transform/instcombine.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,11 @@ impl OptimizationFinder<'b, 'tcx> {
9090
impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
9191
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
9292
if let Rvalue::Ref(_, _, Place {
93-
base: _,
94-
projection: box [.., elem],
93+
base,
94+
projection: box [proj_base @ .., ProjectionElem::Deref],
9595
}) = rvalue {
96-
if *elem == ProjectionElem::Deref {
97-
// FIXME remove this once we can use slices patterns
98-
if let Rvalue::Ref(_, _, Place {
99-
base,
100-
projection,
101-
}) = rvalue {
102-
let proj_base = &projection[..projection.len() - 1];
103-
if Place::ty_from(base, proj_base, self.body, self.tcx).ty.is_region_ptr() {
104-
self.optimizations.and_stars.insert(location);
105-
}
106-
}
96+
if Place::ty_from(base, proj_base, self.body, self.tcx).ty.is_region_ptr() {
97+
self.optimizations.and_stars.insert(location);
10798
}
10899
}
109100

0 commit comments

Comments
 (0)