From 291b4eddcefd1d6da1010f8a1f02f05338a44050 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Thu, 31 Aug 2017 22:52:49 +0300 Subject: [PATCH] add error message for the other case too --- src/libcore/ops/try.rs | 7 ++++++- src/test/ui/suggestions/try-operator-on-main.rs | 2 +- src/test/ui/suggestions/try-operator-on-main.stderr | 10 ++++++---- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/libcore/ops/try.rs b/src/libcore/ops/try.rs index 28e84d2fba69..e788b66a1ec8 100644 --- a/src/libcore/ops/try.rs +++ b/src/libcore/ops/try.rs @@ -27,7 +27,12 @@ message="the `?` operator can only be used in a \ function that returns `Result` \ (or another type that implements `{Try}`)", - label="cannot use the `?` operator in a function that returns `{Self}`")))] + label="cannot use the `?` operator in a function that returns `{Self}`"), + on(all(from_method="into_result", from_desugaring="?"), + message="the `?` operator can only be applied to values \ + that implement `{Try}`", + label="the `?` operator cannot be applied to type `{Self}`") +))] pub trait Try { /// The type of this value when viewed as successful. #[unstable(feature = "try_trait", issue = "42327")] diff --git a/src/test/ui/suggestions/try-operator-on-main.rs b/src/test/ui/suggestions/try-operator-on-main.rs index 473efea54de3..eadd12924df6 100644 --- a/src/test/ui/suggestions/try-operator-on-main.rs +++ b/src/test/ui/suggestions/try-operator-on-main.rs @@ -16,7 +16,7 @@ fn main() { // error for a `Try` type on a non-`Try` fn std::fs::File::open("foo")?; - // a non-`Try` type on a `Try` fn + // a non-`Try` type on a non-`Try` fn ()?; // an unrelated use of `Try` diff --git a/src/test/ui/suggestions/try-operator-on-main.stderr b/src/test/ui/suggestions/try-operator-on-main.stderr index 5fc24e46fa76..e83bf2abc150 100644 --- a/src/test/ui/suggestions/try-operator-on-main.stderr +++ b/src/test/ui/suggestions/try-operator-on-main.stderr @@ -10,15 +10,16 @@ error[E0277]: the `?` operator can only be used in a function that returns `Resu = help: the trait `std::ops::Try` is not implemented for `()` = note: required by `std::ops::Try::from_error` -error[E0277]: the trait bound `(): std::ops::Try` is not satisfied +error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try` --> $DIR/try-operator-on-main.rs:20:5 | 20 | ()?; | --- | | - | the trait `std::ops::Try` is not implemented for `()` + | the `?` operator cannot be applied to type `()` | in this macro invocation | + = help: the trait `std::ops::Try` is not implemented for `()` = note: required by `std::ops::Try::into_result` 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 | = note: required by `try_trait_generic` -error[E0277]: the trait bound `(): std::ops::Try` is not satisfied +error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try` --> $DIR/try-operator-on-main.rs:30:5 | 30 | ()?; | --- | | - | the trait `std::ops::Try` is not implemented for `()` + | the `?` operator cannot be applied to type `()` | in this macro invocation | + = help: the trait `std::ops::Try` is not implemented for `()` = note: required by `std::ops::Try::into_result` error: aborting due to 4 previous errors