diff --git a/library/core/src/option.rs b/library/core/src/option.rs index bca73cb770fbb..4ac1cbd8a1a79 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -2306,6 +2306,19 @@ impl const ops::FromResidual for Option { } } +#[unstable(feature = "try_trait_v2", issue = "84277")] +#[rustc_const_unstable(feature = "const_convert", issue = "88674")] +impl> const ops::FromResidual> + for Option> +{ + #[inline] + fn from_residual(residual: Result) -> Self { + match residual { + Err(e) => Some(Err(From::from(e))), + } + } +} + #[unstable(feature = "try_trait_v2_yeet", issue = "96374")] impl ops::FromResidual> for Option { #[inline] diff --git a/library/core/tests/option.rs b/library/core/tests/option.rs index 9f5e537dcefc0..0d13a33e5f0ff 100644 --- a/library/core/tests/option.rs +++ b/library/core/tests/option.rs @@ -553,3 +553,14 @@ fn zip_unzip_roundtrip() { let a = z.unzip(); assert_eq!(a, (x, y)); } + +#[test] +fn from_residual_option_result() { + fn test Result>(f: F) -> Option> { + let x: u8 = f()?; + Some(Ok(x * 2)) + } + + assert_eq!(test(|| Ok(2)), Some(Ok(4))); + assert_eq!(test(|| Err(2)), Some(Err(2))); +} diff --git a/src/test/ui/try-trait/bad-interconversion.stderr b/src/test/ui/try-trait/bad-interconversion.stderr index 1dbf3ebdf827c..97792246354e3 100644 --- a/src/test/ui/try-trait/bad-interconversion.stderr +++ b/src/test/ui/try-trait/bad-interconversion.stderr @@ -62,6 +62,7 @@ LL | | } | = help: the trait `FromResidual>` is not implemented for `Option` = help: the following other types implement trait `FromResidual`: + > as FromResidual>> as FromResidual>> as FromResidual> @@ -77,6 +78,7 @@ LL | | } | = help: the trait `FromResidual>` is not implemented for `Option` = help: the following other types implement trait `FromResidual`: + > as FromResidual>> as FromResidual>> as FromResidual> diff --git a/src/test/ui/try-trait/option-to-result.stderr b/src/test/ui/try-trait/option-to-result.stderr index ae5c3ad628281..7b1486021dfc0 100644 --- a/src/test/ui/try-trait/option-to-result.stderr +++ b/src/test/ui/try-trait/option-to-result.stderr @@ -27,6 +27,7 @@ LL | | } | = help: the trait `FromResidual>` is not implemented for `Option` = help: the following other types implement trait `FromResidual`: + > as FromResidual>> as FromResidual>> as FromResidual>