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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 9 additions & 5 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 10 additions & 2 deletions
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

Lines changed: 7 additions & 5 deletions
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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()

0 commit comments

Comments
 (0)