Added ExactSizeIterator bound to return types

in librustc in several places
This commit is contained in:
Bartłomiej Kuras 2019-12-07 19:37:09 +01:00
parent d0126e8ed3
commit d97379a96e
19 changed files with 61 additions and 35 deletions

View File

@ -1274,7 +1274,7 @@ fn remove_dir(&self, dir: &Path) {
t!(fs::remove_dir_all(dir))
}
fn read_dir(&self, dir: &Path) -> impl Iterator<Item=fs::DirEntry> {
fn read_dir(&self, dir: &Path) -> impl Iterator<Item=fs::DirEntry> + ExactSizeIterator {
let iter = match fs::read_dir(dir) {
Ok(v) => v,
Err(_) if self.config.dry_run => return vec![].into_iter(),

View File

@ -574,7 +574,7 @@ fn query_outlives_constraints_into_obligations<'a>(
param_env: ty::ParamEnv<'tcx>,
unsubstituted_region_constraints: &'a [QueryOutlivesConstraint<'tcx>],
result_subst: &'a CanonicalVarValues<'tcx>,
) -> impl Iterator<Item = PredicateObligation<'tcx>> + 'a + Captures<'tcx> {
) -> impl Iterator<Item = PredicateObligation<'tcx>> + ExactSizeIterator + 'a + Captures<'tcx> {
unsubstituted_region_constraints
.iter()
.map(move |constraint| {

View File

@ -11,7 +11,7 @@ pub struct FreeRegionMap<'tcx> {
}
impl<'tcx> FreeRegionMap<'tcx> {
pub fn elements(&self) -> impl Iterator<Item=&Region<'tcx>> {
pub fn elements(&self) -> impl Iterator<Item=&Region<'tcx>> + ExactSizeIterator {
self.relation.elements()
}

View File

@ -282,15 +282,15 @@ pub fn mut_vars_and_args_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a
/// Returns an iterator over all function arguments.
#[inline]
pub fn args_iter(&self) -> impl Iterator<Item = Local> {
pub fn args_iter(&self) -> impl Iterator<Item = Local> + ExactSizeIterator {
let arg_count = self.arg_count;
(1..=arg_count).map(Local::new)
(1..arg_count+1).map(Local::new)
}
/// Returns an iterator over all user-defined variables and compiler-generated temporaries (all
/// locals that are neither arguments nor the return place).
#[inline]
pub fn vars_and_temps_iter(&self) -> impl Iterator<Item = Local> {
pub fn vars_and_temps_iter(&self) -> impl Iterator<Item = Local> + ExactSizeIterator {
let arg_count = self.arg_count;
let local_count = self.local_decls.len();
(arg_count + 1..local_count).map(Local::new)
@ -2384,11 +2384,15 @@ pub fn from_projections(projs: impl Iterator<Item = (UserTypeProjection, Span)>)
UserTypeProjections { contents: projs.collect() }
}
pub fn projections_and_spans(&self) -> impl Iterator<Item = &(UserTypeProjection, Span)> {
pub fn projections_and_spans(&self)
-> impl Iterator<Item = &(UserTypeProjection, Span)> + ExactSizeIterator
{
self.contents.iter()
}
pub fn projections(&self) -> impl Iterator<Item = &UserTypeProjection> {
pub fn projections(&self)
-> impl Iterator<Item = &UserTypeProjection> + ExactSizeIterator
{
self.contents.iter().map(|&(ref user_type, _span)| user_type)
}

View File

@ -414,7 +414,9 @@ pub fn is_from_trait(&self) -> bool {
}
/// Iterate over the items defined directly by the given (impl or trait) node.
pub fn items(&self, tcx: TyCtxt<'tcx>) -> ty::AssocItemsIterator<'tcx> {
pub fn items(&self, tcx: TyCtxt<'tcx>)
-> impl Iterator<Item = ty::AssocItem> + ExactSizeIterator + Clone + 'tcx
{
tcx.associated_items(self.def_id())
}

View File

@ -2376,7 +2376,7 @@ pub fn eval_explicit_discr(&self, tcx: TyCtxt<'tcx>, expr_did: DefId) -> Option<
pub fn discriminants(
&'tcx self,
tcx: TyCtxt<'tcx>,
) -> impl Iterator<Item = (VariantIdx, Discr<'tcx>)> + Captures<'tcx> {
) -> impl Iterator<Item = (VariantIdx, Discr<'tcx>)> + ExactSizeIterator + Captures<'tcx> {
let repr_type = self.repr.discr_type();
let initial = repr_type.initial_discriminant(tcx);
let mut prev_discr = None::<Discr<'tcx>>;
@ -2740,7 +2740,9 @@ pub fn body_tables(self, body: hir::BodyId) -> &'tcx TypeckTables<'tcx> {
/// Returns an iterator of the `DefId`s for all body-owners in this
/// crate. If you would prefer to iterate over the bodies
/// themselves, you can do `self.hir().krate().body_ids.iter()`.
pub fn body_owners(self) -> impl Iterator<Item = DefId> + Captures<'tcx> + 'tcx {
pub fn body_owners(self)
-> impl Iterator<Item = DefId> + ExactSizeIterator + Captures<'tcx> + 'tcx
{
self.hir().krate()
.body_ids
.iter()
@ -3116,6 +3118,12 @@ fn next(&mut self) -> Option<AssocItem> {
}
}
impl ExactSizeIterator for AssocItemsIterator<'_> {
fn len(&self) -> usize {
self.def_ids.len() - self.next_index
}
}
fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> AssocItem {
let id = tcx.hir().as_local_hir_id(def_id).unwrap();
let parent_id = tcx.hir().get_parent_item(id);

View File

@ -345,7 +345,7 @@ pub fn upvar_tys(
self,
def_id: DefId,
tcx: TyCtxt<'_>,
) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
) -> impl Iterator<Item = Ty<'tcx>> + ExactSizeIterator + 'tcx {
let SplitClosureSubsts { upvar_kinds, .. } = self.split(def_id, tcx);
upvar_kinds.iter().map(|t| {
if let GenericArgKind::Type(ty) = t.unpack() {
@ -433,7 +433,7 @@ pub fn upvar_tys(
self,
def_id: DefId,
tcx: TyCtxt<'_>,
) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
) -> impl Iterator<Item = Ty<'tcx>> + ExactSizeIterator + 'tcx {
let SplitGeneratorSubsts { upvar_kinds, .. } = self.split(def_id, tcx);
upvar_kinds.iter().map(|t| {
if let GenericArgKind::Type(ty) = t.unpack() {
@ -551,7 +551,7 @@ pub fn state_tys(
self,
def_id: DefId,
tcx: TyCtxt<'tcx>,
) -> impl Iterator<Item = impl Iterator<Item = Ty<'tcx>> + Captures<'tcx>> {
) -> impl Iterator<Item = impl Iterator<Item = Ty<'tcx>> + ExactSizeIterator + Captures<'tcx>> {
let layout = tcx.generator_layout(def_id);
layout.variant_fields.iter().map(move |variant| {
variant.iter().map(move |field| {
@ -563,7 +563,9 @@ pub fn state_tys(
/// This is the types of the fields of a generator which are not stored in a
/// variant.
#[inline]
pub fn prefix_tys(self, def_id: DefId, tcx: TyCtxt<'tcx>) -> impl Iterator<Item = Ty<'tcx>> {
pub fn prefix_tys(self, def_id: DefId, tcx: TyCtxt<'tcx>)
-> impl Iterator<Item = Ty<'tcx>> + ExactSizeIterator
{
self.upvar_tys(def_id, tcx)
}
}
@ -580,7 +582,7 @@ pub fn upvar_tys(
self,
def_id: DefId,
tcx: TyCtxt<'tcx>,
) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
) -> impl Iterator<Item = Ty<'tcx>> + ExactSizeIterator + 'tcx {
let upvar_kinds = match self {
UpvarSubsts::Closure(substs) => substs.as_closure().split(def_id, tcx).upvar_kinds,
UpvarSubsts::Generator(substs) => substs.as_generator().split(def_id, tcx).upvar_kinds,

View File

@ -186,14 +186,18 @@ pub fn edge(&self, idx: EdgeIndex) -> &Edge<E> {
// # Iterating over nodes, edges
pub fn enumerated_nodes(&self) -> impl Iterator<Item = (NodeIndex, &Node<N>)> {
pub fn enumerated_nodes(&self)
-> impl Iterator<Item = (NodeIndex, &Node<N>)> + ExactSizeIterator
{
self.nodes
.iter()
.enumerate()
.map(|(idx, n)| (NodeIndex(idx), n))
}
pub fn enumerated_edges(&self) -> impl Iterator<Item = (EdgeIndex, &Edge<E>)> {
pub fn enumerated_edges(&self)
-> impl Iterator<Item = (EdgeIndex, &Edge<E>)> + ExactSizeIterator
{
self.edges
.iter()
.enumerate()

View File

@ -60,7 +60,7 @@ pub fn is_empty(&self) -> bool {
self.edges.is_empty()
}
pub fn elements(&self) -> impl Iterator<Item=&T> {
pub fn elements(&self) -> impl Iterator<Item=&T> + ExactSizeIterator {
self.elements.iter()
}

View File

@ -449,7 +449,7 @@ fn dep_nodes<'l>(
&self,
labels: &'l Labels,
def_id: DefId
) -> impl Iterator<Item = DepNode> + 'l {
) -> impl Iterator<Item = DepNode> + ExactSizeIterator + 'l {
let def_path_hash = self.tcx.def_path_hash(def_id);
labels
.iter()

View File

@ -748,7 +748,7 @@ pub fn from_row_n(row: &BitSet<C>, num_rows: usize) -> BitMatrix<R, C> {
}
}
pub fn rows(&self) -> impl Iterator<Item = R> {
pub fn rows(&self) -> impl Iterator<Item = R> + ExactSizeIterator {
(0..self.num_rows).map(R::new)
}
@ -975,7 +975,7 @@ pub fn insert_all_into_row(&mut self, row: R) {
self.ensure_row(row).insert_all();
}
pub fn rows(&self) -> impl Iterator<Item = R> {
pub fn rows(&self) -> impl Iterator<Item = R> + ExactSizeIterator {
self.rows.indices()
}

View File

@ -633,14 +633,16 @@ pub fn iter_enumerated_mut(&mut self) -> Enumerated<I, slice::IterMut<'_, T>>
}
#[inline]
pub fn drain<'a, R: RangeBounds<usize>>(
&'a mut self, range: R) -> impl Iterator<Item=T> + 'a {
pub fn drain<'a, R: RangeBounds<usize>>(&'a mut self, range: R)
-> impl Iterator<Item=T> + ExactSizeIterator + 'a
{
self.raw.drain(range)
}
#[inline]
pub fn drain_enumerated<'a, R: RangeBounds<usize>>(
&'a mut self, range: R) -> impl Iterator<Item=(I, T)> + 'a {
pub fn drain_enumerated<'a, R: RangeBounds<usize>>(&'a mut self, range: R)
-> impl Iterator<Item=(I, T)> + ExactSizeIterator + 'a
{
self.raw.drain(range).enumerate().map(IntoIdx { _marker: PhantomData })
}

View File

@ -53,7 +53,7 @@ impl LocationTable {
}
}
crate fn all_points(&self) -> impl Iterator<Item = LocationIndex> {
crate fn all_points(&self) -> impl Iterator<Item = LocationIndex> + ExactSizeIterator {
(0..self.num_points).map(LocationIndex::new)
}

View File

@ -155,7 +155,7 @@ impl<R> MemberConstraintSet<'tcx, R>
{
crate fn all_indices(
&self,
) -> impl Iterator<Item = NllMemberConstraintIndex> {
) -> impl Iterator<Item = NllMemberConstraintIndex> + ExactSizeIterator {
self.constraints.indices()
}

View File

@ -422,7 +422,7 @@ fn init_free_and_bound_regions(&mut self) {
}
/// Returns an iterator over all the region indices.
pub fn regions(&self) -> impl Iterator<Item = RegionVid> {
pub fn regions(&self) -> impl Iterator<Item = RegionVid> + ExactSizeIterator {
self.definitions.indices()
}

View File

@ -162,7 +162,7 @@ impl<N: Idx> LivenessValues<N> {
}
/// Iterate through each region that has a value in this set.
crate fn rows(&self) -> impl Iterator<Item=N> {
crate fn rows(&self) -> impl Iterator<Item=N> + ExactSizeIterator {
self.points.rows()
}

View File

@ -107,7 +107,9 @@ impl<'tcx> DefiningTy<'tcx> {
/// not a closure or generator, there are no upvars, and hence it
/// will be an empty list. The order of types in this list will
/// match up with the upvar order in the HIR, typesystem, and MIR.
pub fn upvar_tys(self, tcx: TyCtxt<'tcx>) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
pub fn upvar_tys(self, tcx: TyCtxt<'tcx>)
-> impl Iterator<Item = Ty<'tcx>> + ExactSizeIterator + 'tcx
{
match self {
DefiningTy::Closure(def_id, substs) => Either::Left(
substs.as_closure().upvar_tys(def_id, tcx)
@ -267,7 +269,7 @@ pub fn region_classification(&self, r: RegionVid) -> Option<RegionClassification
/// Returns an iterator over all the RegionVids corresponding to
/// universally quantified free regions.
pub fn universal_regions(&self) -> impl Iterator<Item = RegionVid> {
pub fn universal_regions(&self) -> impl Iterator<Item = RegionVid> + ExactSizeIterator {
(FIRST_GLOBAL_INDEX..self.num_universals).map(RegionVid::new)
}
@ -293,7 +295,7 @@ pub fn num_global_and_external_regions(&self) -> usize {
/// Gets an iterator over all the early-bound regions that have names.
pub fn named_universal_regions<'s>(
&'s self,
) -> impl Iterator<Item = (ty::Region<'tcx>, ty::RegionVid)> + 's {
) -> impl Iterator<Item = (ty::Region<'tcx>, ty::RegionVid)> + ExactSizeIterator + 's {
self.indices.indices.iter().map(|(&r, &v)| (r, v))
}

View File

@ -396,7 +396,7 @@ fn to_tail(&self) -> Self {
PatStack::from_slice(&self.0[1..])
}
fn iter(&self) -> impl Iterator<Item = &Pat<'tcx>> {
fn iter(&self) -> impl Iterator<Item = &Pat<'tcx>> + ExactSizeIterator {
self.0.iter().map(|p| *p)
}

View File

@ -723,7 +723,9 @@ pub fn memory_index(&self, i: usize) -> usize {
/// Gets source indices of the fields by increasing offsets.
#[inline]
pub fn index_by_increasing_offset<'a>(&'a self) -> impl Iterator<Item=usize>+'a {
pub fn index_by_increasing_offset<'a>(&'a self)
-> impl Iterator<Item=usize> + ExactSizeIterator + 'a
{
let mut inverse_small = [0u8; 64];
let mut inverse_big = vec![];
let use_small = self.count() <= inverse_small.len();