Skip to content

Commit 180fc41

Browse files
committed
Move Place::elem methods and friends to TyCtxt
1 parent d32c286 commit 180fc41

File tree

13 files changed

+139
-124
lines changed

13 files changed

+139
-124
lines changed

src/librustc/mir/mod.rs

-45
Original file line numberDiff line numberDiff line change
@@ -1857,51 +1857,6 @@ impl<'tcx> Place<'tcx> {
18571857
}
18581858
}
18591859

1860-
pub fn field(self, f: Field, ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Place<'tcx> {
1861-
self.elem(ProjectionElem::Field(f, ty), tcx)
1862-
}
1863-
1864-
pub fn deref(self, tcx: TyCtxt<'tcx>) -> Place<'tcx> {
1865-
self.elem(ProjectionElem::Deref, tcx)
1866-
}
1867-
1868-
pub fn downcast(
1869-
self,
1870-
adt_def: &'tcx AdtDef,
1871-
variant_index: VariantIdx,
1872-
tcx: TyCtxt<'tcx>,
1873-
) -> Place<'tcx> {
1874-
self.elem(
1875-
ProjectionElem::Downcast(
1876-
Some(adt_def.variants[variant_index].ident.name),
1877-
variant_index,
1878-
),
1879-
tcx,
1880-
)
1881-
}
1882-
1883-
pub fn downcast_unnamed(self, variant_index: VariantIdx, tcx: TyCtxt<'tcx>) -> Place<'tcx> {
1884-
self.elem(ProjectionElem::Downcast(None, variant_index), tcx)
1885-
}
1886-
1887-
pub fn index(self, index: Local, tcx: TyCtxt<'tcx>) -> Place<'tcx> {
1888-
self.elem(ProjectionElem::Index(index), tcx)
1889-
}
1890-
1891-
/// This method copies `Place`'s projection, add an element and reintern it. Should not be used
1892-
/// to build a full `Place` it's just a convenient way to grab a projection and modify it in
1893-
/// flight.
1894-
// FIXME: It may be a better idea to move all these methods to `PlaceBuilder`
1895-
pub fn elem(self, elem: PlaceElem<'tcx>, tcx: TyCtxt<'tcx>) -> Place<'tcx> {
1896-
let mut projection = self.projection.to_vec();
1897-
projection.push(elem);
1898-
1899-
Place {
1900-
base: self.base,
1901-
projection: tcx.intern_place_elems(&projection),
1902-
}
1903-
}
1904-
19051860
/// Returns `true` if this `Place` contains a `Deref` projection.
19061861
///
19071862
/// If `Place::is_indirect` returns false, the caller knows that the `Place` refers to the

src/librustc/ty/context.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::middle::cstore::EncodedMetadata;
2121
use crate::middle::lang_items;
2222
use crate::middle::resolve_lifetime::{self, ObjectLifetimeDefault};
2323
use crate::middle::stability;
24-
use crate::mir::{Body, interpret, PlaceElem, ProjectionKind, Promoted};
24+
use crate::mir::{Body, Field, interpret, Local, Place, PlaceElem, ProjectionKind, Promoted};
2525
use crate::mir::interpret::{ConstValue, Allocation, Scalar};
2626
use crate::ty::subst::{GenericArg, InternalSubsts, SubstsRef, Subst};
2727
use crate::ty::ReprOptions;
@@ -2597,6 +2597,48 @@ impl<'tcx> TyCtxt<'tcx> {
25972597
self.mk_ty(Opaque(def_id, substs))
25982598
}
25992599

2600+
pub fn mk_place_field(self, place: Place<'tcx>, f: Field, ty: Ty<'tcx>) -> Place<'tcx> {
2601+
self.mk_place_elem(place, PlaceElem::Field(f, ty))
2602+
}
2603+
2604+
pub fn mk_place_deref(self, place: Place<'tcx>) -> Place<'tcx> {
2605+
self.mk_place_elem(place, PlaceElem::Deref)
2606+
}
2607+
2608+
pub fn mk_place_downcast(
2609+
self,
2610+
place: Place<'tcx>,
2611+
adt_def: &'tcx AdtDef,
2612+
variant_index: VariantIdx,
2613+
) -> Place<'tcx> {
2614+
self.mk_place_elem(
2615+
place,
2616+
PlaceElem::Downcast(Some(adt_def.variants[variant_index].ident.name), variant_index),
2617+
)
2618+
}
2619+
2620+
pub fn mk_place_downcast_unnamed(
2621+
self,
2622+
place: Place<'tcx>,
2623+
variant_index: VariantIdx,
2624+
) -> Place<'tcx> {
2625+
self.mk_place_elem(place, PlaceElem::Downcast(None, variant_index))
2626+
}
2627+
2628+
pub fn mk_place_index(self, place: Place<'tcx>, index: Local) -> Place<'tcx> {
2629+
self.mk_place_elem(place, PlaceElem::Index(index))
2630+
}
2631+
2632+
/// This method copies `Place`'s projection, add an element and reintern it. Should not be used
2633+
/// to build a full `Place` it's just a convenient way to grab a projection and modify it in
2634+
/// flight.
2635+
pub fn mk_place_elem(self, place: Place<'tcx>, elem: PlaceElem<'tcx>) -> Place<'tcx> {
2636+
let mut projection = place.projection.to_vec();
2637+
projection.push(elem);
2638+
2639+
Place { base: place.base, projection: self.intern_place_elems(&projection) }
2640+
}
2641+
26002642
pub fn intern_existential_predicates(self, eps: &[ExistentialPredicate<'tcx>])
26012643
-> &'tcx List<ExistentialPredicate<'tcx>> {
26022644
assert!(!eps.is_empty());

src/librustc_mir/build/expr/as_rvalue.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
139139
// initialize the box contents:
140140
unpack!(
141141
block = this.into(
142-
&Place::from(result).deref(this.hir.tcx()),
142+
&this.hir.tcx().mk_place_deref(Place::from(result)),
143143
block, value
144144
)
145145
);
@@ -296,10 +296,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
296296
.zip(field_types.into_iter())
297297
.map(|(n, ty)| match fields_map.get(&n) {
298298
Some(v) => v.clone(),
299-
None => this.consume_by_copy_or_move(base.clone().field(
299+
None => this.consume_by_copy_or_move(this.hir.tcx().mk_place_field(
300+
base.clone(),
300301
n,
301302
ty,
302-
this.hir.tcx(),
303303
)),
304304
})
305305
.collect()
@@ -402,8 +402,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
402402
let val_fld = Field::new(0);
403403
let of_fld = Field::new(1);
404404

405-
let val = result_value.clone().field(val_fld, ty, self.hir.tcx());
406-
let of = result_value.field(of_fld, bool_ty, self.hir.tcx());
405+
let tcx = self.hir.tcx();
406+
let val = tcx.mk_place_field(result_value.clone(), val_fld, ty);
407+
let of = tcx.mk_place_field(result_value, of_fld, bool_ty);
407408

408409
let err = PanicInfo::Overflow(op);
409410

src/librustc_mir/build/expr/into.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
235235
});
236236
let ptr_temp = Place::from(ptr_temp);
237237
let block = unpack!(this.into(&ptr_temp, block, ptr));
238-
this.into(&ptr_temp.deref(this.hir.tcx()), block, val)
238+
this.into(&this.hir.tcx().mk_place_deref(ptr_temp), block, val)
239239
} else {
240240
let args: Vec<_> = args
241241
.into_iter()

src/librustc_mir/build/matches/simplify.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
166166
}
167167
});
168168
if irrefutable {
169-
let place = match_pair.place.downcast(adt_def, variant_index, tcx);
169+
let place = tcx.mk_place_downcast(match_pair.place, adt_def, variant_index);
170170
candidate.match_pairs.extend(self.field_match_pairs(place, subpatterns));
171171
Ok(())
172172
} else {
@@ -191,7 +191,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
191191
}
192192

