Skip to content

Commit 1f43f5f

Browse files
committed
Auto merge of #96376 - scottmcm:do-yeet, r=oli-obk
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
2 parents 2a9df2d + b7c2199 commit 1f43f5f

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

core/src/ops/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ pub use self::range::OneSidedRange;
187187
#[unstable(feature = "try_trait_v2", issue = "84277")]
188188
pub use self::try_trait::{FromResidual, Try};
189189

190+
#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
191+
pub use self::try_trait::Yeet;
192+
190193
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
191194
pub use self::try_trait::Residual;
192195

core/src/ops/try_trait.rs

+22
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,22 @@ pub trait FromResidual<R = <Self as Try>::Residual> {
330330
fn from_residual(residual: R) -> Self;
331331
}
332332

333+
#[cfg(not(bootstrap))]
334+
#[unstable(
335+
feature = "yeet_desugar_details",
336+
issue = "none",
337+
reason = "just here to simplify the desugaring; will never be stabilized"
338+
)]
339+
#[inline]
340+
#[track_caller] // because `Result::from_residual` has it
341+
#[lang = "from_yeet"]
342+
pub fn from_yeet<T, Y>(yeeted: Y) -> T
343+
where
344+
T: FromResidual<Yeet<Y>>,
345+
{
346+
FromResidual::from_residual(Yeet(yeeted))
347+
}
348+
333349
/// Allows retrieving the canonical type implementing [`Try`] that has this type
334350
/// as its residual and allows it to hold an `O` as its output.
335351
///
@@ -395,3 +411,9 @@ impl<T> FromResidual for NeverShortCircuit<T> {
395411
impl<T> Residual<T> for NeverShortCircuitResidual {
396412
type TryType = NeverShortCircuit<T>;
397413
}
414+
415+
/// Implement `FromResidual<Yeet<T>>` on your type to enable
416+
/// `do yeet expr` syntax in functions returning your type.
417+
#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
418+
#[derive(Debug)]
419+
pub struct Yeet<T>(pub T);

core/src/option.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2287,6 +2287,14 @@ impl<T> const ops::FromResidual for Option<T> {
22872287
}
22882288
}
22892289

2290+
#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
2291+
impl<T> ops::FromResidual<ops::Yeet<()>> for Option<T> {
2292+
#[inline]
2293+
fn from_residual(ops::Yeet(()): ops::Yeet<()>) -> Self {
2294+
None
2295+
}
2296+
}
2297+
22902298
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
22912299
impl<T> ops::Residual<T> for Option<convert::Infallible> {
22922300
type TryType = Option<T>;

core/src/result.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2107,6 +2107,14 @@ impl<T, E, F: ~const From<E>> const ops::FromResidual<Result<convert::Infallible
21072107
}
21082108
}
21092109

2110+
#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
2111+
impl<T, E, F: From<E>> ops::FromResidual<ops::Yeet<E>> for Result<T, F> {
2112+
#[inline]
2113+
fn from_residual(ops::Yeet(e): ops::Yeet<E>) -> Self {
2114+
Err(From::from(e))
2115+
}
2116+
}
2117+
21102118
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
21112119
impl<T, E> ops::Residual<T> for Result<convert::Infallible, E> {
21122120
type TryType = Result<T, E>;

0 commit comments

Comments
 (0)