From a440c4a10c9694465c679922f0dcec9d463f3d6b Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 26 Mar 2016 12:10:05 -0500 Subject: [PATCH 1/2] fix ? expansion in comment --- src/test/run-pass/try-operator-hygiene.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/run-pass/try-operator-hygiene.rs b/src/test/run-pass/try-operator-hygiene.rs index 233c03df4e5..ae622df498f 100644 --- a/src/test/run-pass/try-operator-hygiene.rs +++ b/src/test/run-pass/try-operator-hygiene.rs @@ -12,7 +12,7 @@ // // match expr { // Ok(val) => val, -// Err(err) => return From::from(err), +// Err(err) => return Err(From::from(err)), // } // // This test verifies that the expansion is hygienic, i.e. it's not affected by other `val` and From 064ec35c18f396c2c26c05517290fdc1a4379586 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 26 Mar 2016 12:10:16 -0500 Subject: [PATCH 2/2] add regression test for try! --- src/test/run-pass/try-macro.rs | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/test/run-pass/try-macro.rs diff --git a/src/test/run-pass/try-macro.rs b/src/test/run-pass/try-macro.rs new file mode 100644 index 00000000000..a12e20702d2 --- /dev/null +++ b/src/test/run-pass/try-macro.rs @@ -0,0 +1,57 @@ +// Copyright 2016 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. + +use std::num::{ParseFloatError, ParseIntError}; + +fn main() { + assert_eq!(simple(), Ok(1)); + assert_eq!(nested(), Ok(2)); + assert_eq!(merge_ok(), Ok(3.0)); + assert_eq!(merge_int_err(), Err(Error::Int)); + assert_eq!(merge_float_err(), Err(Error::Float)); +} + +fn simple() -> Result { + Ok(try!("1".parse())) +} + +fn nested() -> Result { + Ok(try!(try!("2".parse::()).to_string().parse::())) +} + +fn merge_ok() -> Result { + Ok(try!("1".parse::()) as f32 + try!("2.0".parse::())) +} + +fn merge_int_err() -> Result { + Ok(try!("a".parse::()) as f32 + try!("2.0".parse::())) +} + +fn merge_float_err() -> Result { + Ok(try!("1".parse::()) as f32 + try!("b".parse::())) +} + +#[derive(Debug, PartialEq)] +enum Error { + Int, + Float, +} + +impl From for Error { + fn from(_: ParseIntError) -> Error { + Error::Int + } +} + +impl From for Error { + fn from(_: ParseFloatError) -> Error { + Error::Float + } +}