Skip to content

Commit 291b4ed

Browse files
committed
add error message for the other case too
1 parent efa09ea commit 291b4ed

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/libcore/ops/try.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
message="the `?` operator can only be used in a \
2828
function that returns `Result` \
2929
(or another type that implements `{Try}`)",
30-
label="cannot use the `?` operator in a function that returns `{Self}`")))]
30+
label="cannot use the `?` operator in a function that returns `{Self}`"),
31+
on(all(from_method="into_result", from_desugaring="?"),
32+
message="the `?` operator can only be applied to values \
33+
that implement `{Try}`",
34+
label="the `?` operator cannot be applied to type `{Self}`")
35+
))]
3136
pub trait Try {
3237
/// The type of this value when viewed as successful.
3338
#[unstable(feature = "try_trait", issue = "42327")]

src/test/ui/suggestions/try-operator-on-main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main() {
1616
// error for a `Try` type on a non-`Try` fn
1717
std::fs::File::open("foo")?;
1818

19-
// a non-`Try` type on a `Try` fn
19+
// a non-`Try` type on a non-`Try` fn
2020
()?;
2121

2222
// an unrelated use of `Try`

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ error[E0277]: the `?` operator can only be used in a function that returns `Resu
1010
= help: the trait `std::ops::Try` is not implemented for `()`
1111
= note: required by `std::ops::Try::from_error`
1212

13-
error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
13+
error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
1414
--> $DIR/try-operator-on-main.rs:20:5
1515
|
1616
20 | ()?;
1717
| ---
1818
| |
19-
| the trait `std::ops::Try` is not implemented for `()`
19+
| the `?` operator cannot be applied to type `()`
2020
| in this macro invocation
2121
|
22+
= help: the trait `std::ops::Try` is not implemented for `()`
2223
= note: required by `std::ops::Try::into_result`
2324

2425
error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
@@ -29,15 +30,16 @@ error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
2930
|
3031
= note: required by `try_trait_generic`
3132

32-
error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
33+
error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
3334
--> $DIR/try-operator-on-main.rs:30:5
3435
|
3536
30 | ()?;
3637
| ---
3738
| |
38-
| the trait `std::ops::Try` is not implemented for `()`
39+
| the `?` operator cannot be applied to type `()`
3940
| in this macro invocation
4041
|
42+
= help: the trait `std::ops::Try` is not implemented for `()`
4143
= note: required by `std::ops::Try::into_result`
4244

4345
error: aborting due to 4 previous errors

0 commit comments

Comments
 (0)