193193
PatKind::Deref { ref subpattern } => {
194-
let place = match_pair.place.deref(tcx);
194+
let place = tcx.mk_place_deref(match_pair.place);
195195
candidate.match_pairs.push(MatchPair::new(place, subpattern));
196196
Ok(())
197197
}

src/librustc_mir/build/matches/test.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -743,23 +743,21 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
743743
candidate: &mut Candidate<'pat, 'tcx>,
744744
) {
745745
let match_pair = candidate.match_pairs.remove(match_pair_index);
746+
let tcx = self.hir.tcx();
746747

747748
// So, if we have a match-pattern like `x @ Enum::Variant(P1, P2)`,
748749
// we want to create a set of derived match-patterns like
749750
// `(x as Variant).0 @ P1` and `(x as Variant).1 @ P1`.
750751
let elem = ProjectionElem::Downcast(
751752
Some(adt_def.variants[variant_index].ident.name), variant_index);
752-
let downcast_place = match_pair.place.elem(elem, self.hir.tcx()); // `(x as Variant)`
753-
let consequent_match_pairs =
754-
subpatterns.iter()
755-
.map(|subpattern| {
756-
// e.g., `(x as Variant).0`
757-
let place = downcast_place.clone().field(subpattern.field,
758-
subpattern.pattern.ty,
759-
self.hir.tcx());
760-
// e.g., `(x as Variant).0 @ P1`
761-
MatchPair::new(place, &subpattern.pattern)
762-
});
753+
let downcast_place = tcx.mk_place_elem(match_pair.place, elem); // `(x as Variant)`
754+
let consequent_match_pairs = subpatterns.iter().map(|subpattern| {
755+
// e.g., `(x as Variant).0`
756+
let place =
757+
tcx.mk_place_field(downcast_place.clone(), subpattern.field, subpattern.pattern.ty);
758+
// e.g., `(x as Variant).0 @ P1`
759+
MatchPair::new(place, &subpattern.pattern)
760+
});
763761

764762
candidate.match_pairs.extend(consequent_match_pairs);
765763
}

