Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,8 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
hir::ExprKind::Loop(block, opt_label, hir::LoopSource::While, span)
}

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

// `::std::ops::Try::from_output($tail_expr)`
// `::std::ops::FromOutput::from_output($tail_expr)`
block.expr = Some(this.wrap_in_try_constructor(
hir::LangItem::TryTraitFromOutput,
try_span,
Expand Down
22 changes: 21 additions & 1 deletion compiler/rustc_middle/src/mir/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ pub struct InterpResult<'tcx, T = ()> {
res: Result<T, InterpErrorInfo<'tcx>>,
guard: Guard,
}

#[cfg(bootstrap)]
impl<'tcx, T> ops::Try for InterpResult<'tcx, T> {
type Output = T;
type Residual = InterpResult<'tcx, convert::Infallible>;
Expand All @@ -959,6 +959,26 @@ impl<'tcx, T> ops::Try for InterpResult<'tcx, T> {
}
}
}
#[cfg(not(bootstrap))]
impl<'tcx, T> ops::Branch for InterpResult<'tcx, T> {
type Output = T;
type Residual = InterpResult<'tcx, convert::Infallible>;

#[inline]
fn branch(self) -> ops::ControlFlow<Self::Residual, Self::Output> {
match self.disarm() {
Ok(v) => ops::ControlFlow::Continue(v),
Err(e) => ops::ControlFlow::Break(InterpResult::new(Err(e))),
}
}
}
#[cfg(not(bootstrap))]
impl<'tcx, T> ops::FromOutput for InterpResult<'tcx, T> {
#[inline]
fn from_output(output: Self::Output) -> Self {
InterpResult::new(Ok(output))
}
}

