From 2bd104fd4ffaf2a72799b5d49fcea3931e6a9e15 Mon Sep 17 00:00:00 2001 From: Hunter Praska Date: Wed, 7 Jun 2017 22:51:45 -0500 Subject: [PATCH 1/5] Impl Try for Option --- src/libcore/option.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 138e04c7737e0..fa6e309547915 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -146,7 +146,7 @@ #![stable(feature = "rust1", since = "1.0.0")] use iter::{FromIterator, FusedIterator, TrustedLen}; -use mem; +use {mem, ops}; // Note that this is not a lang item per se, but it has a hidden dependency on // `Iterator`, which is one. The compiler assumes that the `next` method of @@ -1123,3 +1123,26 @@ impl> FromIterator> for Option { } } } + +/// The `Option` type. See [the module level documentation](index.html) for more. +#[unstable(feature = "try_trait", issue = "42327")] +#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] +pub struct Missing; + +#[unstable(feature = "try_trait", issue = "42327")] +impl ops::Try for Option { + type Ok = T; + type Error = Missing; + + fn into_result(self) -> Result { + self.ok_or(Missing) + } + + fn from_ok(v: T) -> Self { + Some(v) + } + + fn from_error(_: Missing) -> Self { + None + } +} From f098d7be2978d8df3d180b6afae435468fc050de Mon Sep 17 00:00:00 2001 From: Hunter Praska Date: Wed, 7 Jun 2017 22:52:13 -0500 Subject: [PATCH 2/5] Add tests for Option and Result Try impl --- src/libcore/tests/lib.rs | 1 + src/libcore/tests/option.rs | 27 +++++++++++++++++++++++++++ src/libcore/tests/result.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs index 47995597a0a91..938bc3556654e 100644 --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@ -38,6 +38,7 @@ #![feature(test)] #![feature(trusted_len)] #![feature(try_from)] +#![feature(try_trait)] #![feature(unique)] #![feature(const_atomic_bool_new)] diff --git a/src/libcore/tests/option.rs b/src/libcore/tests/option.rs index 6bac55575fb18..00700b36b6982 100644 --- a/src/libcore/tests/option.rs +++ b/src/libcore/tests/option.rs @@ -270,3 +270,30 @@ fn test_cloned() { assert_eq!(opt_ref_ref.clone().cloned(), Some(&val)); assert_eq!(opt_ref_ref.cloned().cloned(), Some(1)); } + +#[test] +fn test_try() { + fn try_option_some() -> Option { + let val = Some(1)?; + Some(val) + } + assert_eq!(try_option_some(), Some(1)); + + fn try_option_none() -> Option { + let val = None?; + Some(val) + } + assert_eq!(try_option_none(), None); + + fn try_option_ok() -> Result { + let val = Ok(1)?; + Ok(val) + } + assert_eq!(try_option_ok(), Ok(1)); + + fn try_option_err() -> Result { + let val = Err(Missing)?; + Ok(val) + } + assert_eq!(try_option_err(), Err(Missing)); +} diff --git a/src/libcore/tests/result.rs b/src/libcore/tests/result.rs index 4c5f19dee1293..37264980dc703 100644 --- a/src/libcore/tests/result.rs +++ b/src/libcore/tests/result.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use core::option::*; + fn op1() -> Result { Ok(666) } fn op2() -> Result { Err("sadface") } @@ -202,3 +204,30 @@ pub fn test_unwrap_or_default() { assert_eq!(op1().unwrap_or_default(), 666); assert_eq!(op2().unwrap_or_default(), 0); } + +#[test] +fn test_try() { + fn try_result_some() -> Option { + let val = Ok(1)?; + Some(val) + } + assert_eq!(try_result_some(), Some(1)); + + fn try_result_none() -> Option { + let val = Err(Missing)?; + Some(val) + } + assert_eq!(try_result_none(), None); + + fn try_result_ok() -> Result { + let val = Ok(1)?; + Ok(val) + } + assert_eq!(try_result_ok(), Ok(1)); + + fn try_result_err() -> Result { + let val = Err(1)?; + Ok(val) + } + assert_eq!(try_result_err(), Err(1)); +} From 8f63e8de464155e570ed81905ff203557dd02ac9 Mon Sep 17 00:00:00 2001 From: Hunter Praska Date: Thu, 8 Jun 2017 14:02:04 -0500 Subject: [PATCH 3/5] Add docs for Missing, correct Option's Try test --- src/libcore/option.rs | 2 +- src/libcore/tests/option.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index fa6e309547915..0c3339590dbeb 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1124,7 +1124,7 @@ impl> FromIterator> for Option { } } -/// The `Option` type. See [the module level documentation](index.html) for more. +/// The equivalent of `Option::None` for a `Result::Err`. #[unstable(feature = "try_trait", issue = "42327")] #[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] pub struct Missing; diff --git a/src/libcore/tests/option.rs b/src/libcore/tests/option.rs index 00700b36b6982..e2907e1dd896f 100644 --- a/src/libcore/tests/option.rs +++ b/src/libcore/tests/option.rs @@ -286,13 +286,13 @@ fn test_try() { assert_eq!(try_option_none(), None); fn try_option_ok() -> Result { - let val = Ok(1)?; + let val = Some(1)?; Ok(val) } assert_eq!(try_option_ok(), Ok(1)); fn try_option_err() -> Result { - let val = Err(Missing)?; + let val = None?; Ok(val) } assert_eq!(try_option_err(), Err(Missing)); From 28996db803b958b235f5f3e95bc1762955de8a05 Mon Sep 17 00:00:00 2001 From: Hunter Praska Date: Sun, 18 Jun 2017 13:07:09 -0500 Subject: [PATCH 4/5] Rename option::Missing to NoneError --- src/libcore/option.rs | 15 +++++++++------ src/libcore/tests/option.rs | 6 +++--- src/libcore/tests/result.rs | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 0c3339590dbeb..980ea551f0806 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1124,25 +1124,28 @@ impl> FromIterator> for Option { } } -/// The equivalent of `Option::None` for a `Result::Err`. +/// The error type that results from applying the try operator (`?`) to a `None` value. If you wish +/// to allow `x?` (where `x` is an `Option`) to be converted into your error type, you can +/// implement `impl From` for `YourErrorType`. In that case, `x?` within a function that +/// returns `Result<_, YourErrorType>` will translate a `None` value into an `Err` result. #[unstable(feature = "try_trait", issue = "42327")] #[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] -pub struct Missing; +pub struct NoneError; #[unstable(feature = "try_trait", issue = "42327")] impl ops::Try for Option { type Ok = T; - type Error = Missing; + type Error = NoneError; - fn into_result(self) -> Result { - self.ok_or(Missing) + fn into_result(self) -> Result { + self.ok_or(NoneError) } fn from_ok(v: T) -> Self { Some(v) } - fn from_error(_: Missing) -> Self { + fn from_error(_: NoneError) -> Self { None } } diff --git a/src/libcore/tests/option.rs b/src/libcore/tests/option.rs index e2907e1dd896f..22109e28edd9b 100644 --- a/src/libcore/tests/option.rs +++ b/src/libcore/tests/option.rs @@ -285,15 +285,15 @@ fn test_try() { } assert_eq!(try_option_none(), None); - fn try_option_ok() -> Result { + fn try_option_ok() -> Result { let val = Some(1)?; Ok(val) } assert_eq!(try_option_ok(), Ok(1)); - fn try_option_err() -> Result { + fn try_option_err() -> Result { let val = None?; Ok(val) } - assert_eq!(try_option_err(), Err(Missing)); + assert_eq!(try_option_err(), Err(NoneError)); } diff --git a/src/libcore/tests/result.rs b/src/libcore/tests/result.rs index 37264980dc703..ce41bde8342ed 100644 --- a/src/libcore/tests/result.rs +++ b/src/libcore/tests/result.rs @@ -214,7 +214,7 @@ fn test_try() { assert_eq!(try_result_some(), Some(1)); fn try_result_none() -> Option { - let val = Err(Missing)?; + let val = Err(NoneError)?; Some(val) } assert_eq!(try_result_none(), None); From e30d92bb2d885629b51a5511b58109c94a1c56da Mon Sep 17 00:00:00 2001 From: Hunter Praska Date: Fri, 22 Sep 2017 10:24:06 -0500 Subject: [PATCH 5/5] Add UI tests --- src/test/ui/suggestions/try-on-option.rs | 25 ++++++++++++++++++++ src/test/ui/suggestions/try-on-option.stderr | 22 +++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/test/ui/suggestions/try-on-option.rs create mode 100644 src/test/ui/suggestions/try-on-option.stderr diff --git a/src/test/ui/suggestions/try-on-option.rs b/src/test/ui/suggestions/try-on-option.rs new file mode 100644 index 0000000000000..4cd8cd81151cc --- /dev/null +++ b/src/test/ui/suggestions/try-on-option.rs @@ -0,0 +1,25 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(try_trait)] + +fn main() {} + +fn foo() -> Result { + let x: Option = None; + x?; + Ok(22) +} + +fn bar() -> u32 { + let x: Option = None; + x?; + 22 +} diff --git a/src/test/ui/suggestions/try-on-option.stderr b/src/test/ui/suggestions/try-on-option.stderr new file mode 100644 index 0000000000000..86d4510cad3c2 --- /dev/null +++ b/src/test/ui/suggestions/try-on-option.stderr @@ -0,0 +1,22 @@ +error[E0277]: the trait bound `(): std::convert::From` is not satisfied + --> $DIR/try-on-option.rs:17:5 + | +17 | x?; + | ^^ the trait `std::convert::From` is not implemented for `()` + | + = note: required by `std::convert::From::from` + +error[E0277]: the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`) + --> $DIR/try-on-option.rs:23:5 + | +23 | x?; + | -- + | | + | cannot use the `?` operator in a function that returns `u32` + | in this macro invocation + | + = help: the trait `std::ops::Try` is not implemented for `u32` + = note: required by `std::ops::Try::from_error` + +error: aborting due to 2 previous errors +