Skip to content

Commit 5412587

Browse files
committed
clean-up adt_sized_constraint now that it uses on-demand
1 parent 23de823 commit 5412587

File tree

3 files changed

+21
-49
lines changed

3 files changed

+21
-49
lines changed

src/librustc/traits/select.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,11 +1790,9 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
17901790
ty::TyAdt(def, substs) => {
17911791
let sized_crit = def.sized_constraint(self.tcx());
17921792
// (*) binder moved here
1793-
Where(ty::Binder(match sized_crit.sty {
1794-
ty::TyTuple(tys, _) => tys.to_vec().subst(self.tcx(), substs),
1795-
ty::TyBool => vec![],
1796-
_ => vec![sized_crit.subst(self.tcx(), substs)]
1797-
}))
1793+
Where(ty::Binder(
1794+
sized_crit.iter().map(|ty| ty.subst(self.tcx(), substs)).collect()
1795+
))
17981796
}
17991797

18001798
ty::TyProjection(_) | ty::TyParam(_) | ty::TyAnon(..) => None,

src/librustc/ty/maps.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ define_maps! { <'tcx>
396396
pub trait_def: ItemSignature(DefId) -> &'tcx ty::TraitDef,
397397
pub adt_def: ItemSignature(DefId) -> &'tcx ty::AdtDef,
398398
pub adt_destructor: AdtDestructor(DefId) -> Option<ty::Destructor>,
399-
pub adt_sized_constraint: SizedConstraint(DefId) -> Ty<'tcx>,
399+
pub adt_sized_constraint: SizedConstraint(DefId) -> &'tcx [Ty<'tcx>],
400400

401401
/// True if this is a foreign item (i.e., linked via `extern { ... }`).
402402
pub is_foreign_item: IsForeignItem(DefId) -> bool,

src/librustc/ty/mod.rs

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,33 +1780,26 @@ impl<'a, 'gcx, 'tcx> AdtDef {
17801780
queries::adt_destructor::get(tcx, DUMMY_SP, self.did)
17811781
}
17821782

1783-
/// Returns a simpler type such that `Self: Sized` if and only
1783+
/// Returns a list of types such that `Self: Sized` if and only
17841784
/// if that type is Sized, or `TyErr` if this type is recursive.
17851785
///
1786-
/// HACK: instead of returning a list of types, this function can
1787-
/// return a tuple. In that case, the result is Sized only if
1788-
/// all elements of the tuple are Sized.
1789-
///
1790-
/// This is generally the `struct_tail` if this is a struct, or a
1791-
/// tuple of them if this is an enum.
1792-
///
17931786
/// Oddly enough, checking that the sized-constraint is Sized is
17941787
/// actually more expressive than checking all members:
17951788
/// the Sized trait is inductive, so an associated type that references
17961789
/// Self would prevent its containing ADT from being Sized.
17971790
///
17981791
/// Due to normalization being eager, this applies even if
17991792
/// the associated type is behind a pointer, e.g. issue #31299.
1800-
pub fn sized_constraint(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
1793+
pub fn sized_constraint(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> &'tcx [Ty<'tcx>] {
18011794
match queries::adt_sized_constraint::try_get(tcx, DUMMY_SP, self.did) {
1802-
Ok(ty) => ty,
1795+
Ok(tys) => tys,
18031796
Err(_) => {
18041797
debug!("adt_sized_constraint: {:?} is recursive", self);
18051798
// This should be reported as an error by `check_representable`.
18061799
//
18071800
// Consider the type as Sized in the meanwhile to avoid
18081801
// further errors.
1809-
tcx.types.err
1802+
tcx.intern_type_list(&[tcx.types.err])
18101803
}
18111804
}
18121805
}
@@ -1836,18 +1829,13 @@ impl<'a, 'gcx, 'tcx> AdtDef {
18361829

18371830
TyAdt(adt, substs) => {
18381831
// recursive case
1839-
let adt_ty =
1840-
adt.sized_constraint(tcx)
1841-
.subst(tcx, substs);
1832+
let adt_tys = adt.sized_constraint(tcx);
18421833
debug!("sized_constraint_for_ty({:?}) intermediate = {:?}",
1843-
ty, adt_ty);
1844-
if let ty::TyTuple(ref tys, _) = adt_ty.sty {
1845-
tys.iter().flat_map(|ty| {
1846-
self.sized_constraint_for_ty(tcx, ty)
1847-
}).collect()
1848-
} else {
1849-
self.sized_constraint_for_ty(tcx, adt_ty)
1850-
}
1834+
ty, adt_tys);
1835+
adt_tys.iter()
1836+
.map(|ty| ty.subst(tcx, substs))
1837+
.flat_map(|ty| self.sized_constraint_for_ty(tcx, ty))
1838+
.collect()
18511839
}
18521840

18531841
TyProjection(..) | TyAnon(..) => {
@@ -2697,13 +2685,7 @@ fn associated_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
26972685

26982686
/// Calculates the Sized-constraint.
26992687
///
2700-
/// As the Sized-constraint of enums can be a *set* of types,
2701-
/// the Sized-constraint may need to be a set also. Because introducing
2702-
/// a new type of IVar is currently a complex affair, the Sized-constraint
2703-
/// may be a tuple.
2704-
///
2705-
/// In fact, there are only a few options for the constraint:
2706-
/// - `bool`, if the type is always Sized
2688+
/// In fact, there are only a few options for the types in the constraint:
27072689
/// - an obviously-unsized type
27082690
/// - a type parameter or projection whose Sizedness can't be known
27092691
/// - a tuple of type parameters or projections, if there are multiple
@@ -2712,26 +2694,18 @@ fn associated_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
27122694
/// check should catch this case.
27132695
fn adt_sized_constraint<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
27142696
def_id: DefId)
2715-
-> Ty<'tcx> {
2697+
-> &'tcx [Ty<'tcx>] {
27162698
let def = tcx.lookup_adt_def(def_id);
27172699

2718-
let tys: Vec<_> = def.variants.iter().flat_map(|v| {
2700+
let result = tcx.intern_type_list(&def.variants.iter().flat_map(|v| {
27192701
v.fields.last()
27202702
}).flat_map(|f| {
2721-
let ty = tcx.item_type(f.did);
2722-
def.sized_constraint_for_ty(tcx, ty)
2723-
}).collect();
2724-
2725-
let ty = match tys.len() {
2726-
_ if tys.references_error() => tcx.types.err,
2727-
0 => tcx.types.bool,
2728-
1 => tys[0],
2729-
_ => tcx.intern_tup(&tys[..], false)
2730-
};
2703+
def.sized_constraint_for_ty(tcx, tcx.item_type(f.did))
2704+
}).collect::<Vec<_>>());
27312705

2732-
debug!("adt_sized_constraint: {:?} => {:?}", def, ty);
2706+
debug!("adt_sized_constraint: {:?} => {:?}", def, result);
27332707

2734-
ty
2708+
result
27352709
}
27362710

27372711
fn associated_item_def_ids<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

0 commit comments

Comments
 (0)