impl<'tcx, T> ops::Residual<T> for InterpResult<'tcx, convert::Infallible> {
type TryType = InterpResult<'tcx, T>;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ symbols! {
Forward,
From,
FromIterator,
FromOutput,
FromResidual,
GlobalAlloc,
Hash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ impl CallDesugaringKind {
match self {
Self::ForLoopIntoIter => tcx.get_diagnostic_item(sym::IntoIterator).unwrap(),
Self::ForLoopNext => tcx.require_lang_item(LangItem::Iterator, DUMMY_SP),
Self::QuestionBranch | Self::TryBlockFromOutput => {
tcx.require_lang_item(LangItem::Try, DUMMY_SP)
}
Self::TryBlockFromOutput => tcx.get_diagnostic_item(sym::FromOutput).unwrap(),
Self::QuestionBranch => tcx.require_lang_item(LangItem::Try, DUMMY_SP),
Self::QuestionFromResidual => tcx.get_diagnostic_item(sym::FromResidual).unwrap(),
Self::Await => tcx.get_diagnostic_item(sym::IntoFuture).unwrap(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
ObligationCauseCode::QuestionMark,
) && !(
self.tcx.is_diagnostic_item(sym::FromResidual, main_trait_predicate.def_id())
|| self.tcx.is_diagnostic_item(sym::FromOutput, main_trait_predicate.def_id())
|| self.tcx.is_lang_item(main_trait_predicate.def_id(), LangItem::Try)
);
let is_unsize =
Expand Down Expand Up @@ -399,6 +400,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
// `std::marker::Sized` is not implemented for `T`" as we will point
// at the type param with a label to suggest constraining it.
&& !self.tcx.is_diagnostic_item(sym::FromResidual, leaf_trait_predicate.def_id())
&& !self.tcx.is_diagnostic_item(sym::FromOutput, leaf_trait_predicate.def_id())
// Don't say "the trait `FromResidual<Option<Infallible>>` is
// not implemented for `Result<T, E>`".
{
Expand Down Expand Up @@ -2264,7 +2266,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
candidates = specific_candidates;
}
if let &[(cand, def_id)] = &candidates[..] {
if self.tcx.is_diagnostic_item(sym::FromResidual, cand.def_id)
if (self.tcx.is_diagnostic_item(sym::FromResidual, cand.def_id)
|| self.tcx.is_diagnostic_item(sym::FromOutput, cand.def_id))
&& !self.tcx.features().enabled(sym::try_trait_v2)
{
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3582,8 +3582,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let mut parent_trait_pred =
self.resolve_vars_if_possible(data.derived.parent_trait_pred);
let parent_def_id = parent_trait_pred.def_id();
if tcx.is_diagnostic_item(sym::FromResidual, parent_def_id)
&& !tcx.features().enabled(sym::try_trait_v2)
if (self.tcx.is_diagnostic_item(sym::FromResidual, parent_def_id)
|| self.tcx.is_diagnostic_item(sym::FromOutput, parent_def_id))
&& !self.tcx.features().enabled(sym::try_trait_v2)
{
// If `#![feature(try_trait_v2)]` is not enabled, then there's no point on
// talking about `FromResidual<Result<A, B>>`, as the end user has nothing they
Expand Down
8 changes: 4 additions & 4 deletions library/core/src/iter/adapters/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::iter::InPlaceIterable;
use crate::num::NonZero;
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try};
use crate::ops::{Branch, ChangeOutputType, ControlFlow, FromOutput, FromResidual, Residual, Try};

mod array_chunks;
mod by_ref_sized;
Expand Down Expand Up @@ -163,15 +163,15 @@ where
let value = f(shunt);
match residual {
Some(r) => FromResidual::from_residual(r),
None => Try::from_output(value),
None => FromOutput::from_output(value),
}
}

impl<I, R> Iterator for GenericShunt<'_, I, R>
where
I: Iterator<Item: Try<Residual = R>>,
{
type Item = <I::Item as Try>::Output;
type Item = <I::Item as Branch>::Output;

fn next(&mut self) -> Option<Self::Item> {
self.try_for_each(ControlFlow::Break).break_value()
Expand All @@ -192,7 +192,7 @@ where
T: Try<Output = B>,
{
self.iter
.try_fold(init, |acc, x| match Try::branch(x) {
.try_fold(init, |acc, x| match x.branch() {
ControlFlow::Continue(x) => ControlFlow::from_try(f(acc, x)),
ControlFlow::Break(r) => {
*self.residual = Some(r);
Expand Down
14 changes: 7 additions & 7 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::TrustedLen;
use crate::array;
use crate::cmp::{self, Ordering};
use crate::num::NonZero;
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try};
use crate::ops::{Branch, ChangeOutputType, ControlFlow, FromOutput, FromResidual, Residual, Try};

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

Expand Down Expand Up @@ -2180,7 +2180,7 @@ pub const trait Iterator {
where
Self: Sized,
Self::Item: Try<Residual: Residual<B>>,
B: FromIterator<<Self::Item as Try>::Output>,
B: FromIterator<<Self::Item as Branch>::Output>,
{
try_process(ByRefSized(self), |i| i.collect())
}
Expand Down Expand Up @@ -2797,12 +2797,12 @@ pub const trait Iterator {
{
let first = match self.next() {
Some(i) => i,
None => return Try::from_output(None),
None => return FromOutput::from_output(None),
};

match self.try_fold(first, f).branch() {
ControlFlow::Break(r) => FromResidual::from_residual(r),
ControlFlow::Continue(i) => Try::from_output(Some(i)),
ControlFlow::Continue(i) => FromOutput::from_output(Some(i)),
}
}

Expand Down Expand Up @@ -3083,14 +3083,14 @@ pub const trait Iterator {
{
move |(), x| match f(&x).branch() {
ControlFlow::Continue(false) => ControlFlow::Continue(()),
ControlFlow::Continue(true) => ControlFlow::Break(Try::from_output(Some(x))),
ControlFlow::Break(r) => ControlFlow::Break(FromResidual::from_residual(r)),
ControlFlow::Continue(true) => ControlFlow::Break(<_>::from_output(Some(x))),
ControlFlow::Break(r) => ControlFlow::Break(<_>::from_residual(r)),
}
}

match self.try_fold((), check(f)) {
ControlFlow::Break(x) => x,
ControlFlow::Continue(()) => Try::from_output(None),
ControlFlow::Continue(()) => <_>::from_output(None),
}
}

Expand Down
16 changes: 9 additions & 7 deletions library/core/src/ops/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,10 @@ pub enum ControlFlow<B, C = ()> {

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

#[inline]
fn from_output(output: Self::Output) -> Self {
ControlFlow::Continue(output)
}

#[inline]
fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
match self {
Expand All @@ -119,7 +114,14 @@ impl<B, C> const ops::Try for ControlFlow<B, C> {
}
}
}

#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<B, C> const ops::FromOutput for ControlFlow<B, C> {
#[inline]
fn from_output(output: Self::Output) -> Self {
ControlFlow::Continue(output)
}
}
#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
// Note: manually specifying the residual type instead of using the default to work around
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ pub use self::reborrow::{CoerceShared, Reborrow};
pub use self::try_trait::Residual;
#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
pub use self::try_trait::Yeet;
pub(crate) use self::try_trait::{ChangeOutputType, NeverShortCircuit};
#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
pub use self::try_trait::{FromResidual, Try};
pub use self::try_trait::{Branch, FromOutput, FromResidual, Try};
pub(crate) use self::try_trait::{ChangeOutputType, NeverShortCircuit};
#[unstable(feature = "coerce_unsized", issue = "18598")]
pub use self::unsize::CoerceUnsized;
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
Expand Down
Loading
Loading