Skip to content

Commit f4a1628

Browse files
committed
Remove adt_def from PlaceTy
1 parent cf5fd22 commit f4a1628

30 files changed

+112
-118
lines changed

src/librustc/mir/tcx.rs

+29-35
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
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;
@@ -16,8 +16,7 @@ pub enum PlaceTy<'tcx> {
1616
Ty { ty: Ty<'tcx> },
1717

1818
/// Downcast to a particular variant of an enum.
19-
Downcast { adt_def: &'tcx AdtDef,
20-
substs: SubstsRef<'tcx>,
19+
Downcast { ty: Ty<'tcx>,
2120
variant_index: VariantIdx },
2221
}
2322

@@ -30,12 +29,10 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
3029
PlaceTy::Ty { ty }
3130
}
3231

33-
pub fn to_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
32+
pub fn to_ty(&self) -> Ty<'tcx> {
3433
match *self {
35-
PlaceTy::Ty { ty } =>
36-
ty,
37-
PlaceTy::Downcast { adt_def, substs, variant_index: _ } =>
38-
tcx.mk_adt(adt_def, substs),
34+
PlaceTy::Ty { ty } => ty,
35+
PlaceTy::Downcast { ty, variant_index: _ } => ty,
3936
}
4037
}
4138

@@ -48,21 +45,18 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
4845
/// Note that the resulting type has not been normalized.
4946
pub fn field_ty(self, tcx: TyCtxt<'a, 'gcx, 'tcx>, f: &Field) -> Ty<'tcx>
5047
{
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 }, _) => {
48+
let answer = match self.to_ty().sty {
49+
ty::TyKind::Adt(adt_def, substs) => {
50+
let variant_index = match &self {
51+
PlaceTy::Ty { .. } => VariantIdx::new(0),
52+
PlaceTy::Downcast { ty: _, variant_index } => *variant_index,
53+
};
5654
let variant_def = &adt_def.variants[variant_index];
5755
let field_def = &variant_def.fields[f.index()];
5856
field_def.ty(tcx, substs)
5957
}
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-
}
58+
ty::Tuple(ref tys) => tys[f.index()],
59+
_ => bug!("extracting field of non-tuple non-adt: {:?}", self),
6660
};
6761
debug!("field_ty self: {:?} f: {:?} yields: {:?}", self, f, answer);
6862
answer
@@ -94,7 +88,7 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
9488
{
9589
let answer = match *elem {
9690
ProjectionElem::Deref => {
97-
let ty = self.to_ty(tcx)
91+
let ty = self.to_ty()
9892
.builtin_deref(true)
9993
.unwrap_or_else(|| {
10094
bug!("deref projection of non-dereferencable ty {:?}", self)
@@ -106,10 +100,10 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
106100
}
107101
ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } =>
108102
PlaceTy::Ty {
109-
ty: self.to_ty(tcx).builtin_index().unwrap()
103+
ty: self.to_ty().builtin_index().unwrap()
110104
},
111105
ProjectionElem::Subslice { from, to } => {
112-
let ty = self.to_ty(tcx);
106+
let ty = self.to_ty();
113107
PlaceTy::Ty {
114108
ty: match ty.sty {
115109
ty::Array(inner, size) => {
@@ -124,19 +118,19 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
124118
}
125119
}
126120
}
127-
ProjectionElem::Downcast(_name, index) =>
128-
match self.to_ty(tcx).sty {
129-
ty::Adt(adt_def, substs) => {
121+
ProjectionElem::Downcast(_name, index) => {
122+
let ty = self.to_ty();
123+
match &ty.sty {
124+
ty::Adt(adt_def, _substs) => {
130125
assert!(adt_def.is_enum());
131126
assert!(index.as_usize() < adt_def.variants.len());
132-
PlaceTy::Downcast { adt_def,
133-
substs,
134-
variant_index: index }
135127
}
136128
_ => {
137129
bug!("cannot downcast non-ADT type: `{:?}`", self)
138130
}
139-
},
131+
};
132+
PlaceTy::Downcast { ty, variant_index: index }
133+
}
140134
ProjectionElem::Field(ref f, ref fty) =>
141135
PlaceTy::Ty { ty: handle_field(&self, f, fty) },
142136
};
@@ -148,7 +142,7 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
148142
EnumTypeFoldableImpl! {
149143
impl<'tcx> TypeFoldable<'tcx> for PlaceTy<'tcx> {
150144
(PlaceTy::Ty) { ty },
151-
(PlaceTy::Downcast) { adt_def, substs, variant_index },
145+
(PlaceTy::Downcast) { ty, variant_index },
152146
}
153147
}
154148

@@ -185,7 +179,7 @@ impl<'tcx> Place<'tcx> {
185179
match place {
186180
Place::Projection(ref proj) => match proj.elem {
187181
ProjectionElem::Field(field, _ty) => {
188-
let base_ty = proj.base.ty(mir, *tcx).to_ty(*tcx);
182+
let base_ty = proj.base.ty(mir, *tcx).to_ty();
189183

190184
if (base_ty.is_closure() || base_ty.is_generator()) &&
191185
(!by_ref || mir.upvar_decls[field.index()].by_ref)
@@ -217,7 +211,7 @@ impl<'tcx> Rvalue<'tcx> {
217211
tcx.mk_array(operand.ty(local_decls, tcx), count)
218212
}
219213
Rvalue::Ref(reg, bk, ref place) => {
220-
let place_ty = place.ty(local_decls, tcx).to_ty(tcx);
214+
let place_ty = place.ty(local_decls, tcx).to_ty();
221215
tcx.mk_ref(reg,
222216
ty::TypeAndMut {
223217
ty: place_ty,
@@ -243,7 +237,7 @@ impl<'tcx> Rvalue<'tcx> {
243237
operand.ty(local_decls, tcx)
244238
}
245239
Rvalue::Discriminant(ref place) => {
246-
let ty = place.ty(local_decls, tcx).to_ty(tcx);
240+
let ty = place.ty(local_decls, tcx).to_ty();
247241
if let ty::Adt(adt_def, _) = ty.sty {
248242
adt_def.repr.discr_type().to_ty(tcx)
249243
} else {
@@ -292,7 +286,7 @@ impl<'tcx> Operand<'tcx> {
292286
{
293287
match self {
294288
&Operand::Copy(ref l) |
295-
&Operand::Move(ref l) => l.ty(local_decls, tcx).to_ty(tcx),
289+
&Operand::Move(ref l) => l.ty(local_decls, tcx).to_ty(),
296290
&Operand::Constant(ref c) => c.ty,
297291
}
298292
}

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+
.to_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.to_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.to_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
@@ -305,7 +305,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
305305
target: mir::BasicBlock,
306306
unwind: Option<mir::BasicBlock>,
307307
) {
308-
let ty = location.ty(self.mir, bx.tcx()).to_ty(bx.tcx());
308+
let ty = location.ty(self.mir, bx.tcx()).to_ty();
309309
let ty = self.monomorphize(&ty);
310310
let drop_fn = monomorphize::resolve_drop_in_place(bx.tcx(), ty);
311311

src/librustc_codegen_ssa/mir/place.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
490490
let mut subslice = cg_base.project_index(bx,
491491
bx.cx().const_usize(from as u64));
492492
let projected_ty = PlaceTy::Ty { ty: cg_base.layout.ty }
493-
.projection_ty(tcx, &projection.elem).to_ty(tcx);
493+
.projection_ty(tcx, &projection.elem).to_ty();
494494
subslice.layout = bx.cx().layout_of(self.monomorphize(&projected_ty));
495495

496496
if subslice.layout.is_unsized() {
@@ -518,6 +518,6 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
518518
pub fn monomorphized_place_ty(&self, place: &mir::Place<'tcx>) -> Ty<'tcx> {
519519
let tcx = self.cx.tcx();
520520
let place_ty = place.ty(self.mir, tcx);
521-
self.monomorphize(&place_ty.to_ty(tcx))
521+
self.monomorphize(&place_ty.to_ty())
522522
}
523523
}

src/librustc_mir/borrow_check/error_reporting.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
188188
);
189189
}
190190

191-
let ty = used_place.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx);
191+
let ty = used_place.ty(self.mir, self.infcx.tcx).to_ty();
192192
let needs_note = match ty.sty {
193193
ty::Closure(id, _) => {
194194
let tables = self.infcx.tcx.typeck_tables_of(id);
@@ -203,7 +203,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
203203
let mpi = self.move_data.moves[move_out_indices[0]].path;
204204
let place = &self.move_data.move_paths[mpi].place;
205205

206-
let ty = place.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx);
206+
let ty = place.ty(self.mir, self.infcx.tcx).to_ty();
207207
let opt_name = self.describe_place_with_options(place, IncludingDowncast(true));
208208
let note_msg = match opt_name {
209209
Some(ref name) => format!("`{}`", name),
@@ -580,7 +580,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
580580
// is a union.
581581
let is_union = |place: &Place<'tcx>| -> bool {
582582
place.ty(self.mir, self.infcx.tcx)
583-
.to_ty(self.infcx.tcx)
583+
.to_ty()
584584
.ty_adt_def()
585585
.map(|adt| adt.is_union())
586586
.unwrap_or(false)
@@ -629,7 +629,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
629629

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

634634
return Some((desc_base, desc_first, desc_second, ty.to_string()));
635635
},
@@ -1839,15 +1839,15 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
18391839
StorageDeadOrDrop::LocalStorageDead
18401840
| StorageDeadOrDrop::BoxedStorageDead => {
18411841
assert!(
1842-
base.ty(self.mir, tcx).to_ty(tcx).is_box(),
1842+
base.ty(self.mir, tcx).to_ty().is_box(),
18431843
"Drop of value behind a reference or raw pointer"
18441844
);
18451845
StorageDeadOrDrop::BoxedStorageDead
18461846
}
18471847
StorageDeadOrDrop::Destructor(_) => base_access,
18481848
},
18491849
ProjectionElem::Field(..) | ProjectionElem::Downcast(..) => {
1850-
let base_ty = base.ty(self.mir, tcx).to_ty(tcx);
1850+
let base_ty = base.ty(self.mir, tcx).to_ty();
18511851
match base_ty.sty {
18521852
ty::Adt(def, _) if def.has_dtor(tcx) => {
18531853
// Report the outermost adt with a destructor

src/librustc_mir/borrow_check/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ impl<'cx, 'gcx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx
617617

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

622622
// "Lift" into the gcx -- once regions are erased, this type should be in the
623623
// global arenas; this "lift" operation basically just asserts that is true, but
@@ -1641,7 +1641,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
16411641
// assigning to `P.f` requires `P` itself
16421642
// be already initialized
16431643
let tcx = self.infcx.tcx;
1644-
match base.ty(self.mir, tcx).to_ty(tcx).sty {
1644+
match base.ty(self.mir, tcx).to_ty().sty {
16451645
ty::Adt(def, _) if def.has_dtor(tcx) => {
16461646
self.check_if_path_or_subpath_is_moved(
16471647
context, InitializationRequiringAction::Assignment,
@@ -1746,7 +1746,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
17461746
// no move out from an earlier location) then this is an attempt at initialization
17471747
// of the union - we should error in that case.
17481748
let tcx = this.infcx.tcx;
1749-
if let ty::TyKind::Adt(def, _) = base.ty(this.mir, tcx).to_ty(tcx).sty {
1749+
if let ty::TyKind::Adt(def, _) = base.ty(this.mir, tcx).to_ty().sty {
17501750
if def.is_union() {
17511751
if this.move_data.path_map[mpi].iter().any(|moi| {
17521752
this.move_data.moves[*moi].source.is_predecessor_of(
@@ -2007,7 +2007,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
20072007
Place::Projection(ref proj) => {
20082008
match proj.elem {
20092009
ProjectionElem::Deref => {
2010-
let base_ty = proj.base.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx);
2010+
let base_ty = proj.base.ty(self.mir, self.infcx.tcx).to_ty();
20112011

20122012
// Check the kind of deref to decide
20132013
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).to_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).to_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).to_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).to_ty().is_unsafe_ptr() {
585585
return BorrowedContentSource::DerefRawPointer;
586586
}
587587
}

src/librustc_mir/borrow_check/mutability_errors.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
6464
elem: ProjectionElem::Field(upvar_index, _),
6565
}) => {
6666
debug_assert!(is_closure_or_generator(
67-
base.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx)
67+
base.ty(self.mir, self.infcx.tcx).to_ty()
6868
));
6969

7070
item_msg = format!("`{}`", access_place_desc.unwrap());
@@ -85,7 +85,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
8585
item_msg = format!("`{}`", access_place_desc.unwrap());
8686
debug_assert!(self.mir.local_decls[Local::new(1)].ty.is_region_ptr());
8787
debug_assert!(is_closure_or_generator(
88-
the_place_err.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx)
88+
the_place_err.ty(self.mir, self.infcx.tcx).to_ty()
8989
));
9090

9191
reason = if access_place.is_upvar_field_projection(self.mir,
@@ -110,7 +110,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
110110
reason = ", as it is immutable for the pattern guard".to_string();
111111
} else {
112112
let pointer_type =
113-
if base.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx).is_region_ptr() {
113+
if base.ty(self.mir, self.infcx.tcx).to_ty().is_region_ptr() {
114114
"`&` reference"
115115
} else {
116116
"`*const` pointer"
@@ -232,7 +232,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
232232

233233
if let Some((span, message)) = annotate_struct_field(
234234
self.infcx.tcx,
235-
base.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx),
235+
base.ty(self.mir, self.infcx.tcx).to_ty(),
236236
field,
237237
) {
238238
err.span_suggestion(
@@ -304,7 +304,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
304304
elem: ProjectionElem::Field(upvar_index, _),
305305
}) => {
306306
debug_assert!(is_closure_or_generator(
307-
base.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx)
307+
base.ty(self.mir, self.infcx.tcx).to_ty()
308308
));
309309

310310
err.span_label(span, format!("cannot {ACT}", ACT = act));

0 commit comments

Comments
 (0)