Skip to content

Commit

Permalink
Rollup merge of #128008 - weiznich:fix/121521, r=lcnr
Browse files Browse the repository at this point in the history
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

The result in not perfect yet, but at least the `Yeet` suggestion is not shown anymore. I would consider that as a minor improvement.
  • Loading branch information
tgross35 authored Jul 22, 2024
2 parents a7e884f + 00da9fc commit 8ee5e27
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 22 deletions.
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
16 changes: 4 additions & 12 deletions tests/ui/try-trait/bad-interconversion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions tests/ui/try-trait/option-to-result.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 1 addition & 3 deletions tests/ui/try-trait/try-on-option.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8ee5e27

Please sign in to comment.