Skip to content

Commit

Permalink
Auto merge of #96376 - scottmcm:do-yeet, r=oli-obk
Browse files Browse the repository at this point in the history
Add `do yeet` expressions to allow experimentation in nightly

Two main goals for this:
- Ensure that trait restructuring in rust-lang/rust#84277 (comment) doesn't accidentally close us off from the possibility of doing this in future, as sketched in https://rust-lang.github.io/rfcs/3058-try-trait-v2.html#possibilities-for-yeet
- Experiment with the *existence* of syntax for this, to be able to weight the syntax-vs-library tradeoffs better than we can right now.  Notably the syntax (with `do`) and name in this PR are not intended as candidates for stabilization, but they make a good v0 PR for adding this with minimal impact to compiler maintenance or priming one possible name choice over another.

r? `@oli-obk`
The lang `second` for doing this: rust-lang/lang-team#160 (comment)

Tracking issues
- Lang, rust-lang/rust#96373
- Libs-api, rust-lang/rust#96374
  • Loading branch information
bors committed May 1, 2022
2 parents 2a9df2d + b7c2199 commit 1f43f5f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ pub use self::range::OneSidedRange;
#[unstable(feature = "try_trait_v2", issue = "84277")]
pub use self::try_trait::{FromResidual, Try};

#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
pub use self::try_trait::Yeet;

#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
pub use self::try_trait::Residual;

Expand Down
22 changes: 22 additions & 0 deletions core/src/ops/try_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,22 @@ pub trait FromResidual<R = <Self as Try>::Residual> {
fn from_residual(residual: R) -> Self;
}

#[cfg(not(bootstrap))]
#[unstable(
feature = "yeet_desugar_details",
issue = "none",
reason = "just here to simplify the desugaring; will never be stabilized"
)]
#[inline]
#[track_caller] // because `Result::from_residual` has it
#[lang = "from_yeet"]
pub fn from_yeet<T, Y>(yeeted: Y) -> T
where
T: FromResidual<Yeet<Y>>,
{
FromResidual::from_residual(Yeet(yeeted))
}

/// Allows retrieving the canonical type implementing [`Try`] that has this type
/// as its residual and allows it to hold an `O` as its output.
///
Expand Down Expand Up @@ -395,3 +411,9 @@ impl<T> FromResidual for NeverShortCircuit<T> {
impl<T> Residual<T> for NeverShortCircuitResidual {
type TryType = NeverShortCircuit<T>;
}

/// Implement `FromResidual<Yeet<T>>` on your type to enable
/// `do yeet expr` syntax in functions returning your type.
#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
#[derive(Debug)]
pub struct Yeet<T>(pub T);
8 changes: 8 additions & 0 deletions core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2287,6 +2287,14 @@ impl<T> const ops::FromResidual for Option<T> {
}
}

#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
impl<T> ops::FromResidual<ops::Yeet<()>> for Option<T> {
#[inline]
fn from_residual(ops::Yeet(()): ops::Yeet<()>) -> Self {
None
}
}

#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
impl<T> ops::Residual<T> for Option<convert::Infallible> {
type TryType = Option<T>;
Expand Down
8 changes: 8 additions & 0 deletions core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2107,6 +2107,14 @@ impl<T, E, F: ~const From<E>> const ops::FromResidual<Result<convert::Infallible
}
}

#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
impl<T, E, F: From<E>> ops::FromResidual<ops::Yeet<E>> for Result<T, F> {
#[inline]
fn from_residual(ops::Yeet(e): ops::Yeet<E>) -> Self {
Err(From::from(e))
}
}

#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
impl<T, E> ops::Residual<T> for Result<convert::Infallible, E> {
type TryType = Result<T, E>;
Expand Down

0 comments on commit 1f43f5f

Please sign in to comment.