Skip to content

Commit 4122d22

Browse files
committed
Remove adt_def from PlaceTy and make it a struct
1 parent ac29ca7 commit 4122d22

30 files changed

+157
-196
lines changed

src/librustc/mir/tcx.rs

+42-74
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@
44
*/
55

66
use crate::mir::*;
7-
use crate::ty::subst::{Subst, SubstsRef};
8-
use crate::ty::{self, AdtDef, Ty, TyCtxt};
7+
use crate::ty::subst::Subst;
8+
use crate::ty::{self, Ty, TyCtxt};
99
use crate::ty::layout::VariantIdx;
1010
use crate::hir;
1111
use crate::ty::util::IntTypeExt;
1212

1313
#[derive(Copy, Clone, Debug)]
14-
pub enum PlaceTy<'tcx> {
15-
/// Normal type.
16-
Ty { ty: Ty<'tcx> },
17-
18-
/// Downcast to a particular variant of an enum.
19-
Downcast { adt_def: &'tcx AdtDef,
20-
substs: SubstsRef<'tcx>,
21-
variant_index: VariantIdx },
14+
pub struct PlaceTy<'tcx> {
15+
pub ty: Ty<'tcx>,
16+
/// Downcast to a particular variant of an enum, if included.
17+
pub variant_index: Option<VariantIdx>,
2218
}
2319

