Skip to content

Commit a6919ef

Browse files
Rollup merge of rust-lang#77877 - scottmcm:fewer-try-trait-method-references, r=shepmaster
Use `try{}` in `try_fold` to decouple iterators in the library from `Try` details I'd like to experiment with changing the `?`/`try` desugaring and correspondingly the `Try` trait (see rust-lang#42327 for discussions about the suboptimalities of the current one) and this change would keep from needing any `cfg(bootstrap)` in iterator things. This will be lowered to the same thing, so shouldn't cause any perf issues: https://github.com/rust-lang/rust/blob/08e2d4616613716362b4b49980ff303f2b9ae654/compiler/rustc_ast_lowering/src/expr.rs#L428-L429 But ~~I'll trigger~~ I've triggered [a perf run](https://perf.rust-lang.org/compare.html?start=d65c08e9cc164b7b44de53503fae859a4fafd976&end=2c067c5235e779cd75e9f0cdfe572c64f1a12b9b) just in case. ~~EDIT: changed to a draft because of the rustfmt-only syntax error. zulip thread about it: https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/New.20bootstrap.20rustfmt.20doesn't.20support.20syntax.20from.20sept.3F/near/213098097~~ EDIT: This now includes a rustfmt version bump to get through tidy.
2 parents cb2462c + 8374c17 commit a6919ef

File tree

9 files changed

+32
-35
lines changed

9 files changed

+32
-35
lines changed

library/core/src/iter/adapters/chain.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ where
109109
acc = b.try_fold(acc, f)?;
110110
// we don't fuse the second iterator
111111
}
112-
Try::from_ok(acc)
112+
try { acc }
113113
}
114114

115115
fn fold<Acc, F>(self, mut acc: Acc, mut f: F) -> Acc
@@ -292,7 +292,7 @@ where
292292
acc = a.try_rfold(acc, f)?;
293293
// we don't fuse the second iterator
294294
}
295-
Try::from_ok(acc)
295+
try { acc }
296296
}
297297

298298
fn rfold<Acc, F>(self, mut acc: Acc, mut f: F) -> Acc

library/core/src/iter/adapters/flatten.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ where
317317
}
318318
self.backiter = None;
319319

320-
Try::from_ok(init)
320+
try { init }
321321
}
322322

323323
#[inline]
@@ -397,7 +397,7 @@ where
397397
}
398398
self.frontiter = None;
399399

400-
Try::from_ok(init)
400+
try { init }
401401
}
402402

403403
#[inline]

library/core/src/iter/adapters/fuse.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ where
303303
acc = iter.try_fold(acc, fold)?;
304304
self.iter = None;
305305
}
306-
Try::from_ok(acc)
306+
try { acc }
307307
}
308308

309309
#[inline]
@@ -353,7 +353,7 @@ where
353353
acc = iter.try_rfold(acc, fold)?;
354354
self.iter = None;
355355
}
356-
Try::from_ok(acc)
356+
try { acc }
357357
}
358358

359359
#[inline]

library/core/src/iter/adapters/mod.rs

+18-22
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ where
579579
})?;
580580

581581
if is_empty {
582-
return Try::from_ok(acc);
582+
return try { acc };
583583
}
584584

585585
loop {
@@ -715,7 +715,7 @@ where
715715
if self.first_take {
716716
self.first_take = false;
717717
match self.iter.next() {
718-
None => return Try::from_ok(acc),
718+
None => return try { acc },
719719
Some(x) => acc = f(acc, x)?,
720720
}
721721
}
@@ -792,7 +792,7 @@ where
792792
}
793793