src/librustc_mir/build/matches/util.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@ use std::u32;
66
use std::convert::TryInto;
77

88
impl<'a, 'tcx> Builder<'a, 'tcx> {
9-
pub fn field_match_pairs<'pat>(&mut self,
10-
place: Place<'tcx>,
11-
subpatterns: &'pat [FieldPat<'tcx>])
12-
-> Vec<MatchPair<'pat, 'tcx>> {
13-
subpatterns.iter()
14-
.map(|fieldpat| {
15-
let place = place.clone().field(fieldpat.field,
16-
fieldpat.pattern.ty,
17-
self.hir.tcx());
18-
MatchPair::new(place, &fieldpat.pattern)
19-
})
20-
.collect()
9+
pub fn field_match_pairs<'pat>(
10+
&mut self,
11+
place: Place<'tcx>,
12+
subpatterns: &'pat [FieldPat<'tcx>],
13+
) -> Vec<MatchPair<'pat, 'tcx>> {
14+
subpatterns
15+
.iter()
16+
.map(|fieldpat| {
17+
let place = self.hir.tcx().mk_place_field(
18+
place.clone(),
19+
fieldpat.field,
20+
fieldpat.pattern.ty,
21+
);
22+
MatchPair::new(place, &fieldpat.pattern)
23+
})
24+
.collect()
2125
}
2226

2327
pub fn prefix_slice_suffix<'pat>(&mut self,
@@ -28,6 +32,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2832
suffix: &'pat [Pat<'tcx>]) {
2933
let min_length = prefix.len() + suffix.len();
3034
let min_length = min_length.try_into().unwrap();
35+
let tcx = self.hir.tcx();
3136

3237
match_pairs.extend(
3338
prefix.iter()
@@ -38,16 +43,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3843
min_length,
3944
from_end: false,
4045
};
41-
let place = place.clone().elem(elem, self.hir.tcx());
46+
let place = tcx.mk_place_elem(place.clone(), elem);
4247
MatchPair::new(place, subpattern)
4348
})
4449
);
4550

4651
if let Some(subslice_pat) = opt_slice {
47-
let subslice = place.clone().elem(ProjectionElem::Subslice {
52+
let subslice = tcx.mk_place_elem(place.clone(),ProjectionElem::Subslice {
4853
from: prefix.len() as u32,
4954
to: suffix.len() as u32
50-
}, self.hir.tcx());
55+
});
5156
match_pairs.push(MatchPair::new(subslice, subslice_pat));
5257
}
5358

@@ -61,7 +66,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6166
min_length,
6267
from_end: true,
6368
};
64-
let place = place.clone().elem(elem, self.hir.tcx());
69+
let place = tcx.mk_place_elem(place.clone(), elem);
6570
MatchPair::new(place, subpattern)
6671
})
6772
);

