Skip to content

Commit 1d0c015

Browse files
committed
added enclosing_scope attr to Try trait and fixed ui tests accordingly
1 parent 45aadf7 commit 1d0c015

File tree

6 files changed

+92
-23
lines changed

6 files changed

+92
-23
lines changed

src/libcore/ops/try.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
/// extracting those success or failure values from an existing instance and
66
/// creating a new instance from a success or failure value.
77
#[unstable(feature = "try_trait", issue = "42327")]
8-
#[rustc_on_unimplemented(
8+
#[cfg_attr(not(bootstrap), rustc_on_unimplemented(
99
on(all(
1010
any(from_method="from_error", from_method="from_ok"),
1111
from_desugaring="QuestionMark"),
1212
message="the `?` operator can only be used in {ItemContext} \
1313
that returns `Result` or `Option` \
1414
(or another type that implements `{Try}`)",
15-
label="cannot use the `?` operator in {ItemContext} that returns `{Self}`"),
15+
label="cannot use the `?` operator in {ItemContext} that returns `{Self}`",
16+
enclosing_scope="this function should return `Result` or `Option` to accept `?`"),
1617
on(all(from_method="into_result", from_desugaring="QuestionMark"),
1718
message="the `?` operator can only be applied to values \
1819
that implement `{Try}`",
1920
label="the `?` operator cannot be applied to type `{Self}`")
20-
)]
21+
))]
2122
#[doc(alias = "?")]
2223
pub trait Try {
2324
/// The type of this value when viewed as successful.

src/test/ui/async-await/try-on-option-in-async.stderr

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,44 @@
11
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`)
22
--> $DIR/try-on-option-in-async.rs:8:9
33
|
4-
LL | x?;
5-
| ^^ cannot use the `?` operator in an async block that returns `{integer}`
4+
LL | async {
5+
| ___________-
6+
LL | | let x: Option<u32> = None;
7+
LL | | x?;
8+
| | ^^ cannot use the `?` operator in an async block that returns `{integer}`
9+
LL | | 22
10+
LL | | }.await
11+
| |_____- this function should return `Result` or `Option` to accept `?`
612
|
713
= help: the trait `std::ops::Try` is not implemented for `{integer}`
814
= note: required by `std::ops::Try::from_error`
915

1016
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`)
1117
--> $DIR/try-on-option-in-async.rs:16:9
1218
|
13-
LL | x?;
14-
| ^^ cannot use the `?` operator in an async closure that returns `u32`
19+
LL | let async_closure = async || {
20+
| __________________________________-
21+
LL | | let x: Option<u32> = None;
22+
LL | | x?;
23+
| | ^^ cannot use the `?` operator in an async closure that returns `u32`
24+
LL | | 22_u32
25+
LL | | };
26+
| |_____- this function should return `Result` or `Option` to accept `?`
1527
|
1628
= help: the trait `std::ops::Try` is not implemented for `u32`
1729
= note: required by `std::ops::Try::from_error`
1830

