From 3bcdf9eb8cb9b58df2cb860e18db958da6658392 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Tue, 29 Sep 2015 05:15:59 +0200 Subject: [PATCH] =?UTF-8?q?Convert=20errors=20in=20libcore=E2=80=99s=20try?= =?UTF-8?q?!=20macro=20too.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It is still a separate definition from libstd’s so that `$crate` expands to `std::` in the latter, not `core::`. See discussion at https://github.com/rust-lang/rust/issues/27701 . There is an expectation that `core::foo` is the same as `std::foo`. We should not have two things with the same name but subtly different behavior. --- src/libcore/macros.rs | 5 ++--- src/libcore/result.rs | 2 ++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index 25ca33a3d5011..c8f5399a6f7af 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -154,16 +154,15 @@ macro_rules! debug_assert_eq { } /// Short circuiting evaluation on Err -/// -/// `libstd` contains a more general `try!` macro that uses `From`. #[macro_export] macro_rules! try { ($e:expr) => ({ use $crate::result::Result::{Ok, Err}; + use $crate::convert::From; match $e { Ok(e) => e, - Err(e) => return Err(e), + Err(e) => return Err(From::from(e)), } }) } diff --git a/src/libcore/result.rs b/src/libcore/result.rs index fe0fdb5baa5f3..8b9571e7461d1 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -223,6 +223,8 @@ //! } //! ``` //! +//! (The real `try!` macro uses `From` to convert between error types automatically.) +//! //! `try!` is imported by the prelude and is available everywhere, but it can only //! be used in functions that return `Result` because of the early return of //! `Err` that it provides.