src/librustc_mir/dataflow/move_paths/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
274274
// Box starts out uninitialized - need to create a separate
275275
// move-path for the interior so it will be separate from
276276
// the exterior.
277-
self.create_move_path(&place.clone().deref(self.builder.tcx));
277+
self.create_move_path(&self.builder.tcx.mk_place_deref(place.clone()));
278278
self.gather_init(place.as_ref(), InitKind::Shallow);
279279
} else {
280280
self.gather_init(place.as_ref(), InitKind::Deep);

src/librustc_mir/shim.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option<Ty<'tcx>>)
231231
tcx,
232232
param_env
233233
};
234-
let dropee = dropee_ptr.deref(tcx);
234+
let dropee = tcx.mk_place_deref(dropee_ptr);
235235
let resume_block = elaborator.patch.resume_block();
236236
elaborate_drops::elaborate_drop(
237237
&mut elaborator,
@@ -312,7 +312,7 @@ fn build_clone_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, self_ty: Ty<'tcx>) -
312312
let is_copy = self_ty.is_copy_modulo_regions(tcx, param_env, builder.span);
313313

314314
let dest = Place::return_place();
315-
let src = Place::from(Local::new(1+0)).deref(tcx);
315+
let src = tcx.mk_place_deref(Place::from(Local::new(1+0)));
316316

317317
match self_ty.kind {
318318
_ if is_copy => builder.copy_shim(),
@@ -415,7 +415,7 @@ impl CloneShimBuilder<'tcx> {
415415
}
416416

417417
fn copy_shim(&mut self) {
418-
let rcvr = Place::from(Local::new(1+0)).deref(self.tcx);
418+
let rcvr = self.tcx.mk_place_deref(Place::from(Local::new(1+0)));
419419
let ret_statement = self.make_statement(
420420
StatementKind::Assign(
421421
box(
@@ -561,8 +561,8 @@ impl CloneShimBuilder<'tcx> {
561561
// BB #2
562562
// `dest[i] = Clone::clone(src[beg])`;
563563
// Goto #3 if ok, #5 if unwinding happens.
564-
let dest_field = dest.clone().index(beg, self.tcx);
565-
let src_field = src.index(beg, self.tcx);
564+
let dest_field = self.tcx.mk_place_index(dest.clone(), beg);
565+
let src_field = self.tcx.mk_place_index(src, beg);
566566
self.make_clone_call(dest_field, src_field, ty, BasicBlock::new(3),
567567
BasicBlock::new(5));
568568

@@ -616,7 +616,7 @@ impl CloneShimBuilder<'tcx> {
616616
// BB #7 (cleanup)
617617
// `drop(dest[beg])`;
618618
self.block(vec![], TerminatorKind::Drop {
619-
location: dest.index(beg, self.tcx),
619+
location: self.tcx.mk_place_index(dest, beg),
620620
target: BasicBlock::new(8),
621621
unwind: None,
622622
}, true);
@@ -648,9 +648,9 @@ impl CloneShimBuilder<'tcx> {
648648
let mut previous_field = None;
649649
for (i, ity) in tys.enumerate() {
650650
let field = Field::new(i);
651-
let src_field = src.clone().field(field, ity, self.tcx);
651+
let src_field = self.tcx.mk_place_field(src.clone(), field, ity);
652652

653-
let dest_field = dest.clone().field(field, ity, self.tcx);
653+
let dest_field = self.tcx.mk_place_field(dest.clone(), field, ity);
654654

655655
// #(2i + 1) is the cleanup block for the previous clone operation
656656
let cleanup_block = self.block_index_offset(1);
@@ -721,14 +721,14 @@ fn build_call_shim<'tcx>(
721721

722722
let rcvr = match rcvr_adjustment {
723723
Adjustment::Identity => Operand::Move(rcvr_l),
724-
Adjustment::Deref => Operand::Copy(rcvr_l.deref(tcx)),
724+
Adjustment::Deref => Operand::Copy(tcx.mk_place_deref(rcvr_l)),
725725
Adjustment::DerefMove => {
726726
// fn(Self, ...) -> fn(*mut Self, ...)
727727
let arg_ty = local_decls[rcvr_arg].ty;
728728
debug_assert!(tcx.generics_of(def_id).has_self && arg_ty == tcx.types.self_param);
729729
local_decls[rcvr_arg].ty = tcx.mk_mut_ptr(arg_ty);
730730

731-
Operand::Move(rcvr_l.deref(tcx))
731+
Operand::Move(tcx.mk_place_deref(rcvr_l))
732732
}
733733
Adjustment::RefMut => {
734734
// let rcvr = &mut rcvr;
@@ -772,7 +772,7 @@ fn build_call_shim<'tcx>(
772772
if let Some(untuple_args) = untuple_args {
773773
args.extend(untuple_args.iter().enumerate().map(|(i, ity)| {
774774
let arg_place = Place::from(Local::new(1+1));
775-
Operand::Move(arg_place.field(Field::new(i), *ity, tcx))
775+
Operand::Move(tcx.mk_place_field(arg_place, Field::new(i), *ity))
776776
}));
777777
} else {
778778
args.extend((1..sig.inputs().len()).map(|i| {

src/librustc_mir/transform/generator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl TransformVisitor<'tcx> {
246246
// Create a Place referencing a generator struct field
247247
fn make_field(&self, variant_index: VariantIdx, idx: usize, ty: Ty<'tcx>) -> Place<'tcx> {
248248
let self_place = Place::from(self_arg());
249-
let base = self_place.downcast_unnamed(variant_index, self.tcx);
249+
let base = self.tcx.mk_place_downcast_unnamed(self_place, variant_index);
250250
let mut projection = base.projection.to_vec();
251251
projection.push(ProjectionElem::Field(Field::new(idx), ty));
252252

0 commit comments

Comments
 (0)