Skip to content

Commit 076301c

Browse files
authored
Rollup merge of rust-lang#148725 - scottmcm:experiment-new-try-block-v3, r=petrochenkov
Implement the alternative `try` block desugaring As discussed in rust-lang/rfcs#3721 (comment), update the `try` in nightly to match the RFC as a way to experiment. This addresses the following unresolved issue from rust-lang#31436 > Address issues with type inference (`try { expr? }?` currently requires an explicit type annotation somewhere).
2 parents 5207380 + 52d7065 commit 076301c

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

core/src/ops/try_trait.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,24 @@ where
359359
/// and in the other direction,
360360
/// `<Result<Infallible, E> as Residual<T>>::TryType = Result<T, E>`.
361361
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
362-
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
363-
pub const trait Residual<O> {
362+
#[rustc_const_unstable(feature = "const_try_residual", issue = "91285")]
363+
pub const trait Residual<O>: Sized {
364364
/// The "return" type of this meta-function.
365365
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
366-
type TryType: Try<Output = O, Residual = Self>;
366+
type TryType: [const] Try<Output = O, Residual = Self>;
367+
}
368+
369+
/// Used in `try {}` blocks so the type produced in the `?` desugaring
370+
/// depends on the residual type `R` and the output type of the block `O`,
371+
/// but importantly not on the contextual type the way it would be if
372+
/// we called `<_ as FromResidual>::from_residual(r)` directly.
373+
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
374+
// needs to be `pub` to avoid `private type` errors
375+
#[expect(unreachable_pub)]
376+
#[inline] // FIXME: force would be nice, but fails -- see #148915
377+
#[lang = "into_try_type"]
378+
pub fn residual_into_try_type<R: Residual<O>, O>(r: R) -> <R as Residual<O>>::TryType {
379+
FromResidual::from_residual(r)
367380
}
368381

369382
#[unstable(feature = "pub_crate_should_not_need_unstable_attr", issue = "none")]

std/src/sys/pal/unix/kernel_copy/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn copy_specialization() -> Result<()> {
5050
"inner Take allowed reading beyond end of file, some bytes should be left"
5151
);
5252

53-
let mut sink = sink.into_inner()?;
53+
let mut sink = sink.into_inner().map_err(io::Error::from)?;
5454
sink.seek(SeekFrom::Start(0))?;
5555
let mut copied = Vec::new();
5656
sink.read_to_end(&mut copied)?;

0 commit comments

Comments
 (0)