Skip to content

Commit d03b64a

Browse files
committed
add tuple_fields convenience method and use it in a few places
1 parent a902f7a commit d03b64a

File tree

7 files changed

+25
-18
lines changed

7 files changed

+25
-18
lines changed

src/librustc/ty/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,6 +2565,8 @@ impl<'tcx> AdtDef {
25652565
}
25662566

25672567
impl<'tcx> FieldDef {
2568+
/// Returns the type of this field. The `subst` is typically obtained
2569+
/// via the second field of `TyKind::AdtDef`.
25682570
pub fn ty(&self, tcx: TyCtxt<'tcx>, subst: SubstsRef<'tcx>) -> Ty<'tcx> {
25692571
tcx.type_of(self.did).subst(tcx, subst)
25702572
}

src/librustc/ty/sty.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ pub enum TyKind<'tcx> {
171171
Never,
172172

173173
/// A tuple type. For example, `(i32, bool)`.
174+
/// Use `TyS::tuple_fields` to iterate over the field types.
174175
Tuple(SubstsRef<'tcx>),
175176

176177
/// The projection of an associated type. For example,
@@ -1722,8 +1723,8 @@ impl<'tcx> TyS<'tcx> {
17221723
})
17231724
})
17241725
}
1725-
ty::Tuple(tys) => tys.iter().any(|ty| {
1726-
ty.expect_ty().conservative_is_privately_uninhabited(tcx)
1726+
ty::Tuple(..) => self.tuple_fields().any(|ty| {
1727+
ty.conservative_is_privately_uninhabited(tcx)
17271728
}),
17281729
ty::Array(ty, len) => {
17291730
match len.try_eval_usize(tcx, ParamEnv::empty()) {
@@ -2102,6 +2103,15 @@ impl<'tcx> TyS<'tcx> {
21022103
}
21032104
}
21042105

2106+
/// Iterates over tuple fields.
2107+
/// Panics when called on anything but a tuple.
2108+
pub fn tuple_fields(&self) -> impl DoubleEndedIterator<Item=Ty<'tcx>> {
2109+
match self.sty {
2110+
Tuple(substs) => substs.iter().map(|field| field.expect_ty()),
2111+
_ => bug!("tuple_fields called on non-tuple"),
2112+
}
2113+
}
2114+
21052115
/// If the type contains variants, returns the valid range of variant indices.
21062116
/// FIXME This requires the optimized MIR in the case of generators.
21072117
#[inline]

src/librustc/ty/util.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -845,15 +845,15 @@ impl<'tcx> ty::TyS<'tcx> {
845845
ty: Ty<'tcx>,
846846
) -> Representability {
847847
match ty.sty {
848-
Tuple(ref ts) => {
848+
Tuple(..) => {
849849
// Find non representable
850-
fold_repr(ts.iter().map(|ty| {
850+
fold_repr(ty.tuple_fields().map(|ty| {
851851
is_type_structurally_recursive(
852852
tcx,
853853
sp,
854854
seen,
855855
representable_cache,
856-
ty.expect_ty(),
856+
ty,
857857
)
858858
}))
859859
}
@@ -1095,7 +1095,7 @@ fn needs_drop_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>
10951095
// state transformation pass
10961096
ty::Generator(..) => true,
10971097

1098-
ty::Tuple(ref tys) => tys.iter().map(|k| k.expect_ty()).any(needs_drop),
1098+
ty::Tuple(..) => ty.tuple_fields().any(needs_drop),
10991099

11001100
// unions don't have destructors because of the child types,
11011101
// only if they manually implement `Drop` (handled above).

src/librustc/ty/walk.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ fn push_subtypes<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent_ty: Ty<'tcx>) {
119119
ty::GeneratorWitness(ts) => {
120120
stack.extend(ts.skip_binder().iter().cloned().rev());
121121
}
122-
ty::Tuple(ts) => {
123-
stack.extend(ts.iter().map(|k| k.expect_ty()).rev());
122+
ty::Tuple(..) => {
123+
stack.extend(parent_ty.tuple_fields().rev());
124124
}
125125
ty::FnDef(_, substs) => {
126126
stack.extend(substs.types().rev());

src/librustc_lint/builtin.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,14 +1903,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
19031903
_ => true, // Conservative fallback for multi-variant enum.
19041904
}
19051905
}
1906-
Tuple(substs) => {
1906+
Tuple(..) => {
19071907
// Proceed recursively, check all fields.
1908-
substs.iter().all(|field| {
1909-
ty_maybe_allows_zero_init(
1910-
tcx,
1911-
field.expect_ty(),
1912-
)
1913-
})
1908+
ty.tuple_fields().all(|field| ty_maybe_allows_zero_init(tcx, field))
19141909
}
19151910
// FIXME: Would be nice to also warn for `NonNull`/`NonZero*`.
19161911
// Conservative fallback.

src/librustc_mir/shim.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ fn build_clone_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, self_ty: Ty<'tcx>) -
324324
substs.upvar_tys(def_id, tcx)
325325
)
326326
}
327-
ty::Tuple(tys) => builder.tuple_like_shim(dest, src, tys.iter().map(|k| k.expect_ty())),
327+
ty::Tuple(..) => builder.tuple_like_shim(dest, src, self_ty.tuple_fields()),
328328
_ => {
329329
bug!("clone shim for `{:?}` which is not `Copy` and is not an aggregate", self_ty)
330330
}

src/librustc_mir/util/elaborate_drops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,8 @@ where
804804
let tys : Vec<_> = substs.upvar_tys(def_id, self.tcx()).collect();
805805
self.open_drop_for_tuple(&tys)
806806
}
807-
ty::Tuple(tys) => {
808-
let tys: Vec<_> = tys.iter().map(|k| k.expect_ty()).collect();
807+
ty::Tuple(..) => {
808+
let tys: Vec<_> = ty.tuple_fields().collect();
809809
self.open_drop_for_tuple(&tys)
810810
}
811811
ty::Adt(def, substs) => {

0 commit comments

Comments
 (0)