1931
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`)
2032
--> $DIR/try-on-option-in-async.rs:25:5
2133
|
22-
LL | x?;
23-
| ^^ cannot use the `?` operator in an async function that returns `u32`
34+
LL | async fn an_async_function() -> u32 {
35+
| _____________________________________-
36+
LL | | let x: Option<u32> = None;
37+
LL | | x?;
38+
| | ^^ cannot use the `?` operator in an async function that returns `u32`
39+
LL | | 22
40+
LL | | }
41+
| |_- this function should return `Result` or `Option` to accept `?`
2442
|
2543
= help: the trait `std::ops::Try` is not implemented for `u32`
2644
= note: required by `std::ops::Try::from_error`

src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr

+33-6
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,17 @@ LL | if (let 0 = 0)? {}
575575
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
576576
--> $DIR/disallowed-positions.rs:46:8
577577
|
578-
LL | if (let 0 = 0)? {}
579-
| ^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
578+
LL | / fn nested_within_if_expr() {
579+
LL | | if &let 0 = 0 {}
580+
LL | |
581+
LL | |
582+
... |
583+
LL | | if (let 0 = 0)? {}
584+
| | ^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
585+
... |
586+
LL | | if let true = let true = true {}
587+
LL | | }
588+
| |_- this function should return `Result` or `Option` to accept `?`
580589
|
581590
= help: the trait `std::ops::Try` is not implemented for `()`
582591
= note: required by `std::ops::Try::from_error`
@@ -754,8 +763,17 @@ LL | while (let 0 = 0)? {}
754763
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
755764
--> $DIR/disallowed-positions.rs:110:11
756765
|
757-
LL | while (let 0 = 0)? {}
758-
| ^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
766+
LL | / fn nested_within_while_expr() {
767+
LL | | while &let 0 = 0 {}
768+
LL | |
769+
LL | |
770+
... |
771+
LL | | while (let 0 = 0)? {}
772+
| | ^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
773+
... |
774+
LL | | while let true = let true = true {}
775+
LL | | }
776+
| |_- this function should return `Result` or `Option` to accept `?`
759777
|
760778
= help: the trait `std::ops::Try` is not implemented for `()`
761779
= note: required by `std::ops::Try::from_error`
@@ -924,8 +942,17 @@ LL | (let 0 = 0)?;
924942
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
925943
--> $DIR/disallowed-positions.rs:183:5
926944
|
927-
LL | (let 0 = 0)?;
928-
| ^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
945+
LL | / fn outside_if_and_while_expr() {
946+
LL | | &let 0 = 0;
947+
LL | |
948+
LL | | !let 0 = 0;
949+
... |
950+
LL | | (let 0 = 0)?;
951+
| | ^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
952+
... |
953+
LL | |
954+
LL | | }
955+
| |_- this function should return `Result` or `Option` to accept `?`
929956
|
930957
= help: the trait `std::ops::Try` is not implemented for `()`
931958
= note: required by `std::ops::Try::from_error`

src/test/ui/try-on-option-diagnostics.stderr

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
22
--> $DIR/try-on-option-diagnostics.rs:7:5
33
|
4-
LL | x?;
5-
| ^^ cannot use the `?` operator in a function that returns `u32`
4+
LL | / fn a_function() -> u32 {
5+
LL | | let x: Option<u32> = None;
6+
LL | | x?;
7+
| | ^^ cannot use the `?` operator in a function that returns `u32`
8+
LL | | 22
9+
LL | | }
10+
| |_- this function should return `Result` or `Option` to accept `?`
611
|
712
= help: the trait `std::ops::Try` is not implemented for `u32`
813
= note: required by `std::ops::Try::from_error`
914

1015
error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
1116
--> $DIR/try-on-option-diagnostics.rs:14:9
1217
|
13-
LL | x?;
14-
| ^^ cannot use the `?` operator in a closure that returns `{integer}`
18+
LL | let a_closure = || {
19+
| _____________________-
20+
LL | | let x: Option<u32> = None;
21+
LL | | x?;
22+
| | ^^ cannot use the `?` operator in a closure that returns `{integer}`
23+
LL | | 22
24+
LL | | };
25+
| |_____- this function should return `Result` or `Option` to accept `?`
1526
|
1627
= help: the trait `std::ops::Try` is not implemented for `{integer}`
1728
= note: required by `std::ops::Try::from_error`

src/test/ui/try-on-option.stderr

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ LL | x?;
1010
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
1111
--> $DIR/try-on-option.rs:13:5
1212
|
13-
LL | x?;
14-
| ^^ cannot use the `?` operator in a function that returns `u32`
13+
LL | / fn bar() -> u32 {
14+
LL | | let x: Option<u32> = None;
15+
LL | | x?;
16+
| | ^^ cannot use the `?` operator in a function that returns `u32`
17+
LL | | 22
18+
LL | | }
19+
| |_- this function should return `Result` or `Option` to accept `?`
1520
|
1621
= help: the trait `std::ops::Try` is not implemented for `u32`
1722
= note: required by `std::ops::Try::from_error`

src/test/ui/try-operator-on-main.stderr

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
22
--> $DIR/try-operator-on-main.rs:9:5
33
|
4-
LL | std::fs::File::open("foo")?;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
4+
LL | / fn main() {
5+
LL | | // error for a `Try` type on a non-`Try` fn
6+
LL | | std::fs::File::open("foo")?;
7+
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
8+
LL | |
9+
... |
10+
LL | | try_trait_generic::<()>();
11+
LL | | }
12+
| |_- this function should return `Result` or `Option` to accept `?`
613
|
714
= help: the trait `std::ops::Try` is not implemented for `()`
815
= note: required by `std::ops::Try::from_error`

0 commit comments

Comments
 (0)