Skip to content

Commit

Permalink
Rollup merge of rust-lang#60292 - varkor:ty-tuple-substs, r=nikomatsakis
Browse files Browse the repository at this point in the history
Replace the `&'tcx List<Ty<'tcx>>` in `TyKind::Tuple` with `SubstsRef<'tcx>`

Part of the suggested refactoring for rust-lang#42340. As expected, this is a little messy, because there are many places that the components of tuples are expected to be types, rather than arbitrary kinds. However, it should open up the way for a refactoring of `TyS` itself.

r? @nikomatsakis
  • Loading branch information
Centril authored Apr 27, 2019
2 parents d4a32d5 + a3470c6 commit 04cf752
Show file tree
Hide file tree
Showing 41 changed files with 146 additions and 93 deletions.
2 changes: 1 addition & 1 deletion src/librustc/mir/tcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
let field_def = &variant_def.fields[f.index()];
field_def.ty(tcx, substs)
}
ty::Tuple(ref tys) => tys[f.index()],
ty::Tuple(ref tys) => tys[f.index()].expect_ty(),
_ => bug!("extracting field of non-tuple non-adt: {:?}", self),
};
debug!("field_ty self: {:?} f: {:?} yields: {:?}", self, f, answer);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
let expected_ty = expected_trait_ref.skip_binder().substs.type_at(1);
let expected = match expected_ty.sty {
ty::Tuple(ref tys) => tys.iter()
.map(|t| ArgKind::from_expected_ty(t, Some(span))).collect(),
.map(|t| ArgKind::from_expected_ty(t.expect_ty(), Some(span))).collect(),
_ => vec![ArgKind::Arg("_".to_owned(), expected_ty.to_string())],
};

