1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // ignore-cloudabi no std::fs
14 use std::io::{Read, self};
15 use std::num::ParseIntError;
16 use std::str::FromStr;
18 fn on_method() -> Result<i32, ParseIntError> {
19 Ok("1".parse::<i32>()? + "2".parse::<i32>()?)
22 fn in_chain() -> Result<String, ParseIntError> {
23 Ok("3".parse::<i32>()?.to_string())
26 fn on_call() -> Result<i32, ParseIntError> {
27 fn parse<T: FromStr>(s: &str) -> Result<T, T::Err> {
34 fn nested() -> Result<i32, ParseIntError> {
35 Ok("5".parse::<i32>()?.to_string().parse()?)
38 fn on_path() -> Result<i32, ParseIntError> {
39 let x = "6".parse::<i32>();
44 fn on_macro() -> Result<i32, ParseIntError> {
49 Ok(id!("7".parse::<i32>())?)
52 fn on_parens() -> Result<i32, ParseIntError> {
53 let x = "8".parse::<i32>();
58 fn on_block() -> Result<i32, ParseIntError> {
59 let x = "9".parse::<i32>();
64 fn on_field() -> Result<i32, ParseIntError> {
65 struct Pair<A, B> { a: A, b: B }
67 let x = Pair { a: "10".parse::<i32>(), b: 0 };
72 fn on_tuple_field() -> Result<i32, ParseIntError> {
73 let x = ("11".parse::<i32>(), 0);
78 fn on_try() -> Result<i32, ParseIntError> {
79 let x = "12".parse::<i32>().map(|i| i.to_string().parse::<i32>());
84 fn on_binary_op() -> Result<i32, ParseIntError> {
85 let x = 13 - "14".parse::<i32>()?;
86 let y = "15".parse::<i32>()? - 16;
87 let z = "17".parse::<i32>()? - "18".parse::<i32>()?;
92 fn on_index() -> Result<i32, ParseIntError> {
94 let y = "0".parse::<usize>();
99 fn on_args() -> Result<i32, ParseIntError> {
100 fn sub(x: i32, y: i32) -> i32 { x - y }
102 let x = "20".parse();
103 let y = "21".parse();
108 fn on_if() -> Result<i32, ParseIntError> {
116 fn on_if_let() -> Result<i32, ParseIntError> {
117 Ok(if let Ok(..) = "24".parse::<i32>() {
124 fn on_match() -> Result<i32, ParseIntError> {
125 Ok(match "27".parse::<i32>() {
126 Err(..) => "28".parse::<i32>(),
127 Ok(..) => "29".parse::<i32>(),
131 fn tight_binding() -> Result<bool, ()> {
132 fn ok<T>(x: T) -> Result<T, ()> { Ok(x) }
139 fn merge_error() -> Result<i32, Error> {
140 let mut s = String::new();
142 File::open("foo.txt")?.read_to_string(&mut s)?;
144 Ok(s.parse::<i32>()? + 1)
148 assert_eq!(Ok(3), on_method());
150 assert_eq!(Ok("3".to_string()), in_chain());
152 assert_eq!(Ok(4), on_call());
154 assert_eq!(Ok(5), nested());
156 assert_eq!(Ok(6), on_path());
158 assert_eq!(Ok(7), on_macro());
160 assert_eq!(Ok(8), on_parens());
162 assert_eq!(Ok(9), on_block());
164 assert_eq!(Ok(10), on_field());
166 assert_eq!(Ok(11), on_tuple_field());
168 assert_eq!(Ok(12), on_try());
170 assert_eq!(Ok(-3), on_binary_op());
172 assert_eq!(Ok(19), on_index());
174 assert_eq!(Ok(-1), on_args());
176 assert_eq!(Ok(22), on_if());
178 assert_eq!(Ok(25), on_if_let());
180 assert_eq!(Ok(29), on_match());
182 assert_eq!(Ok(false), tight_binding());
187 Parse(ParseIntError),
190 impl From<io::Error> for Error {
191 fn from(e: io::Error) -> Error {
196 impl From<ParseIntError> for Error {
197 fn from(e: ParseIntError) -> Error {