Skip to content

Commit

Permalink
added enclosing_scope attr to Try trait and fixed ui tests accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
basil-cow committed Nov 25, 2019
1 parent 45aadf7 commit 1d0c015
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 23 deletions.
7 changes: 4 additions & 3 deletions src/libcore/ops/try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
/// extracting those success or failure values from an existing instance and
/// creating a new instance from a success or failure value.
#[unstable(feature = "try_trait", issue = "42327")]
#[rustc_on_unimplemented(
#[cfg_attr(not(bootstrap), rustc_on_unimplemented(
on(all(
any(from_method="from_error", from_method="from_ok"),
from_desugaring="QuestionMark"),
message="the `?` operator can only be used in {ItemContext} \
that returns `Result` or `Option` \
(or another type that implements `{Try}`)",
label="cannot use the `?` operator in {ItemContext} that returns `{Self}`"),
label="cannot use the `?` operator in {ItemContext} that returns `{Self}`",
enclosing_scope="this function should return `Result` or `Option` to accept `?`"),
on(all(from_method="into_result", from_desugaring="QuestionMark"),
message="the `?` operator can only be applied to values \
that implement `{Try}`",
label="the `?` operator cannot be applied to type `{Self}`")
)]
))]
#[doc(alias = "?")]
pub trait Try {
/// The type of this value when viewed as successful.
Expand Down
30 changes: 24 additions & 6 deletions src/test/ui/async-await/try-on-option-in-async.stderr
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
error[E0277]: the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> $DIR/try-on-option-in-async.rs:8:9
|
LL | x?;
| ^^ cannot use the `?` operator in an async block that returns `{integer}`
LL | async {
| ___________-
LL | | let x: Option<u32> = None;
LL | | x?;
| | ^^ cannot use the `?` operator in an async block that returns `{integer}`
LL | | 22
LL | | }.await
| |_____- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `std::ops::Try` is not implemented for `{integer}`
= note: required by `std::ops::Try::from_error`

error[E0277]: the `?` operator can only be used in an async closure that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> $DIR/try-on-option-in-async.rs:16:9
|
LL | x?;
| ^^ cannot use the `?` operator in an async closure that returns `u32`
LL | let async_closure = async || {
| __________________________________-
LL | | let x: Option<u32> = None;
LL | | x?;
| | ^^ cannot use the `?` operator in an async closure that returns `u32`
LL | | 22_u32
LL | | };
| |_____- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `std::ops::Try` is not implemented for `u32`
= note: required by `std::ops::Try::from_error`

error[E0277]: the `?` operator can only be used in an async function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> $DIR/try-on-option-in-async.rs:25:5
|
LL | x?;
| ^^ cannot use the `?` operator in an async function that returns `u32`
LL | async fn an_async_function() -> u32 {
| _____________________________________-
LL | | let x: Option<u32> = None;
LL | | x?;
| | ^^ cannot use the `?` operator in an async function that returns `u32`
LL | | 22
LL | | }
| |_- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `std::ops::Try` is not implemented for `u32`
= note: required by `std::ops::Try::from_error`
Expand Down
39 changes: 33 additions & 6 deletions src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,17 @@ LL | if (let 0 = 0)? {}
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> $DIR/disallowed-positions.rs:46:8
|
LL | if (let 0 = 0)? {}
| ^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
LL | / fn nested_within_if_expr() {
LL | | if &let 0 = 0 {}
LL | |
LL | |
... |
LL | | if (let 0 = 0)? {}
| | ^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
... |
LL | | if let true = let true = true {}
LL | | }
| |_- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`
Expand Down Expand Up @@ -754,8 +763,17 @@ LL | while (let 0 = 0)? {}
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> $DIR/disallowed-positions.rs:110:11
|
LL | while (let 0 = 0)? {}
| ^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
LL | / fn nested_within_while_expr() {
LL | | while &let 0 = 0 {}
LL | |
LL | |
... |
LL | | while (let 0 = 0)? {}
| | ^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
... |
LL | | while let true = let true = true {}
LL | | }
| |_- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`
Expand Down Expand Up @@ -924,8 +942,17 @@ LL | (let 0 = 0)?;
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> $DIR/disallowed-positions.rs:183:5
|
LL | (let 0 = 0)?;
| ^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
LL | / fn outside_if_and_while_expr() {
LL | | &let 0 = 0;
LL | |
LL | | !let 0 = 0;
... |
LL | | (let 0 = 0)?;
| | ^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
... |
LL | |
LL | | }
| |_- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`
Expand Down
19 changes: 15 additions & 4 deletions src/test/ui/try-on-option-diagnostics.stderr
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> $DIR/try-on-option-diagnostics.rs:7:5
|
LL | x?;
| ^^ cannot use the `?` operator in a function that returns `u32`
LL | / fn a_function() -> u32 {
LL | | let x: Option<u32> = None;
LL | | x?;
| | ^^ cannot use the `?` operator in a function that returns `u32`
LL | | 22
LL | | }
| |_- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `std::ops::Try` is not implemented for `u32`
= note: required by `std::ops::Try::from_error`

error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> $DIR/try-on-option-diagnostics.rs:14:9
|
LL | x?;
| ^^ cannot use the `?` operator in a closure that returns `{integer}`
LL | let a_closure = || {
| _____________________-
LL | | let x: Option<u32> = None;
LL | | x?;
| | ^^ cannot use the `?` operator in a closure that returns `{integer}`
LL | | 22
LL | | };
| |_____- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `std::ops::Try` is not implemented for `{integer}`
= note: required by `std::ops::Try::from_error`
Expand Down
9 changes: 7 additions & 2 deletions src/test/ui/try-on-option.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ LL | x?;
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> $DIR/try-on-option.rs:13:5
|
LL | x?;
| ^^ cannot use the `?` operator in a function that returns `u32`
LL | / fn bar() -> u32 {
LL | | let x: Option<u32> = None;
LL | | x?;
| | ^^ cannot use the `?` operator in a function that returns `u32`
LL | | 22
LL | | }
| |_- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `std::ops::Try` is not implemented for `u32`
= note: required by `std::ops::Try::from_error`
Expand Down
11 changes: 9 additions & 2 deletions src/test/ui/try-operator-on-main.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> $DIR/try-operator-on-main.rs:9:5
|
LL | std::fs::File::open("foo")?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
LL | / fn main() {
LL | | // error for a `Try` type on a non-`Try` fn
LL | | std::fs::File::open("foo")?;
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
LL | |
... |
LL | | try_trait_generic::<()>();
LL | | }
| |_- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`
Expand Down

0 comments on commit 1d0c015

Please sign in to comment.