Expand Down Expand Up @@ -1247,7 +1247,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
let inputs = trait_ref.substs.type_at(1);
let sig = if let ty::Tuple(inputs) = inputs.sty {
tcx.mk_fn_sig(
inputs.iter().cloned(),
inputs.iter().map(|k| k.expect_ty()),
tcx.mk_infer(ty::TyVar(ty::TyVid { index: 0 })),
false,
hir::Unsafety::Normal,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/query/dropck_outlives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub fn trivial_dropck_outlives<'tcx>(tcx: TyCtxt<'_, '_, 'tcx>, ty: Ty<'tcx>) ->

// (T1..Tn) and closures have same properties as T1..Tn --
// check if *any* of those are trivial.
ty::Tuple(ref tys) => tys.iter().all(|t| trivial_dropck_outlives(tcx, t)),
ty::Tuple(ref tys) => tys.iter().all(|t| trivial_dropck_outlives(tcx, t.expect_ty())),
ty::Closure(def_id, ref substs) => substs
.upvar_tys(def_id, tcx)
.all(|t| trivial_dropck_outlives(tcx, t)),
Expand Down
14 changes: 9 additions & 5 deletions src/librustc/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2429,7 +2429,9 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {

ty::Str | ty::Slice(_) | ty::Dynamic(..) | ty::Foreign(..) => None,

ty::Tuple(tys) => Where(ty::Binder::bind(tys.last().into_iter().cloned().collect())),
ty::Tuple(tys) => {
Where(ty::Binder::bind(tys.last().into_iter().map(|k| k.expect_ty()).collect()))
}

ty::Adt(def, substs) => {
let sized_crit = def.sized_constraint(self.tcx());
Expand Down Expand Up @@ -2503,7 +2505,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {

ty::Tuple(tys) => {
// (*) binder moved here
Where(ty::Binder::bind(tys.to_vec()))
Where(ty::Binder::bind(tys.iter().map(|k| k.expect_ty()).collect()))
}

ty::Closure(def_id, substs) => {
Expand Down Expand Up @@ -2590,7 +2592,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {

ty::Tuple(ref tys) => {
// (T1, ..., Tn) -- meets any bound that all of T1...Tn meet
tys.to_vec()
tys.iter().map(|k| k.expect_ty()).collect()
}

ty::Closure(def_id, ref substs) => substs.upvar_tys(def_id, self.tcx()).collect(),
Expand Down Expand Up @@ -3495,7 +3497,9 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {

// Check that the source tuple with the target's
// last element is equal to the target.
let new_tuple = tcx.mk_tup(a_mid.iter().cloned().chain(iter::once(b_last)));
let new_tuple = tcx.mk_tup(
a_mid.iter().map(|k| k.expect_ty()).chain(iter::once(b_last.expect_ty())),
);
let InferOk { obligations, .. } = self.infcx
.at(&obligation.cause, obligation.param_env)
.eq(target, new_tuple)
Expand All @@ -3508,7 +3512,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
obligation.cause.clone(),
obligation.predicate.def_id(),
obligation.recursion_depth + 1,
a_last,
a_last.expect_ty(),
&[b_last.into()],
));
}
Expand Down
10 changes: 7 additions & 3 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2431,7 +2431,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
let converted_sig = sig.map_bound(|s| {
let params_iter = match s.inputs()[0].sty {
ty::Tuple(params) => {
params.into_iter().cloned()
params.into_iter().map(|k| k.expect_ty())
}
_ => bug!(),
};
Expand Down Expand Up @@ -2573,11 +2573,15 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

#[inline]
pub fn intern_tup(self, ts: &[Ty<'tcx>]) -> Ty<'tcx> {
self.mk_ty(Tuple(self.intern_type_list(ts)))
let kinds: Vec<_> = ts.into_iter().map(|&t| Kind::from(t)).collect();
self.mk_ty(Tuple(self.intern_substs(&kinds)))
}

pub fn mk_tup<I: InternAs<[Ty<'tcx>], Ty<'tcx>>>(self, iter: I) -> I::Output {
iter.intern_with(|ts| self.mk_ty(Tuple(self.intern_type_list(ts))))
iter.intern_with(|ts| {
let kinds: Vec<_> = ts.into_iter().map(|&t| Kind::from(t)).collect();
self.mk_ty(Tuple(self.intern_substs(&kinds)))
})
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ impl FlagComputation {
self.add_ty(ty);
}

&ty::Tuple(ref ts) => {
self.add_tys(&ts[..]);
&ty::Tuple(ref substs) => {
self.add_substs(substs);
}

&ty::FnDef(_, substs) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/inhabitedness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {

Tuple(ref tys) => {
DefIdForest::union(tcx, tys.iter().map(|ty| {
ty.uninhabited_from(tcx)
ty.expect_ty().uninhabited_from(tcx)
}))
}

Expand Down
7 changes: 4 additions & 3 deletions src/librustc/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,9 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
StructKind::MaybeUnsized
};

univariant(&tys.iter().map(|ty| self.layout_of(ty)).collect::<Result<Vec<_>, _>>()?,
&ReprOptions::default(), kind)?
univariant(&tys.iter().map(|k| {
self.layout_of(k.expect_ty())
}).collect::<Result<Vec<_>, _>>()?, &ReprOptions::default(), kind)?
}

// SIMD vector types.
Expand Down Expand Up @@ -1723,7 +1724,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx>
substs.field_tys(def_id, tcx).nth(i).unwrap()
}

ty::Tuple(tys) => tys[i],
ty::Tuple(tys) => tys[i].expect_ty(),

// SIMD vector types.
ty::Adt(def, ..) if def.repr.simd() => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2501,7 +2501,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
Tuple(ref tys) => {
match tys.last() {
None => vec![],
Some(ty) => self.sized_constraint_for_ty(tcx, ty)
Some(ty) => self.sized_constraint_for_ty(tcx, ty.expect_ty()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/print/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
ty::Ref(_, ty, _) => characteristic_def_id_of_type(ty),

ty::Tuple(ref tys) => tys.iter()
.filter_map(|ty| characteristic_def_id_of_type(ty))
.filter_map(|ty| characteristic_def_id_of_type(ty.expect_ty()))
.next(),

ty::FnDef(def_id, _) |
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,8 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
if let ty::Tuple(ref args) = principal.substs.type_at(0).sty {
let mut projections = predicates.projection_bounds();
if let (Some(proj), None) = (projections.next(), projections.next()) {
p!(pretty_fn_sig(args, false, proj.ty));
let tys: Vec<_> = args.iter().map(|k| k.expect_ty()).collect();
p!(pretty_fn_sig(&tys, false, proj.ty));
resugared = true;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/librustc/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,9 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
(&ty::Tuple(as_), &ty::Tuple(bs)) =>
{
if as_.len() == bs.len() {
Ok(tcx.mk_tup(as_.iter().zip(bs).map(|(a, b)| relation.relate(a, b)))?)
Ok(tcx.mk_tup(as_.iter().zip(bs).map(|(a, b)| {
relation.relate(&a.expect_ty(), &b.expect_ty())
}))?)
} else if !(as_.is_empty() || bs.is_empty()) {
Err(TypeError::TupleSize(
expected_found(relation, &as_.len(), &bs.len())))
Expand Down
11 changes: 7 additions & 4 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub enum TyKind<'tcx> {
Never,

/// A tuple type. For example, `(i32, bool)`.
Tuple(&'tcx List<Ty<'tcx>>),
Tuple(SubstsRef<'tcx>),

/// The projection of an associated type. For example,
/// `<T as Trait<..>>::N`.
Expand Down Expand Up @@ -1651,7 +1651,9 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
})
})
}
ty::Tuple(tys) => tys.iter().any(|ty| ty.conservative_is_privately_uninhabited(tcx)),
ty::Tuple(tys) => tys.iter().any(|ty| {
ty.expect_ty().conservative_is_privately_uninhabited(tcx)
}),
ty::Array(ty, len) => {
match len.assert_usize(tcx) {
// If the array is definitely non-empty, it's uninhabited if
Expand Down Expand Up @@ -2087,8 +2089,9 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
ty::Str | ty::Slice(_) | ty::Dynamic(..) | ty::Foreign(..) =>
false,

ty::Tuple(tys) =>
tys.iter().all(|ty| ty.is_trivially_sized(tcx)),
ty::Tuple(tys) => {
tys.iter().all(|ty| ty.expect_ty().is_trivially_sized(tcx))
}

ty::Adt(def, _substs) =>
def.sized_constraint(tcx).is_empty(),
Expand Down
13 changes: 11 additions & 2 deletions src/librustc/ty/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ impl<'tcx> Kind<'tcx> {
}
}
}

/// Unpack the `Kind` as a type when it is known certainly to be a type.
/// This is true in cases where `Substs` is used in places where the kinds are known
/// to be limited (e.g. in tuples, where the only parameters are type parameters).
pub fn expect_ty(self) -> Ty<'tcx> {
match self.unpack() {
UnpackedKind::Type(ty) => ty,
_ => bug!("expected a type, but found another kind"),
}
}
}

impl<'a, 'tcx> Lift<'tcx> for Kind<'a> {
Expand Down Expand Up @@ -174,8 +184,7 @@ pub type SubstsRef<'tcx> = &'tcx InternalSubsts<'tcx>;

impl<'a, 'gcx, 'tcx> InternalSubsts<'tcx> {
/// Creates a `InternalSubsts` that maps each generic parameter to itself.
pub fn identity_for_item(tcx: TyCtxt<'a, 'gcx, 'tcx>, def_id: DefId)
-> SubstsRef<'tcx> {
pub fn identity_for_item(tcx: TyCtxt<'a, 'gcx, 'tcx>, def_id: DefId) -> SubstsRef<'tcx> {
Self::for_item(tcx, def_id, |param, _| {
tcx.mk_param_from_def(param)
})
Expand Down
16 changes: 11 additions & 5 deletions src/librustc/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

ty::Tuple(tys) => {
if let Some((&last_ty, _)) = tys.split_last() {
ty = last_ty;
ty = last_ty.expect_ty();
} else {
break;
}
Expand Down Expand Up @@ -316,8 +316,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
(&Tuple(a_tys), &Tuple(b_tys))
if a_tys.len() == b_tys.len() => {
if let Some(a_last) = a_tys.last() {
a = a_last;
b = b_tys.last().unwrap();
a = a_last.expect_ty();
b = b_tys.last().unwrap().expect_ty();
} else {
break;
}
Expand Down Expand Up @@ -795,7 +795,13 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
Tuple(ref ts) => {
// Find non representable
fold_repr(ts.iter().map(|ty| {
is_type_structurally_recursive(tcx, sp, seen, representable_cache, ty)
is_type_structurally_recursive(
tcx,
sp,
seen,
representable_cache,
ty.expect_ty(),
)
}))
}
// Fixed-length vectors.
Expand Down Expand Up @@ -1048,7 +1054,7 @@ fn needs_drop_raw<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
// state transformation pass
ty::Generator(..) => true,

ty::Tuple(ref tys) => tys.iter().cloned().any(needs_drop),
ty::Tuple(ref tys) => tys.iter().map(|k| k.expect_ty()).any(needs_drop),

// unions don't have destructors because of the child types,
// only if they manually implement `Drop` (handled above).
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn push_subtypes<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent_ty: Ty<'tcx>) {
stack.extend(ts.skip_binder().iter().cloned().rev());
}
ty::Tuple(ts) => {
stack.extend(ts.iter().cloned().rev());
stack.extend(ts.iter().map(|k| k.expect_ty()).rev());
}
ty::FnDef(_, substs) => {
stack.extend(substs.types().rev());
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/wf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl<'a, 'gcx, 'tcx> WfPredicates<'a, 'gcx, 'tcx> {
ty::Tuple(ref tys) => {
if let Some((_last, rest)) = tys.split_last() {
for elem in rest {
self.require_sized(elem, traits::TupleElem);
self.require_sized(elem.expect_ty(), traits::TupleElem);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_codegen_llvm/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,9 @@ impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
assert!(!sig.c_variadic && extra_args.is_empty());

match sig.inputs().last().unwrap().sty {
ty::Tuple(ref tupled_arguments) => {
ty::Tuple(tupled_arguments) => {
inputs = &sig.inputs()[0..sig.inputs().len() - 1];
tupled_arguments
tupled_arguments.iter().map(|k| k.expect_ty()).collect()
}
_ => {
bug!("argument to function with \"rust-call\" ABI \
Expand All @@ -437,7 +437,7 @@ impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
}
} else {
assert!(sig.c_variadic || extra_args.is_empty());
extra_args
extra_args.to_vec()
};

let target = &cx.sess().target.target;
Expand Down Expand Up @@ -587,7 +587,7 @@ impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {

let mut fn_ty = FnType {
ret: arg_of(sig.output(), None),
args: inputs.iter().chain(extra_args).enumerate().map(|(i, ty)| {
args: inputs.iter().cloned().chain(extra_args).enumerate().map(|(i, ty)| {
arg_of(ty, Some(i))
}).collect(),
c_variadic: sig.c_variadic,
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_codegen_llvm/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,9 +722,10 @@ pub fn type_metadata(
}
},
ty::Tuple(ref elements) => {
let tys: Vec<_> = elements.iter().map(|k| k.expect_ty()).collect();
prepare_tuple_metadata(cx,
t,
&elements[..],
&tys,
unique_type_id,
usage_site_span).finalize(cx)
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
if let ty::Tuple(args) = sig.inputs()[sig.inputs().len() - 1].sty {
signature.extend(
args.iter().map(|argument_type| {
Some(type_metadata(cx, argument_type, syntax_pos::DUMMY_SP))
Some(type_metadata(cx, argument_type.expect_ty(), syntax_pos::DUMMY_SP))
})
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/debuginfo/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
ty::Tuple(component_types) => {
output.push('(');
for &component_type in component_types {
push_debuginfo_type_name(tcx, component_type, true, output, visited);
push_debuginfo_type_name(tcx, component_type.expect_ty(), true, output, visited);
output.push_str(", ");
}
if !component_types.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2213,7 +2213,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
// Closure arguments are wrapped in a tuple, so we need to get the first
// from that.
if let ty::Tuple(elems) = argument_ty.sty {
let argument_ty = elems.first()?;
let argument_ty = elems.first()?.expect_ty();
if let ty::Ref(_, _, _) = argument_ty.sty {
return Some(AnnotatedBorrowFnSignature::Closure {
argument_ty,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// just worry about trying to match up the rustc type
// with the HIR types:
(ty::Tuple(elem_tys), hir::TyKind::Tup(elem_hir_tys)) => {
search_stack.extend(elem_tys.iter().cloned().zip(elem_hir_tys));
search_stack.extend(elem_tys.iter().map(|k| k.expect_ty()).zip(elem_hir_tys));
}

(ty::Slice(elem_ty), hir::TyKind::Slice(elem_hir_ty))
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/nll/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
}
ty::Tuple(tys) => {
return match tys.get(field.index()) {
Some(&ty) => Ok(ty),
Some(&ty) => Ok(ty.expect_ty()),
None => Err(FieldAccessError::OutOfRange {
field_count: tys.len(),
}),
Expand Down
Loading

0 comments on commit 04cf752

Please sign in to comment.