2420
static_assert!(PLACE_TY_IS_3_PTRS_LARGE:
@@ -27,16 +23,7 @@ static_assert!(PLACE_TY_IS_3_PTRS_LARGE:
2723

2824
impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
2925
pub fn from_ty(ty: Ty<'tcx>) -> PlaceTy<'tcx> {
30-
PlaceTy::Ty { ty }
31-
}
32-
33-
pub fn to_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
34-
match *self {
35-
PlaceTy::Ty { ty } =>
36-
ty,
37-
PlaceTy::Downcast { adt_def, substs, variant_index: _ } =>
38-
tcx.mk_adt(adt_def, substs),
39-
}
26+
PlaceTy { ty, variant_index: None }
4027
}
4128

4229
/// `place_ty.field_ty(tcx, f)` computes the type at a given field
@@ -48,21 +35,20 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
4835
/// Note that the resulting type has not been normalized.
4936
pub fn field_ty(self, tcx: TyCtxt<'a, 'gcx, 'tcx>, f: &Field) -> Ty<'tcx>
5037
{
51-
// Pass `0` here so it can be used as a "default" variant_index in first arm below
52-
let answer = match (self, VariantIdx::new(0)) {
53-
(PlaceTy::Ty {
54-
ty: &ty::TyS { sty: ty::TyKind::Adt(adt_def, substs), .. } }, variant_index) |
55-
(PlaceTy::Downcast { adt_def, substs, variant_index }, _) => {
56-
let variant_def = &adt_def.variants[variant_index];
38+
let answer = match self.ty.sty {
39+
ty::TyKind::Adt(adt_def, substs) => {
40+
let variant_def = match self.variant_index {
41+
None => adt_def.non_enum_variant(),
42+
Some(variant_index) => {
43+
assert!(adt_def.is_enum());
44+
&adt_def.variants[variant_index]
45+
}
46+
};
5747
let field_def = &variant_def.fields[f.index()];
5848
field_def.ty(tcx, substs)
5949
}
60-
(PlaceTy::Ty { ty }, _) => {
61-
match ty.sty {
62-
ty::Tuple(ref tys) => tys[f.index()],
63-
_ => bug!("extracting field of non-tuple non-adt: {:?}", self),
64-
}
65-
}
50+
ty::Tuple(ref tys) => tys[f.index()],
51+
_ => bug!("extracting field of non-tuple non-adt: {:?}", self),
6652
};
6753
debug!("field_ty self: {:?} f: {:?} yields: {:?}", self, f, answer);
6854
answer
@@ -94,61 +80,43 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
9480
{
9581
let answer = match *elem {
9682
ProjectionElem::Deref => {
97-
let ty = self.to_ty(tcx)
83+
let ty = self.ty
9884
.builtin_deref(true)
9985
.unwrap_or_else(|| {
10086
bug!("deref projection of non-dereferencable ty {:?}", self)
10187
})
10288
.ty;
103-
PlaceTy::Ty {
104-
ty,
105-
}
89+
PlaceTy::from_ty(ty)
10690
}
10791
ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } =>
108-
PlaceTy::Ty {
109-
ty: self.to_ty(tcx).builtin_index().unwrap()
110-
},
92+
PlaceTy::from_ty(self.ty.builtin_index().unwrap()),
11193
ProjectionElem::Subslice { from, to } => {
112-
let ty = self.to_ty(tcx);
113-
PlaceTy::Ty {
114-
ty: match ty.sty {
115-
ty::Array(inner, size) => {
116-
let size = size.unwrap_usize(tcx);
117-
let len = size - (from as u64) - (to as u64);
118-
tcx.mk_array(inner, len)
119-
}
120-
ty::Slice(..) => ty,
121-
_ => {
122-
bug!("cannot subslice non-array type: `{:?}`", self)
123-
}
124-
}
125-
}
126-
}
127-
ProjectionElem::Downcast(_name, index) =>
128-
match self.to_ty(tcx).sty {
129-
ty::Adt(adt_def, substs) => {
130-
assert!(adt_def.is_enum());
131-
assert!(index.as_usize() < adt_def.variants.len());
132-
PlaceTy::Downcast { adt_def,
133-
substs,
134-
variant_index: index }
94+
PlaceTy::from_ty(match self.ty.sty {
95+
ty::Array(inner, size) => {
96+
let size = size.unwrap_usize(tcx);
97+
let len = size - (from as u64) - (to as u64);
98+
tcx.mk_array(inner, len)
13599
}
100+
ty::Slice(..) => self.ty,
136101
_ => {
137-
bug!("cannot downcast non-ADT type: `{:?}`", self)
102+
bug!("cannot subslice non-array type: `{:?}`", self)
138103
}
139-
},
104+
})
105+
}
106+
ProjectionElem::Downcast(_name, index) =>
107+
PlaceTy { ty: self.ty, variant_index: Some(index) },
140108
ProjectionElem::Field(ref f, ref fty) =>
141-
PlaceTy::Ty { ty: handle_field(&self, f, fty) },
109+
PlaceTy::from_ty(handle_field(&self, f, fty)),
142110
};
143111
debug!("projection_ty self: {:?} elem: {:?} yields: {:?}", self, elem, answer);
144112
answer
145113
}
146114
}
147115

148-
EnumTypeFoldableImpl! {
116+
BraceStructTypeFoldableImpl! {
149117
impl<'tcx> TypeFoldable<'tcx> for PlaceTy<'tcx> {
150-
(PlaceTy::Ty) { ty },
151-
(PlaceTy::Downcast) { adt_def, substs, variant_index },
118+
ty,
119+
variant_index,
152120
}
153121
}
154122

@@ -158,9 +126,9 @@ impl<'tcx> Place<'tcx> {
158126
{
159127
match *self {
160128
Place::Base(PlaceBase::Local(index)) =>
161-
PlaceTy::Ty { ty: local_decls.local_decls()[index].ty },
129+
PlaceTy::from_ty(local_decls.local_decls()[index].ty),
162130
Place::Base(PlaceBase::Static(ref data)) =>
163-
PlaceTy::Ty { ty: data.ty },
131+
PlaceTy::from_ty(data.ty),
164132
Place::Projection(ref proj) =>
165133
proj.base.ty(local_decls, tcx).projection_ty(tcx, &proj.elem),
166134
}
@@ -185,7 +153,7 @@ impl<'tcx> Place<'tcx> {
185153
match place {
186154
Place::Projection(ref proj) => match proj.elem {
187155
ProjectionElem::Field(field, _ty) => {
188-
let base_ty = proj.base.ty(mir, *tcx).to_ty(*tcx);
156+
let base_ty = proj.base.ty(mir, *tcx).ty;
189157

190158
if (base_ty.is_closure() || base_ty.is_generator()) &&
191159
(!by_ref || mir.upvar_decls[field.index()].by_ref)
@@ -217,7 +185,7 @@ impl<'tcx> Rvalue<'tcx> {
217185
tcx.mk_array(operand.ty(local_decls, tcx), count)
218186
}
219187
Rvalue::Ref(reg, bk, ref place) => {
220-
let place_ty = place.ty(local_decls, tcx).to_ty(tcx);
188+
let place_ty = place.ty(local_decls, tcx).ty;
221189
tcx.mk_ref(reg,
222190
ty::TypeAndMut {
223191
ty: place_ty,
@@ -243,7 +211,7 @@ impl<'tcx> Rvalue<'tcx> {
243211
operand.ty(local_decls, tcx)
244212
}
245213
Rvalue::Discriminant(ref place) => {
246-
let ty = place.ty(local_decls, tcx).to_ty(tcx);
214+
let ty = place.ty(local_decls, tcx).ty;
247215
if let ty::Adt(adt_def, _) = ty.sty {
248216
adt_def.repr.discr_type().to_ty(tcx)
249217
} else {
@@ -292,7 +260,7 @@ impl<'tcx> Operand<'tcx> {
292260
{
293261
match self {
294262
&Operand::Copy(ref l) |
295-
&Operand::Move(ref l) => l.ty(local_decls, tcx).to_ty(tcx),
263+
&Operand::Move(ref l) => l.ty(local_decls, tcx).ty,
296264
&Operand::Constant(ref c) => c.ty,
297265
}
298266
}

src/librustc_codegen_ssa/mir/analyze.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ impl<'mir, 'a: 'mir, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
172172
// ZSTs don't require any actual memory access.
173173
let elem_ty = base_ty
174174
.projection_ty(cx.tcx(), &proj.elem)
175-
.to_ty(cx.tcx());
175+
.ty;
176176
let elem_ty = self.fx.monomorphize(&elem_ty);
177177
if cx.layout_of(elem_ty).is_zst() {
178178
return;
179179
}
180180

181181
if let mir::ProjectionElem::Field(..) = proj.elem {
182-
let layout = cx.layout_of(base_ty.to_ty(cx.tcx()));
182+
let layout = cx.layout_of(base_ty.ty);
183183
if cx.is_backend_immediate(layout) || cx.is_backend_scalar_pair(layout) {
184184
// Recurse with the same context, instead of `Projection`,
185185
// potentially stopping at non-operand projections,
@@ -247,7 +247,7 @@ impl<'mir, 'a: 'mir, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
247247
PlaceContext::MutatingUse(MutatingUseContext::Drop) => {
248248
let ty = mir::Place::Base(mir::PlaceBase::Local(local)).ty(self.fx.mir,
249249
self.fx.cx.tcx());
250-
let ty = self.fx.monomorphize(&ty.to_ty(self.fx.cx.tcx()));
250+
let ty = self.fx.monomorphize(&ty.ty);
251251

252252
// Only need the place if we're actually dropping it.
253253
if self.fx.cx.type_needs_drop(ty) {

src/librustc_codegen_ssa/mir/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
301301
target: mir::BasicBlock,
302302
unwind: Option<mir::BasicBlock>,
303303
) {
304-
let ty = location.ty(self.mir, bx.tcx()).to_ty(bx.tcx());
304+
let ty = location.ty(self.mir, bx.tcx()).ty;
305305
let ty = self.monomorphize(&ty);
306306
let drop_fn = monomorphize::resolve_drop_in_place(bx.tcx(), ty);
307307

src/librustc_codegen_ssa/mir/place.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,8 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
494494
mir::ProjectionElem::Subslice { from, to } => {
495495
let mut subslice = cg_base.project_index(bx,
496496
bx.cx().const_usize(from as u64));
497-
let projected_ty = PlaceTy::Ty { ty: cg_base.layout.ty }
498-
.projection_ty(tcx, &projection.elem).to_ty(tcx);
497+
let projected_ty = PlaceTy::from_ty(cg_base.layout.ty)
498+
.projection_ty(tcx, &projection.elem).ty;
499499
subslice.layout = bx.cx().layout_of(self.monomorphize(&projected_ty));
500500

501501
if subslice.layout.is_unsized() {
@@ -523,6 +523,6 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
523523
pub fn monomorphized_place_ty(&self, place: &mir::Place<'tcx>) -> Ty<'tcx> {
524524
let tcx = self.cx.tcx();
525525
let place_ty = place.ty(self.mir, tcx);
526-
self.monomorphize(&place_ty.to_ty(tcx))
526+
self.monomorphize(&place_ty.ty)
527527
}
528528
}

src/librustc_mir/borrow_check/error_reporting.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
202202
);
203203
}
204204

205-
let ty = used_place.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx);
205+
let ty = used_place.ty(self.mir, self.infcx.tcx).ty;
206206
let needs_note = match ty.sty {
207207
ty::Closure(id, _) => {
208208
let tables = self.infcx.tcx.typeck_tables_of(id);
@@ -217,7 +217,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
217217
let mpi = self.move_data.moves[move_out_indices[0]].path;
218218
let place = &self.move_data.move_paths[mpi].place;
219219

220-
let ty = place.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx);
220+
let ty = place.ty(self.mir, self.infcx.tcx).ty;
221221
let opt_name = self.describe_place_with_options(place, IncludingDowncast(true));
222222
let note_msg = match opt_name {
223223
Some(ref name) => format!("`{}`", name),
@@ -601,8 +601,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
601601
// Define a small closure that we can use to check if the type of a place
602602
// is a union.
603603
let is_union = |place: &Place<'tcx>| -> bool {
604-
place.ty(self.mir, self.infcx.tcx)
605-
.to_ty(self.infcx.tcx)
604+
place.ty(self.mir, self.infcx.tcx).ty
606605
.ty_adt_def()
607606
.map(|adt| adt.is_union())
608607
.unwrap_or(false)
@@ -651,7 +650,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
651650

652651
// Also compute the name of the union type, eg. `Foo` so we
653652
// can add a helpful note with it.
654-
let ty = base.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx);
653+
let ty = base.ty(self.mir, self.infcx.tcx).ty;
655654

656655
return Some((desc_base, desc_first, desc_second, ty.to_string()));
657656
},
@@ -1777,7 +1776,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
17771776
Place::Projection(ref proj) => match proj.elem {
17781777
ProjectionElem::Deref => self.describe_field(&proj.base, field),
17791778
ProjectionElem::Downcast(_, variant_index) => {
1780-
let base_ty = base.ty(self.mir, self.infcx.tcx).to_ty();
1779+
let base_ty = base.ty(self.mir, self.infcx.tcx).ty;
17811780
self.describe_field_from_ty(&base_ty, field, Some(variant_index))
17821781
}
17831782
ProjectionElem::Field(_, field_type) => {
@@ -1878,15 +1877,15 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
18781877
StorageDeadOrDrop::LocalStorageDead
18791878
| StorageDeadOrDrop::BoxedStorageDead => {
18801879
assert!(
1881-
base.ty(self.mir, tcx).to_ty(tcx).is_box(),
1880+
base.ty(self.mir, tcx).ty.is_box(),
18821881
"Drop of value behind a reference or raw pointer"
18831882
);
18841883
StorageDeadOrDrop::BoxedStorageDead
18851884
}
18861885
StorageDeadOrDrop::Destructor(_) => base_access,
18871886
},
18881887
ProjectionElem::Field(..) | ProjectionElem::Downcast(..) => {
1889-
let base_ty = base.ty(self.mir, tcx).to_ty(tcx);
1888+
let base_ty = base.ty(self.mir, tcx).ty;
18901889
match base_ty.sty {
18911890
ty::Adt(def, _) if def.has_dtor(tcx) => {
18921891
// Report the outermost adt with a destructor

src/librustc_mir/borrow_check/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,7 @@ impl<'cx, 'gcx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx
616616
let drop_place_ty = drop_place.ty(self.mir, self.infcx.tcx);
617617

618618
// Erase the regions.
619-
let drop_place_ty = self.infcx.tcx.erase_regions(&drop_place_ty)
620-
.to_ty(self.infcx.tcx);
619+
let drop_place_ty = self.infcx.tcx.erase_regions(&drop_place_ty).ty;
621620

622621
// "Lift" into the gcx -- once regions are erased, this type should be in the
623622
// global arenas; this "lift" operation basically just asserts that is true, but
@@ -1641,7 +1640,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
16411640
// assigning to `P.f` requires `P` itself
16421641
// be already initialized
16431642
let tcx = self.infcx.tcx;
1644-
match base.ty(self.mir, tcx).to_ty(tcx).sty {
1643+
match base.ty(self.mir, tcx).ty.sty {
16451644
ty::Adt(def, _) if def.has_dtor(tcx) => {
16461645
self.check_if_path_or_subpath_is_moved(
16471646
context, InitializationRequiringAction::Assignment,
@@ -1746,7 +1745,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
17461745
// no move out from an earlier location) then this is an attempt at initialization
17471746
// of the union - we should error in that case.
17481747
let tcx = this.infcx.tcx;
1749-
if let ty::TyKind::Adt(def, _) = base.ty(this.mir, tcx).to_ty(tcx).sty {
1748+
if let ty::TyKind::Adt(def, _) = base.ty(this.mir, tcx).ty.sty {
17501749
if def.is_union() {
17511750
if this.move_data.path_map[mpi].iter().any(|moi| {
17521751
this.move_data.moves[*moi].source.is_predecessor_of(
@@ -2007,7 +2006,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
20072006
Place::Projection(ref proj) => {
20082007
match proj.elem {
20092008
ProjectionElem::Deref => {
2010-
let base_ty = proj.base.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx);
2009+
let base_ty = proj.base.ty(self.mir, self.infcx.tcx).ty;
20112010

20122011
// Check the kind of deref to decide
20132012
match base_ty.sty {

src/librustc_mir/borrow_check/move_errors.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
266266
// Inspect the type of the content behind the
267267
// borrow to provide feedback about why this
268268
// was a move rather than a copy.
269-
let ty = place.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx);
269+
let ty = place.ty(self.mir, self.infcx.tcx).ty;
270270
let is_upvar_field_projection =
271271
self.prefixes(&original_path, PrefixSet::All)
272272
.any(|p| p.is_upvar_field_projection(self.mir, &self.infcx.tcx)
@@ -530,7 +530,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
530530
// We're only interested in assignments (in particular, where the
531531
// assignment came from - was it an `Rc` or `Arc`?).
532532
if let StatementKind::Assign(_, box Rvalue::Ref(_, _, source)) = &stmt.kind {
533-
let ty = source.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx);
533+
let ty = source.ty(self.mir, self.infcx.tcx).ty;
534534
let ty = match ty.sty {
535535
ty::TyKind::Ref(_, ty, _) => ty,
536536
_ => ty,
@@ -555,7 +555,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
555555
_ => continue,
556556
};
557557

558-
let ty = source.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx);
558+
let ty = source.ty(self.mir, self.infcx.tcx).ty;
559559
let ty = match ty.sty {
560560
ty::TyKind::Ref(_, ty, _) => ty,
561561
_ => ty,
@@ -581,7 +581,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
581581
base,
582582
elem: ProjectionElem::Deref,
583583
}) = place {
584-
if base.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx).is_unsafe_ptr() {
584+
if base.ty(self.mir, self.infcx.tcx).ty.is_unsafe_ptr() {
585585
return BorrowedContentSource::DerefRawPointer;
586586
}
587587
}

0 commit comments

Comments
 (0)