794794
match self.next_back() {
795-
None => Try::from_ok(init),
795+
None => try { init },
796796
Some(x) => {
797797
let acc = f(init, x)?;
798798
from_fn(nth_back(&mut self.iter, self.step)).try_fold(acc, f)
@@ -1075,7 +1075,7 @@ fn filter_try_fold<'a, T, Acc, R: Try<Ok = Acc>>(
10751075
predicate: &'a mut impl FnMut(&T) -> bool,
10761076
mut fold: impl FnMut(Acc, T) -> R + 'a,
10771077
) -> impl FnMut(Acc, T) -> R + 'a {
1078-
move |acc, item| if predicate(&item) { fold(acc, item) } else { R::from_ok(acc) }
1078+
move |acc, item| if predicate(&item) { fold(acc, item) } else { try { acc } }
10791079
}
10801080

10811081
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1229,7 +1229,7 @@ fn filter_map_try_fold<'a, T, B, Acc, R: Try<Ok = Acc>>(
12291229
) -> impl FnMut(Acc, T) -> R + 'a {
12301230
move |acc, item| match f(item) {
12311231
Some(x) => fold(acc, x),
1232-
None => R::from_ok(acc),
1232+
None => try { acc },
12331233
}
12341234
}
12351235

@@ -1660,7 +1660,7 @@ impl<I: Iterator> Iterator for Peekable<I> {
16601660
R: Try<Ok = B>,
16611661
{
16621662
let acc = match self.peeked.take() {
1663-
Some(None) => return Try::from_ok(init),
1663+
Some(None) => return try { init },
16641664
Some(Some(v)) => f(init, v)?,
16651665
None => init,
16661666
};
@@ -1703,7 +1703,7 @@ where
17031703
R: Try<Ok = B>,
17041704
{
17051705
match self.peeked.take() {
1706-
Some(None) => Try::from_ok(init),
1706+
Some(None) => try { init },
17071707
Some(Some(v)) => match self.iter.try_rfold(init, &mut f).into_result() {
17081708
Ok(acc) => f(acc, v),
17091709
Err(e) => {
@@ -1938,7 +1938,7 @@ where
19381938
if !self.flag {
19391939
match self.next() {
19401940
Some(v) => init = fold(init, v)?,
1941-
None => return Try::from_ok(init),
1941+
None => return try { init },
19421942
}
19431943
}
19441944
self.iter.try_fold(init, fold)
@@ -2065,13 +2065,13 @@ where
20652065
ControlFlow::from_try(fold(acc, x))
20662066
} else {
20672067
*flag = true;
2068-
ControlFlow::Break(Try::from_ok(acc))
2068+
ControlFlow::Break(try { acc })
20692069
}
20702070
}
20712071
}
20722072

20732073
if self.flag {
2074-
Try::from_ok(init)
2074+
try { init }
20752075
} else {
20762076
let flag = &mut self.flag;
20772077
let p = &mut self.predicate;
@@ -2180,7 +2180,7 @@ where
21802180
let Self { iter, predicate } = self;
21812181
iter.try_fold(init, |acc, x| match predicate(x) {
21822182
Some(item) => ControlFlow::from_try(fold(acc, item)),
2183-
None => ControlFlow::Break(Try::from_ok(acc)),
2183+
None => ControlFlow::Break(try { acc }),
21842184
})
21852185
.into_try()
21862186
}
@@ -2316,7 +2316,7 @@ where
23162316
if n > 0 {
23172317
// nth(n) skips n+1
23182318
if self.iter.nth(n - 1).is_none() {
2319-
return Try::from_ok(init);
2319+
return try { init };
23202320
}
23212321
}
23222322
self.iter.try_fold(init, fold)
@@ -2381,11 +2381,7 @@ where
23812381
}
23822382

23832383
let n = self.len();
2384-
if n == 0 {
2385-
Try::from_ok(init)
2386-
} else {
2387-
self.iter.try_rfold(init, check(n, fold)).into_try()
2388-
}
2384+
if n == 0 { try { init } } else { self.iter.try_rfold(init, check(n, fold)).into_try() }
23892385
}
23902386

23912387
fn rfold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
@@ -2509,7 +2505,7 @@ where
25092505
}
25102506

