Skip to content

Commit

Permalink
Remove the option-to-result back-compat hack
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmcm committed Feb 21, 2021
1 parent 4a07acb commit 508ed8d
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 71 deletions.
25 changes: 0 additions & 25 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1650,28 +1650,3 @@ impl<T, E, F: From<E>> ops::FromResidual<Result<!, E>> for Result<T, F> {
}
}
}

mod sadness {
use super::*;

/// This is a remnant of the old `NoneError` which is never going to be stabilized.
/// It's here as a snapshot of an oversight that allowed this to work in the past,
/// so we're stuck supporting it even though we'd really rather not.
#[unstable(feature = "legacy_try_trait", issue = "none")]
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
pub struct PleaseCallTheOkOrMethodToUseQuestionMarkOnOptionsInAMethodThatReturnsResult;

#[unstable(feature = "try_trait_v2", issue = "42327")]
impl<T, E> ops::FromResidual<Option<!>> for Result<T, E>
where
E: From<PleaseCallTheOkOrMethodToUseQuestionMarkOnOptionsInAMethodThatReturnsResult>,
{
fn from_residual(x: Option<!>) -> Self {
match x {
None => Err(From::from(
PleaseCallTheOkOrMethodToUseQuestionMarkOnOptionsInAMethodThatReturnsResult,
)),
}
}
}
}
2 changes: 1 addition & 1 deletion src/test/ui/option-to-result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ fn main(){ }

fn test_result() -> Result<(),()> {
let a:Option<()> = Some(());
a?;//~ ERROR `?` couldn't convert the error to `()`
a?;//~ ERROR the `?` operator can only be used in a function that returns `Result` or `Option`
Ok(())
}

Expand Down
17 changes: 9 additions & 8 deletions src/test/ui/option-to-result.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
error[E0277]: `?` couldn't convert the error to `()`
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> $DIR/option-to-result.rs:5:5
|
LL | fn test_result() -> Result<(),()> {
| ------------- expected `()` because of this
LL | let a:Option<()> = Some(());
LL | a?;
| ^^ the trait `From<result::sadness::PleaseCallTheOkOrMethodToUseQuestionMarkOnOptionsInAMethodThatReturnsResult>` is not implemented for `()`
LL | / fn test_result() -> Result<(),()> {
LL | | let a:Option<()> = Some(());
LL | | a?;
| | ^^ cannot use the `?` operator in a function that returns `Result<(), ()>`
LL | | Ok(())
LL | | }
| |_- this function should return `Result` or `Option` to accept `?`
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= note: required because of the requirements on the impl of `FromResidual<Option<!>>` for `Result<(), ()>`
= help: the trait `FromResidual<Option<!>>` is not implemented for `Result<(), ()>`
= note: required by `from_residual`

error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/try-block/try-block-bad-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn main() {
let res: Result<i32, i32> = try { }; //~ ERROR type mismatch

let res: () = try { };
//~^ ERROR the trait bound `(): Try` is not satisfied
//~^ ERROR the trait bound `(): Try2021` is not satisfied

let res: i32 = try { 5 }; //~ ERROR the trait bound `i32: Try` is not satisfied
let res: i32 = try { 5 }; //~ ERROR the trait bound `i32: Try2021` is not satisfied
}
12 changes: 6 additions & 6 deletions src/test/ui/try-block/try-block-bad-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ LL | Err("")?;
= note: required because of the requirements on the impl of `FromResidual<Result<!, &str>>` for `Result<u32, TryFromSliceError>`
= note: required by `from_residual`

error[E0271]: type mismatch resolving `<Result<i32, i32> as Try>::Ok == &str`
error[E0271]: type mismatch resolving `<Result<i32, i32> as Try2021>::Ok == &str`
--> $DIR/try-block-bad-type.rs:12:9
|
LL | ""
| ^^ expected `i32`, found `&str`

error[E0271]: type mismatch resolving `<Result<i32, i32> as Try>::Ok == ()`
error[E0271]: type mismatch resolving `<Result<i32, i32> as Try2021>::Ok == ()`
--> $DIR/try-block-bad-type.rs:15:39
|
LL | let res: Result<i32, i32> = try { };
| ^ expected `i32`, found `()`

error[E0277]: the trait bound `(): Try` is not satisfied
error[E0277]: the trait bound `(): Try2021` is not satisfied
--> $DIR/try-block-bad-type.rs:17:25
|
LL | let res: () = try { };
| ^ the trait `Try` is not implemented for `()`
| ^ the trait `Try2021` is not implemented for `()`
|
= note: required by `from_output`

error[E0277]: the trait bound `i32: Try` is not satisfied
error[E0277]: the trait bound `i32: Try2021` is not satisfied
--> $DIR/try-block-bad-type.rs:20:26
|
LL | let res: i32 = try { 5 };
| ^ the trait `Try` is not implemented for `i32`
| ^ the trait `Try2021` is not implemented for `i32`
|
= note: required by `from_output`

Expand Down
20 changes: 0 additions & 20 deletions src/test/ui/try-trait/try-on-option-in-result-method.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/ui/try-trait/try-on-option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn main() {}

fn foo() -> Result<u32, ()> {
let x: Option<u32> = None;
x?; //~ ERROR `?` couldn't convert the error to `()`
x?; //~ ERROR the `?` operator
Ok(22)
}

Expand Down
17 changes: 9 additions & 8 deletions src/test/ui/try-trait/try-on-option.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
error[E0277]: `?` couldn't convert the error to `()`
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:7:5
|
LL | fn foo() -> Result<u32, ()> {
| --------------- expected `()` because of this
LL | let x: Option<u32> = None;
LL | x?;
| ^^ the trait `From<result::sadness::PleaseCallTheOkOrMethodToUseQuestionMarkOnOptionsInAMethodThatReturnsResult>` is not implemented for `()`
LL | / fn foo() -> Result<u32, ()> {
LL | | let x: Option<u32> = None;
LL | | x?;
| | ^^ cannot use the `?` operator in a function that returns `Result<u32, ()>`
LL | | Ok(22)
LL | | }
| |_- this function should return `Result` or `Option` to accept `?`
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= note: required because of the requirements on the impl of `FromResidual<Option<!>>` for `Result<u32, ()>`
= help: the trait `FromResidual<Option<!>>` is not implemented for `Result<u32, ()>`
= note: required by `from_residual`

error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
Expand Down

0 comments on commit 508ed8d

Please sign in to comment.