From 00da9fc961e9b7b369a950c71d88126bbefc677c Mon Sep 17 00:00:00 2001 From: Georg Semmler <github@weiznich.de> Date: Sat, 20 Jul 2024 16:45:33 +0200 Subject: [PATCH] Start using `#[diagnostic::do_not_recommend]` in the standard library This commit starts using `#[diagnostic::do_not_recommend]` in the standard library to improve some error messages. In this case we just hide a certain nightly only impl as suggested in #121521 --- library/core/src/lib.rs | 1 + library/core/src/option.rs | 1 + library/core/src/result.rs | 2 +- tests/ui/try-trait/bad-interconversion.stderr | 16 ++++------------ tests/ui/try-trait/option-to-result.stderr | 8 ++------ tests/ui/try-trait/try-on-option.stderr | 4 +--- 6 files changed, 10 insertions(+), 22 deletions(-) diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 49f89e70255..02cb02dce1d 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -165,6 +165,7 @@ #![feature(const_unsafecell_get_mut)] #![feature(const_waker)] #![feature(coverage_attribute)] +#![feature(do_not_recommend)] #![feature(duration_consts_float)] #![feature(internal_impls_macro)] #![feature(ip)] diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 1a8fe1e6051..d93cb8d10e6 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -2507,6 +2507,7 @@ impl<T> ops::FromResidual for Option<T> { } } +#[diagnostic::do_not_recommend] #[unstable(feature = "try_trait_v2_yeet", issue = "96374")] impl<T> ops::FromResidual<ops::Yeet<()>> for Option<T> { #[inline] diff --git a/library/core/src/result.rs b/library/core/src/result.rs index f8cdcc000c5..7f278296b7b 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1990,7 +1990,7 @@ impl<T, E, F: From<E>> ops::FromResidual<Result<convert::Infallible, E>> for Res } } } - +#[diagnostic::do_not_recommend] #[unstable(feature = "try_trait_v2_yeet", issue = "96374")] impl<T, E, F: From<E>> ops::FromResidual<ops::Yeet<E>> for Result<T, F> { #[inline] diff --git a/tests/ui/try-trait/bad-interconversion.stderr b/tests/ui/try-trait/bad-interconversion.stderr index c30b6334fed..642a93d64e2 100644 --- a/tests/ui/try-trait/bad-interconversion.stderr +++ b/tests/ui/try-trait/bad-interconversion.stderr @@ -23,9 +23,7 @@ LL | Some(3)?; | ^ use `.ok_or(...)?` to provide an error compatible with `Result<u64, String>` | = help: the trait `FromResidual<Option<Infallible>>` is not implemented for `Result<u64, String>` - = help: the following other types implement trait `FromResidual<R>`: - `Result<T, F>` implements `FromResidual<Result<Infallible, E>>` - `Result<T, F>` implements `FromResidual<Yeet<E>>` + = help: the trait `FromResidual<Result<Infallible, E>>` is implemented for `Result<T, F>` error[E0277]: the `?` operator can only be used on `Result`s in a function that returns `Result` --> $DIR/bad-interconversion.rs:17:31 @@ -36,9 +34,7 @@ LL | Ok(ControlFlow::Break(123)?) | ^ this `?` produces `ControlFlow<{integer}, Infallible>`, which is incompatible with `Result<u64, String>` | = help: the trait `FromResidual<ControlFlow<{integer}, Infallible>>` is not implemented for `Result<u64, String>` - = help: the following other types implement trait `FromResidual<R>`: - `Result<T, F>` implements `FromResidual<Result<Infallible, E>>` - `Result<T, F>` implements `FromResidual<Yeet<E>>` + = help: the trait `FromResidual<Result<Infallible, E>>` is implemented for `Result<T, F>` error[E0277]: the `?` operator can only be used on `Option`s, not `Result`s, in a function that returns `Option` --> $DIR/bad-interconversion.rs:22:22 @@ -49,9 +45,7 @@ LL | Some(Err("hello")?) | ^ use `.ok()?` if you want to discard the `Result<Infallible, &str>` error information | = help: the trait `FromResidual<Result<Infallible, &str>>` is not implemented for `Option<u16>` - = help: the following other types implement trait `FromResidual<R>`: - `Option<T>` implements `FromResidual<Yeet<()>>` - `Option<T>` implements `FromResidual` + = help: the trait `FromResidual` is implemented for `Option<T>` error[E0277]: the `?` operator can only be used on `Option`s in a function that returns `Option` --> $DIR/bad-interconversion.rs:27:33 @@ -62,9 +56,7 @@ LL | Some(ControlFlow::Break(123)?) | ^ this `?` produces `ControlFlow<{integer}, Infallible>`, which is incompatible with `Option<u64>` | = help: the trait `FromResidual<ControlFlow<{integer}, Infallible>>` is not implemented for `Option<u64>` - = help: the following other types implement trait `FromResidual<R>`: - `Option<T>` implements `FromResidual<Yeet<()>>` - `Option<T>` implements `FromResidual` + = help: the trait `FromResidual` is implemented for `Option<T>` error[E0277]: the `?` operator can only be used on `ControlFlow`s in a function that returns `ControlFlow` --> $DIR/bad-interconversion.rs:32:39 diff --git a/tests/ui/try-trait/option-to-result.stderr b/tests/ui/try-trait/option-to-result.stderr index 2d97226275d..8055b2a0b04 100644 --- a/tests/ui/try-trait/option-to-result.stderr +++ b/tests/ui/try-trait/option-to-result.stderr @@ -8,9 +8,7 @@ LL | a?; | ^ use `.ok_or(...)?` to provide an error compatible with `Result<(), ()>` | = help: the trait `FromResidual<Option<Infallible>>` is not implemented for `Result<(), ()>` - = help: the following other types implement trait `FromResidual<R>`: - `Result<T, F>` implements `FromResidual<Result<Infallible, E>>` - `Result<T, F>` implements `FromResidual<Yeet<E>>` + = help: the trait `FromResidual<Result<Infallible, E>>` is implemented for `Result<T, F>` error[E0277]: the `?` operator can only be used on `Option`s, not `Result`s, in a function that returns `Option` --> $DIR/option-to-result.rs:11:6 @@ -22,9 +20,7 @@ LL | a?; | ^ use `.ok()?` if you want to discard the `Result<Infallible, i32>` error information | = help: the trait `FromResidual<Result<Infallible, i32>>` is not implemented for `Option<i32>` - = help: the following other types implement trait `FromResidual<R>`: - `Option<T>` implements `FromResidual<Yeet<()>>` - `Option<T>` implements `FromResidual` + = help: the trait `FromResidual` is implemented for `Option<T>` error: aborting due to 2 previous errors diff --git a/tests/ui/try-trait/try-on-option.stderr b/tests/ui/try-trait/try-on-option.stderr index 84a51a078af..15d0b28ddc1 100644 --- a/tests/ui/try-trait/try-on-option.stderr +++ b/tests/ui/try-trait/try-on-option.stderr @@ -8,9 +8,7 @@ LL | x?; | ^ use `.ok_or(...)?` to provide an error compatible with `Result<u32, ()>` | = help: the trait `FromResidual<Option<Infallible>>` is not implemented for `Result<u32, ()>` - = help: the following other types implement trait `FromResidual<R>`: - `Result<T, F>` implements `FromResidual<Result<Infallible, E>>` - `Result<T, F>` implements `FromResidual<Yeet<E>>` + = help: the trait `FromResidual<Result<Infallible, E>>` is implemented for `Result<T, F>` error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) --> $DIR/try-on-option.rs:11:6