Skip to content

Commit cb71243

Browse files
committed
split try trait in twain
1 parent 8da2d28 commit cb71243

52 files changed

Lines changed: 507 additions & 255 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,8 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
564564
hir::ExprKind::Loop(block, opt_label, hir::LoopSource::While, span)
565565
}
566566

567-
/// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_output(<expr>) }`,
568-
/// `try { <stmts>; }` into `{ <stmts>; ::std::ops::Try::from_output(()) }`
567+
/// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::FromOutput::from_output(<expr>) }`,
568+
/// `try { <stmts>; }` into `{ <stmts>; ::std::ops::FromOutput::from_output(()) }`
569569
/// and save the block id to use it as a break target for desugaring of the `?` operator.
570570
fn lower_expr_try_block(&mut self, body: &Block, opt_ty: Option<&Ty>) -> hir::ExprKind<'hir> {
571571
let body_hir_id = self.lower_node_id(body.id);
@@ -600,7 +600,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
600600
let ok_wrapped_span =
601601
this.mark_span_with_reason(DesugaringKind::TryBlock, tail_expr.span, None);
602602

603-
// `::std::ops::Try::from_output($tail_expr)`
603+
// `::std::ops::FromOutput::from_output($tail_expr)`
604604
block.expr = Some(this.wrap_in_try_constructor(
605605
hir::LangItem::TryTraitFromOutput,
606606
try_span,

compiler/rustc_middle/src/mir/interpret/error.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ pub struct InterpResult<'tcx, T = ()> {
941941
res: Result<T, InterpErrorInfo<'tcx>>,
942942
guard: Guard,
943943
}
944-
944+
#[cfg(bootstrap)]
945945
impl<'tcx, T> ops::Try for InterpResult<'tcx, T> {
946946
type Output = T;
947947
type Residual = InterpResult<'tcx, convert::Infallible>;
@@ -959,6 +959,26 @@ impl<'tcx, T> ops::Try for InterpResult<'tcx, T> {
959959
}
960960
}
961961
}
962+
#[cfg(not(bootstrap))]
963+
impl<'tcx, T> ops::Branch for InterpResult<'tcx, T> {
964+
type Output = T;
965+
type Residual = InterpResult<'tcx, convert::Infallible>;
966+
967+
#[inline]
968+
fn branch(self) -> ops::ControlFlow<Self::Residual, Self::Output> {
969+
match self.disarm() {
970+
Ok(v) => ops::ControlFlow::Continue(v),
971+
Err(e) => ops::ControlFlow::Break(InterpResult::new(Err(e))),
972+
}
973+
}
974+
}
975+
#[cfg(not(bootstrap))]
976+
impl<'tcx, T> ops::FromOutput for InterpResult<'tcx, T> {
977+
#[inline]
978+
fn from_output(output: Self::Output) -> Self {
979+
InterpResult::new(Ok(output))
980+
}
981+
}
962982

