Skip to content

Commit 5ea8eb5

Browse files
committed
Auto merge of rust-lang#55067 - ljedrz:generic_iterator_related_improvements, r=petrochenkov
A few iterator-related improvements - typeck: don't collect into a vector when unnecessary - create only one vector when winnowing candidates - change a cloning map to `into_iter`
2 parents df0d6ad + a14a950 commit 5ea8eb5

File tree

6 files changed

+16
-18
lines changed

6 files changed

+16
-18
lines changed

Diff for: src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#![feature(in_band_lifetimes)]
7272
#![feature(macro_at_most_once_rep)]
7373
#![feature(crate_visibility_modifier)]
74+
#![feature(transpose_result)]
7475

7576
#![recursion_limit="512"]
7677

Diff for: src/librustc/traits/select.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1368,8 +1368,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
13681368

13691369
// Winnow, but record the exact outcome of evaluation, which
13701370
// is needed for specialization. Propagate overflow if it occurs.
1371-
let candidates: Result<Vec<Option<EvaluatedCandidate<'_>>>, _> = candidates
1372-
.into_iter()
1371+
let mut candidates = candidates.into_iter()
13731372
.map(|c| match self.evaluate_candidate(stack, &c) {
13741373
Ok(eval) if eval.may_apply() => Ok(Some(EvaluatedCandidate {
13751374
candidate: c,
@@ -1378,10 +1377,8 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
13781377
Ok(_) => Ok(None),
13791378
Err(OverflowError) => Err(Overflow),
13801379
})
1381-
.collect();
1382-
1383-
let mut candidates: Vec<EvaluatedCandidate<'_>> =
1384-
candidates?.into_iter().filter_map(|c| c).collect();
1380+
.flat_map(Result::transpose)
1381+
.collect::<Result<Vec<_>, _>>()?;
13851382

13861383
debug!(
13871384
"winnowed to {} candidates for {:?}: {:?}",
@@ -1390,7 +1387,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
13901387
candidates
13911388
);
13921389

1393-
// If there are STILL multiple candidate, we can further
1390+
// If there are STILL multiple candidates, we can further
13941391
// reduce the list by dropping duplicates -- including
13951392
// resolving specializations.
13961393
if candidates.len() > 1 {

Diff for: src/librustc/ty/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
681681
match (r1, r2) {
682682
(Representability::SelfRecursive(v1),
683683
Representability::SelfRecursive(v2)) => {
684-
Representability::SelfRecursive(v1.iter().map(|s| *s).chain(v2).collect())
684+
Representability::SelfRecursive(v1.into_iter().chain(v2).collect())
685685
}
686686
(r1, r2) => cmp::max(r1, r2)
687687
}

Diff for: src/librustc_typeck/astconv.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1592,8 +1592,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
15921592
debug!("ty_of_fn");
15931593

15941594
let tcx = self.tcx();
1595-
let input_tys: Vec<Ty> =
1596-
decl.inputs.iter().map(|a| self.ty_of_arg(a, None)).collect();
1595+
let input_tys =
1596+
decl.inputs.iter().map(|a| self.ty_of_arg(a, None));
15971597

15981598
let output_ty = match decl.output {
15991599
hir::Return(ref output) => self.ast_ty_to_ty(output),
@@ -1603,7 +1603,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
16031603
debug!("ty_of_fn: output_ty={:?}", output_ty);
16041604

16051605
let bare_fn_ty = ty::Binder::bind(tcx.mk_fn_sig(
1606-
input_tys.into_iter(),
1606+
input_tys,
16071607
output_ty,
16081608
decl.variadic,
16091609
unsafety,

Diff for: src/librustc_typeck/check/_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
626626

627627
// Typecheck the patterns first, so that we get types for all the
628628
// bindings.
629-
let all_arm_pats_diverge: Vec<_> = arms.iter().map(|arm| {
629+
let all_arm_pats_diverge = arms.iter().map(|arm| {
630630
let mut all_pats_diverge = Diverges::WarnedAlways;
631631
for p in &arm.pats {
632632
self.diverges.set(Diverges::Maybe);
@@ -642,7 +642,7 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
642642
Diverges::Maybe => Diverges::Maybe,
643643
Diverges::Always | Diverges::WarnedAlways => Diverges::WarnedAlways,
644644
}
645-
}).collect();
645+
});
646646

647647
// Now typecheck the blocks.
648648
//

Diff for: src/librustc_typeck/check/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1769,7 +1769,7 @@ fn check_transparent<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, sp: Span, def_id: De
17691769
}
17701770

17711771
// For each field, figure out if it's known to be a ZST and align(1)
1772-
let field_infos: Vec<_> = adt.non_enum_variant().fields.iter().map(|field| {
1772+
let field_infos = adt.non_enum_variant().fields.iter().map(|field| {
17731773
let ty = field.ty(tcx, Substs::identity_for_item(tcx, field.did));
17741774
let param_env = tcx.param_env(field.did);
17751775
let layout = tcx.layout_of(param_env.and(ty));
@@ -1778,19 +1778,19 @@ fn check_transparent<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, sp: Span, def_id: De
17781778
let zst = layout.map(|layout| layout.is_zst()).unwrap_or(false);
17791779
let align1 = layout.map(|layout| layout.align.abi() == 1).unwrap_or(false);
17801780
(span, zst, align1)
1781-
}).collect();
1781+
});
17821782

1783-
let non_zst_fields = field_infos.iter().filter(|(_span, zst, _align1)| !*zst);
1783+
let non_zst_fields = field_infos.clone().filter(|(_span, zst, _align1)| !*zst);
17841784
let non_zst_count = non_zst_fields.clone().count();
17851785
if non_zst_count != 1 {
1786-
let field_spans: Vec<_> = non_zst_fields.map(|(span, _zst, _align1)| *span).collect();
1786+
let field_spans: Vec<_> = non_zst_fields.map(|(span, _zst, _align1)| span).collect();
17871787
struct_span_err!(tcx.sess, sp, E0690,
17881788
"transparent struct needs exactly one non-zero-sized field, but has {}",
17891789
non_zst_count)
17901790
.span_note(field_spans, "non-zero-sized field")
17911791
.emit();
17921792
}
1793-
for &(span, zst, align1) in &field_infos {
1793+
for (span, zst, align1) in field_infos {
17941794
if zst && !align1 {
17951795
span_err!(tcx.sess, span, E0691,
17961796
"zero-sized field in transparent struct has alignment larger than 1");

0 commit comments

Comments
 (0)