@@ -102,30 +102,23 @@ struct CrateDump<'a>(&'a CStore);
102
102
impl<'a> std::fmt::Debug for CrateDump<'a> {
103
103
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104
104
writeln!(fmt, "resolved crates:")?;
105
- // `iter_crate_data` does not allow returning values. Thus we use a mutable variable here
106
- // that aggregates the value (and any errors that could happen).
107
- let mut res = Ok(());
108
- self.0.iter_crate_data(|cnum, data| {
109
- res = res.and(
110
- try {
111
- writeln!(fmt, " name: {}", data.name())?;
112
- writeln!(fmt, " cnum: {}", cnum)?;
113
- writeln!(fmt, " hash: {}", data.hash())?;
114
- writeln!(fmt, " reqd: {:?}", data.dep_kind())?;
115
- let CrateSource { dylib, rlib, rmeta } = data.source();
116
- if let Some(dylib) = dylib {
117
- writeln!(fmt, " dylib: {}", dylib.0.display())?;
118
- }
119
- if let Some(rlib) = rlib {
120
- writeln!(fmt, " rlib: {}", rlib.0.display())?;
121
- }
122
- if let Some(rmeta) = rmeta {
123
- writeln!(fmt, " rmeta: {}", rmeta.0.display())?;
124
- }
125
- },
126
- );
127
- });
128
- res
105
+ for (cnum, data) in self.0.iter_crate_data() {
106
+ writeln!(fmt, " name: {}", data.name())?;
107
+ writeln!(fmt, " cnum: {}", cnum)?;
108
+ writeln!(fmt, " hash: {}", data.hash())?;
109
+ writeln!(fmt, " reqd: {:?}", data.dep_kind())?;
110
+ let CrateSource { dylib, rlib, rmeta } = data.source();
111
+ if let Some(dylib) = dylib {
112
+ writeln!(fmt, " dylib: {}", dylib.0.display())?;
113
+ }
114
+ if let Some(rlib) = rlib {
115
+ writeln!(fmt, " rlib: {}", rlib.0.display())?;
116
+ }
117
+ if let Some(rmeta) = rmeta {
118
+ writeln!(fmt, " rmeta: {}", rmeta.0.display())?;
119
+ }
120
+ }
121
+ Ok(())
129
122
}
130
123
}
131
124
@@ -154,12 +147,10 @@ impl CStore {
154
147
self.metas[cnum] = Some(Lrc::new(data));
155
148
}
156
149
157
- crate fn iter_crate_data(&self, mut f: impl FnMut(CrateNum, &CrateMetadata)) {
158
- for (cnum, data) in self.metas.iter_enumerated() {
159
- if let Some(data) = data {
160
- f(cnum, data);
161
- }
162
- }
150
+ crate fn iter_crate_data(&self) -> impl Iterator<Item = (CrateNum, &CrateMetadata)> {
151
+ self.metas
152
+ .iter_enumerated()
153
+ .filter_map(|(cnum, data)| data.as_ref().map(|data| (cnum, &**data)))
163
154
}
164
155
165
156
fn push_dependencies_in_postorder(&self, deps: &mut Vec<CrateNum>, cnum: CrateNum) {
@@ -178,7 +169,9 @@ impl CStore {
178
169
crate fn crate_dependencies_in_postorder(&self, cnum: CrateNum) -> Vec<CrateNum> {
179
170
let mut deps = Vec::new();
180
171
if cnum == LOCAL_CRATE {
181
- self.iter_crate_data(|cnum, _| self.push_dependencies_in_postorder(&mut deps, cnum));
172
+ for (cnum, _) in self.iter_crate_data() {
173
+ self.push_dependencies_in_postorder(&mut deps, cnum);
174
+ }
182
175
} else {
183
176
self.push_dependencies_in_postorder(&mut deps, cnum);
184
177
}
@@ -263,21 +256,17 @@ impl<'a> CrateLoader<'a> {
263
256
}
264
257
265
258
fn existing_match(&self, name: Symbol, hash: Option<Svh>, kind: PathKind) -> Option<CrateNum> {
266
- let mut ret = None;
267
- self.cstore.iter_crate_data(|cnum, data| {
259
+ for (cnum, data) in self.cstore.iter_crate_data() {
268
260
if data.name() != name {
269
261
tracing::trace!("{} did not match {}", data.name(), name);
270
- return ;
262
+ continue ;
271
263
}
272
264
273
265
match hash {
274
- Some(hash) if hash == data.hash() => {
275
- ret = Some(cnum);
276
- return;
277
- }
266
+ Some(hash) if hash == data.hash() => return Some(cnum),
278
267
Some(hash) => {
279
268
debug!("actual hash {} did not match expected {}", hash, data.hash());
280
- return ;
269
+ continue ;
281
270
}
282
271
None => {}
283
272
}
@@ -301,10 +290,10 @@ impl<'a> CrateLoader<'a> {
301
290
|| source.rlib.as_ref().map(|(p, _)| p) == Some(l)
302
291
|| source.rmeta.as_ref().map(|(p, _)| p) == Some(l)
303
292
}) {
304
- ret = Some(cnum);
293
+ return Some(cnum);
305
294
}
306
295
}
307
- return ;
296
+ continue ;
308
297
}
309
298
310
299
// Alright, so we've gotten this far which means that `data` has the
@@ -321,15 +310,16 @@ impl<'a> CrateLoader<'a> {
321
310
.expect("No sources for crate")
322
311
.1;
323
312
if kind.matches(prev_kind) {
324
- ret = Some(cnum);
313
+ return Some(cnum);
325
314
} else {
326
315
debug!(
327
316
"failed to load existing crate {}; kind {:?} did not match prev_kind {:?}",
328
317
name, kind, prev_kind
329
318
);
330
319
}
331
- });
332
- ret
320
+ }
321
+
322
+ None
333
323
}
334
324
335
325
fn verify_no_symbol_conflicts(&self, root: &CrateRoot<'_>) -> Result<(), CrateError> {
@@ -339,17 +329,14 @@ impl<'a> CrateLoader<'a> {
339
329
}
340
330
341
331
// Check for conflicts with any crate loaded so far
342
- let mut res = Ok(());
343
- self.cstore.iter_crate_data(|_, other| {
344
- if other.stable_crate_id() == root.stable_crate_id() && // same stable crate id
345
- other.hash() != root.hash()
346
- {
347
- // but different SVH
348
- res = Err(CrateError::SymbolConflictsOthers(root.name()));
332
+ for (_, other) in self.cstore.iter_crate_data() {
333
+ // Same stable crate id but different SVH
334
+ if other.stable_crate_id() == root.stable_crate_id() && other.hash() != root.hash() {
335
+ return Err(CrateError::SymbolConflictsOthers(root.name()));
349
336
}
350
- });
337
+ }
351
338
352
- res
339
+ Ok(())
353
340
}
354
341
355
342
fn verify_no_stable_crate_id_hash_conflicts(
@@ -607,13 +594,14 @@ impl<'a> CrateLoader<'a> {
607
594
locator.triple == self.sess.opts.target_triple || locator.is_proc_macro;
608
595
Ok(Some(if can_reuse_cratenum {
609
596
let mut result = LoadResult::Loaded(library);
610
- self.cstore.iter_crate_data(|cnum, data| {
597
+ for (cnum, data) in self.cstore.iter_crate_data() {
611
598
if data.name() == root.name() && root.hash() == data.hash() {
612
599
assert!(locator.hash.is_none());
613
600
info!("load success, going to previous cnum: {}", cnum);
614
601
result = LoadResult::Previous(cnum);
602
+ break;
615
603
}
616
- });
604
+ }
617
605
result
618
606
} else {
619
607
LoadResult::Loaded(library)
@@ -711,7 +699,7 @@ impl<'a> CrateLoader<'a> {
711
699
let mut needs_panic_runtime =
712
700
self.sess.contains_name(&krate.attrs, sym::needs_panic_runtime);
713
701
714
- self.cstore.iter_crate_data(|cnum, data| {
702
+ for (cnum, data) in self.cstore.iter_crate_data() {
715
703
needs_panic_runtime = needs_panic_runtime || data.needs_panic_runtime();
716
704
if data.is_panic_runtime() {
717
705
// Inject a dependency from all #![needs_panic_runtime] to this
@@ -721,7 +709,7 @@ impl<'a> CrateLoader<'a> {
721
709
});
722
710
runtime_found = runtime_found || data.dep_kind() == CrateDepKind::Explicit;
723
711
}
724
- });
712
+ }
725
713
726
714
// If an explicitly linked and matching panic runtime was found, or if
727
715
// we just don't need one at all, then we're done here and there's
@@ -813,11 +801,9 @@ impl<'a> CrateLoader<'a> {
813
801
// Check to see if we actually need an allocator. This desire comes
814
802
// about through the `#![needs_allocator]` attribute and is typically
815
803
// written down in liballoc.
816
- let mut needs_allocator = self.sess.contains_name(&krate.attrs, sym::needs_allocator);
817
- self.cstore.iter_crate_data(|_, data| {
818
- needs_allocator = needs_allocator || data.needs_allocator();
819
- });
820
- if !needs_allocator {
804
+ if !self.sess.contains_name(&krate.attrs, sym::needs_allocator)
805
+ && !self.cstore.iter_crate_data().any(|(_, data)| data.needs_allocator())
806
+ {
821
807
return;
822
808
}
823
809
@@ -838,23 +824,19 @@ impl<'a> CrateLoader<'a> {
838
824
// global allocator.
839
825
let mut global_allocator =
840
826
self.cstore.has_global_allocator.then(|| Symbol::intern("this crate"));
841
- self.cstore.iter_crate_data(|_, data| {
842
- if !data.has_global_allocator() {
843
- return;
844
- }
845
- match global_allocator {
846
- Some(other_crate) => {
847
- self.sess.err(&format!(
848
- "the `#[global_allocator]` in {} \
849
- conflicts with global \
850
- allocator in: {}",
827
+ for (_, data) in self.cstore.iter_crate_data() {
828
+ if data.has_global_allocator() {
829
+ match global_allocator {
830
+ Some(other_crate) => self.sess.err(&format!(
831
+ "the `#[global_allocator]` in {} conflicts with global allocator in: {}",
851
832
other_crate,
852
833
data.name()
853
- ));
834
+ )),
835
+ None => global_allocator = Some(data.name()),
854
836
}
855
- None => global_allocator = Some(data.name()),
856
837
}
857
- });
838
+ }
839
+
858
840
if global_allocator.is_some() {
859
841
self.cstore.allocator_kind = Some(AllocatorKind::Global);
860
842
return;
@@ -864,19 +846,12 @@ impl<'a> CrateLoader<'a> {
864
846
// allocator. At this point our allocator request is typically fulfilled
865
847
// by the standard library, denoted by the `#![default_lib_allocator]`
866
848
// attribute.
867
- let mut has_default = self.sess.contains_name(&krate.attrs, sym::default_lib_allocator);
868
- self.cstore.iter_crate_data(|_, data| {
869
- if data.has_default_lib_allocator() {
870
- has_default = true;
871
- }
872
- });
873
-
874
- if !has_default {
849
+ if !self.sess.contains_name(&krate.attrs, sym::default_lib_allocator)
850
+ && !self.cstore.iter_crate_data().any(|(_, data)| data.has_default_lib_allocator())
851
+ {
875
852
self.sess.err(
876
- "no global memory allocator found but one is \
877
- required; link to std or \
878
- add `#[global_allocator]` to a static item \
879
- that implements the GlobalAlloc trait",
853
+ "no global memory allocator found but one is required; link to std or add \
854
+ `#[global_allocator]` to a static item that implements the GlobalAlloc trait",
880
855
);
881
856
}
882
857
self.cstore.allocator_kind = Some(AllocatorKind::Default);
@@ -916,14 +891,12 @@ impl<'a> CrateLoader<'a> {
916
891
// crate provided for this compile, but in order for this compilation to
917
892
// be successfully linked we need to inject a dependency (to order the
918
893
// crates on the command line correctly).
919
- self.cstore.iter_crate_data(|cnum, data| {
920
- if !needs_dep(data) {
921
- return;
894
+ for (cnum, data) in self.cstore.iter_crate_data() {
895
+ if needs_dep(data) {
896
+ info!("injecting a dep from {} to {}", cnum, krate);
897
+ data.add_dependency(krate);
922
898
}
923
-
924
- info!("injecting a dep from {} to {}", cnum, krate);
925
- data.add_dependency(krate);
926
- });
899
+ }
927
900
}
928
901
929
902
fn report_unused_deps(&mut self, krate: &ast::Crate) {
0 commit comments