963983
impl<'tcx, T> ops::Residual<T> for InterpResult<'tcx, convert::Infallible> {
964984
type TryType = InterpResult<'tcx, T>;

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ symbols! {
225225
Forward,
226226
From,
227227
FromIterator,
228+
FromOutput,
228229
FromResidual,
229230
GlobalAlloc,
230231
Hash,

compiler/rustc_trait_selection/src/error_reporting/traits/call_kind.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ impl CallDesugaringKind {
3232
match self {
3333
Self::ForLoopIntoIter => tcx.get_diagnostic_item(sym::IntoIterator).unwrap(),
3434
Self::ForLoopNext => tcx.require_lang_item(LangItem::Iterator, DUMMY_SP),
35-
Self::QuestionBranch | Self::TryBlockFromOutput => {
36-
tcx.require_lang_item(LangItem::Try, DUMMY_SP)
37-
}
35+
Self::TryBlockFromOutput => tcx.get_diagnostic_item(sym::FromOutput).unwrap(),
36+
Self::QuestionBranch => tcx.require_lang_item(LangItem::Try, DUMMY_SP),
3837
Self::QuestionFromResidual => tcx.get_diagnostic_item(sym::FromResidual).unwrap(),
3938
Self::Await => tcx.get_diagnostic_item(sym::IntoFuture).unwrap(),
4039
}

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
207207
ObligationCauseCode::QuestionMark,
208208
) && !(
209209
self.tcx.is_diagnostic_item(sym::FromResidual, main_trait_predicate.def_id())
210+
|| self.tcx.is_diagnostic_item(sym::FromOutput, main_trait_predicate.def_id())
210211
|| self.tcx.is_lang_item(main_trait_predicate.def_id(), LangItem::Try)
211212
);
212213
let is_unsize =
@@ -392,6 +393,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
392393
// `std::marker::Sized` is not implemented for `T`" as we will point
393394
// at the type param with a label to suggest constraining it.
394395
&& !self.tcx.is_diagnostic_item(sym::FromResidual, leaf_trait_predicate.def_id())
396+
&& !self.tcx.is_diagnostic_item(sym::FromOutput, leaf_trait_predicate.def_id())
395397
// Don't say "the trait `FromResidual<Option<Infallible>>` is
396398
// not implemented for `Result<T, E>`".
397399
{
@@ -2214,7 +2216,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
22142216
candidates = specific_candidates;
22152217
}
22162218
if let &[(cand, def_id)] = &candidates[..] {
2217-
if self.tcx.is_diagnostic_item(sym::FromResidual, cand.def_id)
2219+
if (self.tcx.is_diagnostic_item(sym::FromResidual, cand.def_id)
2220+
|| self.tcx.is_diagnostic_item(sym::FromOutput, cand.def_id))
22182221
&& !self.tcx.features().enabled(sym::try_trait_v2)
22192222
{
22202223
return false;

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3852,8 +3852,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
38523852
let mut parent_trait_pred =
38533853
self.resolve_vars_if_possible(data.derived.parent_trait_pred);
38543854
let parent_def_id = parent_trait_pred.def_id();
3855-
if tcx.is_diagnostic_item(sym::FromResidual, parent_def_id)
3856-
&& !tcx.features().enabled(sym::try_trait_v2)
3855+
if (self.tcx.is_diagnostic_item(sym::FromResidual, parent_def_id)
3856+
|| self.tcx.is_diagnostic_item(sym::FromOutput, parent_def_id))
3857+
&& !self.tcx.features().enabled(sym::try_trait_v2)
38573858
{
38583859
// If `#![feature(try_trait_v2)]` is not enabled, then there's no point on
38593860
// talking about `FromResidual<Result<A, B>>`, as the end user has nothing they

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::iter::InPlaceIterable;
22
use crate::num::NonZero;
3-
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try};
3+
use crate::ops::{Branch, ChangeOutputType, ControlFlow, FromOutput, FromResidual, Residual, Try};
44

55
mod array_chunks;
66
mod by_ref_sized;
@@ -163,15 +163,15 @@ where
163163
let value = f(shunt);
164164
match residual {
165165
Some(r) => FromResidual::from_residual(r),
166-
None => Try::from_output(value),
166+
None => FromOutput::from_output(value),
167167
}
168168
}
169169

170170
impl<I, R> Iterator for GenericShunt<'_, I, R>
171171
where
172172
I: Iterator<Item: Try<Residual = R>>,
173173
{
174-
type Item = <I::Item as Try>::Output;
174+
type Item = <I::Item as Branch>::Output;
175175

176176
fn next(&mut self) -> Option<Self::Item> {
177177
self.try_for_each(ControlFlow::Break).break_value()
@@ -192,7 +192,7 @@ where
192192
T: Try<Output = B>,
193193
{
194194
self.iter
195-
.try_fold(init, |acc, x| match Try::branch(x) {
195+
.try_fold(init, |acc, x| match x.branch() {
196196
ControlFlow::Continue(x) => ControlFlow::from_try(f(acc, x)),
197197
ControlFlow::Break(r) => {
198198
*self.residual = Some(r);

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::array;
99
use crate::cmp::{self, Ordering};
1010
use crate::marker::Destruct;
1111
use crate::num::NonZero;
12-
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try};
12+
use crate::ops::{Branch, ChangeOutputType, ControlFlow, FromOutput, FromResidual, Residual, Try};
1313

1414
fn _assert_is_dyn_compatible(_: &dyn Iterator<Item = ()>) {}
1515

@@ -2168,7 +2168,7 @@ pub const trait Iterator {
21682168
where
21692169
Self: Sized,
21702170
Self::Item: Try<Residual: Residual<B>>,
2171-
B: FromIterator<<Self::Item as Try>::Output>,
2171+
B: FromIterator<<Self::Item as Branch>::Output>,
21722172
{
21732173
try_process(ByRefSized(self), |i| i.collect())
21742174
}
@@ -2781,12 +2781,12 @@ pub const trait Iterator {
27812781
{
27822782
let first = match self.next() {
27832783
Some(i) => i,
2784-
None => return Try::from_output(None),
2784+
None => return FromOutput::from_output(None),
27852785
};
27862786

27872787
match self.try_fold(first, f).branch() {
27882788
ControlFlow::Break(r) => FromResidual::from_residual(r),
2789-
ControlFlow::Continue(i) => Try::from_output(Some(i)),
2789+
ControlFlow::Continue(i) => FromOutput::from_output(Some(i)),
27902790
}
27912791
}
27922792

@@ -3067,14 +3067,14 @@ pub const trait Iterator {
30673067
{
30683068
move |(), x| match f(&x).branch() {
30693069
ControlFlow::Continue(false) => ControlFlow::Continue(()),
3070-
ControlFlow::Continue(true) => ControlFlow::Break(Try::from_output(Some(x))),
3071-
ControlFlow::Break(r) => ControlFlow::Break(FromResidual::from_residual(r)),
3070+
ControlFlow::Continue(true) => ControlFlow::Break(<_>::from_output(Some(x))),
3071+
ControlFlow::Break(r) => ControlFlow::Break(<_>::from_residual(r)),
30723072
}
30733073
}
30743074

30753075
match self.try_fold((), check(f)) {
30763076
ControlFlow::Break(x) => x,
3077-
ControlFlow::Continue(()) => Try::from_output(None),
3077+
ControlFlow::Continue(()) => <_>::from_output(None),
30783078
}
30793079
}
30803080

library/core/src/ops/control_flow.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,10 @@ pub enum ControlFlow<B, C = ()> {
102102

103103
#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
104104
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
105-
impl<B, C> const ops::Try for ControlFlow<B, C> {
105+
impl<B, C> const ops::Branch for ControlFlow<B, C> {
106106
type Output = C;
107107
type Residual = ControlFlow<B, convert::Infallible>;
108108

109-
#[inline]
110-
fn from_output(output: Self::Output) -> Self {
111-
ControlFlow::Continue(output)
112-
}
113-
114109
#[inline]
115110
fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
116111
match self {
@@ -119,7 +114,14 @@ impl<B, C> const ops::Try for ControlFlow<B, C> {
119114
}
120115
}
121116
}
122-
117+
#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
118+
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
119+
impl<B, C> const ops::FromOutput for ControlFlow<B, C> {
120+
#[inline]
121+
fn from_output(output: Self::Output) -> Self {
122+
ControlFlow::Continue(output)
123+
}
124+
}
123125
#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
124126
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
125127
// Note: manually specifying the residual type instead of using the default to work around

library/core/src/ops/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ pub use self::reborrow::{CoerceShared, Reborrow};
196196
pub use self::try_trait::Residual;
197197
#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
198198
pub use self::try_trait::Yeet;
199-
pub(crate) use self::try_trait::{ChangeOutputType, NeverShortCircuit};
200199
#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
201-
pub use self::try_trait::{FromResidual, Try};
200+
pub use self::try_trait::{Branch, FromOutput, FromResidual, Try};
201+
pub(crate) use self::try_trait::{ChangeOutputType, NeverShortCircuit};
202202
#[unstable(feature = "coerce_unsized", issue = "18598")]
203203
pub use self::unsize::CoerceUnsized;
204204
#[unstable(feature = "dispatch_from_dyn", issue = "none")]

0 commit comments

Comments
 (0)