Skip to content

Commit

Permalink
Added ExactSizeIterator bound to return types
Browse files Browse the repository at this point in the history
in librustc in several places
  • Loading branch information
Bartłomiej Kuras committed Dec 7, 2019
1 parent d0126e8 commit d97379a
Show file tree
Hide file tree
Showing 19 changed files with 61 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,7 @@ impl Build {
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(),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/canonical/query_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
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| {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/outlives/free_region_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
14 changes: 9 additions & 5 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,15 @@ impl<'tcx> Body<'tcx> {

/// 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)
Expand Down Expand Up @@ -2384,11 +2384,15 @@ impl<'tcx> UserTypeProjections {
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)
}

Expand Down
4 changes: 3 additions & 1 deletion src/librustc/traits/specialize/specialization_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,9 @@ impl<'tcx> Node {
}

/// 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())
}

Expand Down
12 changes: 10 additions & 2 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2376,7 +2376,7 @@ impl<'tcx> AdtDef {
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>>;
Expand Down Expand Up @@ -2740,7 +2740,9 @@ impl<'tcx> TyCtxt<'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()
Expand Down Expand Up @@ -3116,6 +3118,12 @@ impl Iterator for AssocItemsIterator<'_> {
}
}

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);
Expand Down
12 changes: 7 additions & 5 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ impl<'tcx> ClosureSubsts<'tcx> {
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() {
Expand Down Expand Up @@ -433,7 +433,7 @@ impl<'tcx> GeneratorSubsts<'tcx> {
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() {
Expand Down Expand Up @@ -551,7 +551,7 @@ impl<'tcx> GeneratorSubsts<'tcx> {
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| {
Expand All @@ -563,7 +563,9 @@ impl<'tcx> GeneratorSubsts<'tcx> {
/// 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)
}
}
Expand All @@ -580,7 +582,7 @@ impl<'tcx> UpvarSubsts<'tcx> {
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,
Expand Down
8 changes: 6 additions & 2 deletions src/librustc_data_structures/graph/implementation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,18 @@ impl<N: Debug, E: Debug> Graph<N, 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()
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_data_structures/transitive_relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> {
self.edges.is_empty()
}

pub fn elements(&self) -> impl Iterator<Item=&T> {
pub fn elements(&self) -> impl Iterator<Item=&T> + ExactSizeIterator {
self.elements.iter()
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_incremental/persist/dirty_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ impl DirtyCleanVisitor<'tcx> {
&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()
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_index/bit_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ impl<R: Idx, C: Idx> 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)
}

Expand Down Expand Up @@ -975,7 +975,7 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
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()
}

Expand Down
10 changes: 6 additions & 4 deletions src/librustc_index/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,14 +633,16 @@ impl<I: Idx, T> IndexVec<I, 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 })
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/nll/member_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ where
{
crate fn all_indices(
&self,
) -> impl Iterator<Item = NllMemberConstraintIndex> {
) -> impl Iterator<Item = NllMemberConstraintIndex> + ExactSizeIterator {
self.constraints.indices()
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/nll/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
}

/// 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()
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/nll/region_infer/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
8 changes: 5 additions & 3 deletions src/librustc_mir/borrow_check/nll/universal_regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -267,7 +269,7 @@ impl<'tcx> UniversalRegions<'tcx> {

/// 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)
}

Expand All @@ -293,7 +295,7 @@ impl<'tcx> UniversalRegions<'tcx> {
/// 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))
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/hair/pattern/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ impl<'p, 'tcx> PatStack<'p, 'tcx> {
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)
}

Expand Down
4 changes: 3 additions & 1 deletion src/librustc_target/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,9 @@ impl FieldPlacement {

/// 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();
Expand Down

0 comments on commit d97379a

Please sign in to comment.