25112507
if self.n == 0 {
2512-
Try::from_ok(init)
2508+
try { init }
25132509
} else {
25142510
let n = &mut self.n;
25152511
self.iter.try_fold(init, check(n, fold)).into_try()
@@ -2587,11 +2583,11 @@ where
25872583
R: Try<Ok = Acc>,
25882584
{
25892585
if self.n == 0 {
2590-
Try::from_ok(init)
2586+
try { init }
25912587
} else {
25922588
let len = self.iter.len();
25932589
if len > self.n && self.iter.nth_back(len - self.n - 1).is_none() {
2594-
Try::from_ok(init)
2590+
try { init }
25952591
} else {
25962592
self.iter.try_rfold(init, fold)
25972593
}
@@ -2687,7 +2683,7 @@ where
26872683
mut fold: impl FnMut(Acc, B) -> R + 'a,
26882684
) -> impl FnMut(Acc, T) -> ControlFlow<Acc, R> + 'a {
26892685
move |acc, x| match f(state, x) {
2690-
None => ControlFlow::Break(Try::from_ok(acc)),
2686+
None => ControlFlow::Break(try { acc }),
26912687
Some(x) => ControlFlow::from_try(fold(acc, x)),
26922688
}
26932689
}
@@ -2951,7 +2947,7 @@ where
29512947
Ok(x) => ControlFlow::from_try(f(acc, x)),
29522948
Err(e) => {
29532949
*error = Err(e);
2954-
ControlFlow::Break(Try::from_ok(acc))
2950+
ControlFlow::Break(try { acc })
29552951
}
29562952
})
29572953
.into_try()

library/core/src/iter/range.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
713713
R: Try<Ok = B>,
714714
{
715715
if self.is_empty() {
716-
return Try::from_ok(init);
716+
return try { init };
717717
}
718718

719719
let mut accum = init;
@@ -731,7 +731,7 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
731731
accum = f(accum, self.start.clone())?;
732732
}
733733

734-
Try::from_ok(accum)
734+
try { accum }
735735
}
736736

737737
#[inline]
@@ -818,7 +818,7 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
818818
R: Try<Ok = B>,
819819
{
820820
if self.is_empty() {
821-
return Try::from_ok(init);
821+
return try { init };
822822
}
823823

824824
let mut accum = init;
@@ -836,7 +836,7 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
836836
accum = f(accum, self.start.clone())?;
837837
}
838838

839-
Try::from_ok(accum)
839+
try { accum }
840840
}
841841

842842
#[inline]

library/core/src/iter/traits/double_ended.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ pub trait DoubleEndedIterator: Iterator {
224224
while let Some(x) = self.next_back() {
225225
accum = f(accum, x)?;
226226
}
227-
Try::from_ok(accum)
227+
try { accum }
228228
}
229229

230230
/// An iterator method that reduces the iterator's elements to a single,

library/core/src/iter/traits/iterator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,7 @@ pub trait Iterator {
18871887
while let Some(x) = self.next() {
18881888
accum = f(accum, x)?;
18891889
}
1890-
Try::from_ok(accum)
1890+
try { accum }
18911891
}
18921892

18931893
/// An iterator method that applies a fallible function to each item in the

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
#![feature(str_split_as_str)]
130130
#![feature(str_split_inclusive_as_str)]
131131
#![feature(transparent_unions)]
132+
#![feature(try_blocks)]
132133
#![feature(unboxed_closures)]
133134
#![feature(unsized_locals)]
134135
#![cfg_attr(bootstrap, feature(untagged_unions))]

src/stage0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cargo: beta
2020
# bootstrapping issues with use of new syntax in this repo. If you're looking at
2121
# the beta/stable branch, this key should be omitted, as we don't want to depend
2222
# on rustfmt from nightly there.
23-
rustfmt: nightly-2020-10-07
23+
rustfmt: nightly-2020-10-12
2424

2525
# When making a stable release the process currently looks like:
2626
#

0 commit comments

Comments
 (0)