Skip to content

Commit d97379a

Browse files
author
Bartłomiej Kuras
committed
Added ExactSizeIterator bound to return types
in librustc in several places
1 parent d0126e8 commit d97379a

File tree

19 files changed

+61
-35
lines changed

19 files changed

+61
-35
lines changed

src/bootstrap/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ impl Build {
12741274
t!(fs::remove_dir_all(dir))
12751275
}
12761276

1277-
fn read_dir(&self, dir: &Path) -> impl Iterator<Item=fs::DirEntry> {
1277+
fn read_dir(&self, dir: &Path) -> impl Iterator<Item=fs::DirEntry> + ExactSizeIterator {
12781278
let iter = match fs::read_dir(dir) {
12791279
Ok(v) => v,
12801280
Err(_) if self.config.dry_run => return vec![].into_iter(),

src/librustc/infer/canonical/query_response.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
574574
param_env: ty::ParamEnv<'tcx>,
575575
unsubstituted_region_constraints: &'a [QueryOutlivesConstraint<'tcx>],
576576
result_subst: &'a CanonicalVarValues<'tcx>,
577-
) -> impl Iterator<Item = PredicateObligation<'tcx>> + 'a + Captures<'tcx> {
577+
) -> impl Iterator<Item = PredicateObligation<'tcx>> + ExactSizeIterator + 'a + Captures<'tcx> {
578578
unsubstituted_region_constraints
579579
.iter()
580580
.map(move |constraint| {

src/librustc/infer/outlives/free_region_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub struct FreeRegionMap<'tcx> {
1111
}
1212

1313
impl<'tcx> FreeRegionMap<'tcx> {
14-
pub fn elements(&self) -> impl Iterator<Item=&Region<'tcx>> {
14+
pub fn elements(&self) -> impl Iterator<Item=&Region<'tcx>> + ExactSizeIterator {
1515
self.relation.elements()
1616
}
1717

src/librustc/mir/mod.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,15 @@ impl<'tcx> Body<'tcx> {
282282

283283
/// Returns an iterator over all function arguments.
284284
#[inline]
285-
pub fn args_iter(&self) -> impl Iterator<Item = Local> {
285+
pub fn args_iter(&self) -> impl Iterator<Item = Local> + ExactSizeIterator {
286286
let arg_count = self.arg_count;
287-
(1..=arg_count).map(Local::new)
287+
(1..arg_count+1).map(Local::new)
288288
}
289289

290290
/// Returns an iterator over all user-defined variables and compiler-generated temporaries (all
291291
/// locals that are neither arguments nor the return place).
292292
#[inline]
293-
pub fn vars_and_temps_iter(&self) -> impl Iterator<Item = Local> {
293+
pub fn vars_and_temps_iter(&self) -> impl Iterator<Item = Local> + ExactSizeIterator {
294294
let arg_count = self.arg_count;
295295
let local_count = self.local_decls.len();
296296
(arg_count + 1..local_count).map(Local::new)
@@ -2384,11 +2384,15 @@ impl<'tcx> UserTypeProjections {
23842384
UserTypeProjections { contents: projs.collect() }
23852385
}
23862386

2387-
pub fn projections_and_spans(&self) -> impl Iterator<Item = &(UserTypeProjection, Span)> {
2387+
pub fn projections_and_spans(&self)
2388+
-> impl Iterator<Item = &(UserTypeProjection, Span)> + ExactSizeIterator
2389+
{
23882390
self.contents.iter()
23892391
}
23902392

2391-
pub fn projections(&self) -> impl Iterator<Item = &UserTypeProjection> {
2393+
pub fn projections(&self)
2394+
-> impl Iterator<Item = &UserTypeProjection> + ExactSizeIterator
2395+
{
23922396
self.contents.iter().map(|&(ref user_type, _span)| user_type)
23932397
}
23942398

src/librustc/traits/specialize/specialization_graph.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,9 @@ impl<'tcx> Node {
414414
}
415415

416416
/// Iterate over the items defined directly by the given (impl or trait) node.
417-
pub fn items(&self, tcx: TyCtxt<'tcx>) -> ty::AssocItemsIterator<'tcx> {
417+
pub fn items(&self, tcx: TyCtxt<'tcx>)
418+
-> impl Iterator<Item = ty::AssocItem> + ExactSizeIterator + Clone + 'tcx
419+
{
418420
tcx.associated_items(self.def_id())
419421
}
420422

src/librustc/ty/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -2376,7 +2376,7 @@ impl<'tcx> AdtDef {
23762376
pub fn discriminants(
23772377
&'tcx self,
23782378
tcx: TyCtxt<'tcx>,
2379-
) -> impl Iterator<Item = (VariantIdx, Discr<'tcx>)> + Captures<'tcx> {
2379+
) -> impl Iterator<Item = (VariantIdx, Discr<'tcx>)> + ExactSizeIterator + Captures<'tcx> {
23802380
let repr_type = self.repr.discr_type();
23812381
let initial = repr_type.initial_discriminant(tcx);
23822382
let mut prev_discr = None::<Discr<'tcx>>;
@@ -2740,7 +2740,9 @@ impl<'tcx> TyCtxt<'tcx> {
27402740
/// Returns an iterator of the `DefId`s for all body-owners in this
27412741
/// crate. If you would prefer to iterate over the bodies
27422742
/// themselves, you can do `self.hir().krate().body_ids.iter()`.
2743-
pub fn body_owners(self) -> impl Iterator<Item = DefId> + Captures<'tcx> + 'tcx {
2743+
pub fn body_owners(self)
2744+
-> impl Iterator<Item = DefId> + ExactSizeIterator + Captures<'tcx> + 'tcx
2745+
{
27442746
self.hir().krate()
27452747
.body_ids
27462748
.iter()
@@ -3116,6 +3118,12 @@ impl Iterator for AssocItemsIterator<'_> {
31163118
}
31173119
}
31183120

3121+
impl ExactSizeIterator for AssocItemsIterator<'_> {
3122+
fn len(&self) -> usize {
3123+
self.def_ids.len() - self.next_index
3124+
}
3125+
}
3126+
31193127
fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> AssocItem {
31203128
let id = tcx.hir().as_local_hir_id(def_id).unwrap();
31213129
let parent_id = tcx.hir().get_parent_item(id);

src/librustc/ty/sty.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ impl<'tcx> ClosureSubsts<'tcx> {
345345
self,
346346
def_id: DefId,
347347
tcx: TyCtxt<'_>,
348-
) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
348+
) -> impl Iterator<Item = Ty<'tcx>> + ExactSizeIterator + 'tcx {
349349
let SplitClosureSubsts { upvar_kinds, .. } = self.split(def_id, tcx);
350350
upvar_kinds.iter().map(|t| {
351351
if let GenericArgKind::Type(ty) = t.unpack() {
@@ -433,7 +433,7 @@ impl<'tcx> GeneratorSubsts<'tcx> {
433433
self,
434434
def_id: DefId,
435435
tcx: TyCtxt<'_>,
436-
) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
436+
) -> impl Iterator<Item = Ty<'tcx>> + ExactSizeIterator + 'tcx {
437437
let SplitGeneratorSubsts { upvar_kinds, .. } = self.split(def_id, tcx);
438438
upvar_kinds.iter().map(|t| {
439439
if let GenericArgKind::Type(ty) = t.unpack() {
@@ -551,7 +551,7 @@ impl<'tcx> GeneratorSubsts<'tcx> {
551551
self,
552552
def_id: DefId,
553553
tcx: TyCtxt<'tcx>,
554-
) -> impl Iterator<Item = impl Iterator<Item = Ty<'tcx>> + Captures<'tcx>> {
554+
) -> impl Iterator<Item = impl Iterator<Item = Ty<'tcx>> + ExactSizeIterator + Captures<'tcx>> {
555555
let layout = tcx.generator_layout(def_id);
556556
layout.variant_fields.iter().map(move |variant| {
557557
variant.iter().map(move |field| {
@@ -563,7 +563,9 @@ impl<'tcx> GeneratorSubsts<'tcx> {
563563
/// This is the types of the fields of a generator which are not stored in a
564564
/// variant.
565565
#[inline]
566-
pub fn prefix_tys(self, def_id: DefId, tcx: TyCtxt<'tcx>) -> impl Iterator<Item = Ty<'tcx>> {
566+
pub fn prefix_tys(self, def_id: DefId, tcx: TyCtxt<'tcx>)
567+
-> impl Iterator<Item = Ty<'tcx>> + ExactSizeIterator
568+
{
567569
self.upvar_tys(def_id, tcx)
568570
}
569571
}
@@ -580,7 +582,7 @@ impl<'tcx> UpvarSubsts<'tcx> {
580582
self,
581583
def_id: DefId,
582584
tcx: TyCtxt<'tcx>,
583-
) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
585+
) -> impl Iterator<Item = Ty<'tcx>> + ExactSizeIterator + 'tcx {
584586
let upvar_kinds = match self {
585587
UpvarSubsts::Closure(substs) => substs.as_closure().split(def_id, tcx).upvar_kinds,
586588
UpvarSubsts::Generator(substs) => substs.as_generator().split(def_id, tcx).upvar_kinds,

src/librustc_data_structures/graph/implementation/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,18 @@ impl<N: Debug, E: Debug> Graph<N, E> {
186186

187187
// # Iterating over nodes, edges
188188

189-
pub fn enumerated_nodes(&self) -> impl Iterator<Item = (NodeIndex, &Node<N>)> {
189+
pub fn enumerated_nodes(&self)
190+
-> impl Iterator<Item = (NodeIndex, &Node<N>)> + ExactSizeIterator
191+
{
190192
self.nodes
191193
.iter()
192194
.enumerate()
193195
.map(|(idx, n)| (NodeIndex(idx), n))
194196
}
195197

196-
pub fn enumerated_edges(&self) -> impl Iterator<Item = (EdgeIndex, &Edge<E>)> {
198+
pub fn enumerated_edges(&self)
199+
-> impl Iterator<Item = (EdgeIndex, &Edge<E>)> + ExactSizeIterator
200+
{
197201
self.edges
198202
.iter()
199203
.enumerate()

src/librustc_data_structures/transitive_relation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> {
6060
self.edges.is_empty()
6161
}
6262

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

src/librustc_incremental/persist/dirty_clean.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ impl DirtyCleanVisitor<'tcx> {
449449
&self,
450450
labels: &'l Labels,
451451
def_id: DefId
452-
) -> impl Iterator<Item = DepNode> + 'l {
452+
) -> impl Iterator<Item = DepNode> + ExactSizeIterator + 'l {
453453
let def_path_hash = self.tcx.def_path_hash(def_id);
454454
labels
455455
.iter()

src/librustc_index/bit_set.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
748748
}
749749
}
750750

751-
pub fn rows(&self) -> impl Iterator<Item = R> {
751+
pub fn rows(&self) -> impl Iterator<Item = R> + ExactSizeIterator {
752752
(0..self.num_rows).map(R::new)
753753
}
754754

@@ -975,7 +975,7 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
975975
self.ensure_row(row).insert_all();
976976
}
977977

978-
pub fn rows(&self) -> impl Iterator<Item = R> {
978+
pub fn rows(&self) -> impl Iterator<Item = R> + ExactSizeIterator {
979979
self.rows.indices()
980980
}
981981

src/librustc_index/vec.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -633,14 +633,16 @@ impl<I: Idx, T> IndexVec<I, T> {
633633
}
634634

635635
#[inline]
636-
pub fn drain<'a, R: RangeBounds<usize>>(
637-
&'a mut self, range: R) -> impl Iterator<Item=T> + 'a {
636+
pub fn drain<'a, R: RangeBounds<usize>>(&'a mut self, range: R)
637+
-> impl Iterator<Item=T> + ExactSizeIterator + 'a
638+
{
638639
self.raw.drain(range)
639640
}
640641

641642
#[inline]
642-
pub fn drain_enumerated<'a, R: RangeBounds<usize>>(
643-
&'a mut self, range: R) -> impl Iterator<Item=(I, T)> + 'a {
643+
pub fn drain_enumerated<'a, R: RangeBounds<usize>>(&'a mut self, range: R)
644+
-> impl Iterator<Item=(I, T)> + ExactSizeIterator + 'a
645+
{
644646
self.raw.drain(range).enumerate().map(IntoIdx { _marker: PhantomData })
645647
}
646648

src/librustc_mir/borrow_check/location.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl LocationTable {
5353
}
5454
}
5555

56-
crate fn all_points(&self) -> impl Iterator<Item = LocationIndex> {
56+
crate fn all_points(&self) -> impl Iterator<Item = LocationIndex> + ExactSizeIterator {
5757
(0..self.num_points).map(LocationIndex::new)
5858
}
5959

src/librustc_mir/borrow_check/nll/member_constraints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ where
155155
{
156156
crate fn all_indices(
157157
&self,
158-
) -> impl Iterator<Item = NllMemberConstraintIndex> {
158+
) -> impl Iterator<Item = NllMemberConstraintIndex> + ExactSizeIterator {
159159
self.constraints.indices()
160160
}
161161

src/librustc_mir/borrow_check/nll/region_infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
422422
}
423423

424424
/// Returns an iterator over all the region indices.
425-
pub fn regions(&self) -> impl Iterator<Item = RegionVid> {
425+
pub fn regions(&self) -> impl Iterator<Item = RegionVid> + ExactSizeIterator {
426426
self.definitions.indices()
427427
}
428428

src/librustc_mir/borrow_check/nll/region_infer/values.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl<N: Idx> LivenessValues<N> {
162162
}
163163

164164
/// Iterate through each region that has a value in this set.
165-
crate fn rows(&self) -> impl Iterator<Item=N> {
165+
crate fn rows(&self) -> impl Iterator<Item=N> + ExactSizeIterator {
166166
self.points.rows()
167167
}
168168

src/librustc_mir/borrow_check/nll/universal_regions.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ impl<'tcx> DefiningTy<'tcx> {
107107
/// not a closure or generator, there are no upvars, and hence it
108108
/// will be an empty list. The order of types in this list will
109109
/// match up with the upvar order in the HIR, typesystem, and MIR.
110-
pub fn upvar_tys(self, tcx: TyCtxt<'tcx>) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
110+
pub fn upvar_tys(self, tcx: TyCtxt<'tcx>)
111+
-> impl Iterator<Item = Ty<'tcx>> + ExactSizeIterator + 'tcx
112+
{
111113
match self {
112114
DefiningTy::Closure(def_id, substs) => Either::Left(
113115
substs.as_closure().upvar_tys(def_id, tcx)
@@ -267,7 +269,7 @@ impl<'tcx> UniversalRegions<'tcx> {
267269

268270
/// Returns an iterator over all the RegionVids corresponding to
269271
/// universally quantified free regions.
270-
pub fn universal_regions(&self) -> impl Iterator<Item = RegionVid> {
272+
pub fn universal_regions(&self) -> impl Iterator<Item = RegionVid> + ExactSizeIterator {
271273
(FIRST_GLOBAL_INDEX..self.num_universals).map(RegionVid::new)
272274
}
273275

@@ -293,7 +295,7 @@ impl<'tcx> UniversalRegions<'tcx> {
293295
/// Gets an iterator over all the early-bound regions that have names.
294296
pub fn named_universal_regions<'s>(
295297
&'s self,
296-
) -> impl Iterator<Item = (ty::Region<'tcx>, ty::RegionVid)> + 's {
298+
) -> impl Iterator<Item = (ty::Region<'tcx>, ty::RegionVid)> + ExactSizeIterator + 's {
297299
self.indices.indices.iter().map(|(&r, &v)| (r, v))
298300
}
299301

src/librustc_mir/hair/pattern/_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ impl<'p, 'tcx> PatStack<'p, 'tcx> {
396396
PatStack::from_slice(&self.0[1..])
397397
}
398398

399-
fn iter(&self) -> impl Iterator<Item = &Pat<'tcx>> {
399+
fn iter(&self) -> impl Iterator<Item = &Pat<'tcx>> + ExactSizeIterator {
400400
self.0.iter().map(|p| *p)
401401
}
402402

src/librustc_target/abi/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,9 @@ impl FieldPlacement {
723723

724724
/// Gets source indices of the fields by increasing offsets.
725725
#[inline]
726-
pub fn index_by_increasing_offset<'a>(&'a self) -> impl Iterator<Item=usize>+'a {
726+
pub fn index_by_increasing_offset<'a>(&'a self)
727+
-> impl Iterator<Item=usize> + ExactSizeIterator + 'a
728+
{
727729
let mut inverse_small = [0u8; 64];
728730
let mut inverse_big = vec![];
729731
let use_small = self.count() <= inverse_small.len();

0 commit comments

Comments
 (0)